@Mene Do you have background(40,40,50) at the start of your draw() routine. You have to clear the screen of what was drawn previously before you draw the next screen. As with anything new, it takes time to learn all the little things.
About your drawing problem: you probably set the random coordinates in the draw function, right? So it picks a new random coordinate every step. What you want to do, is to set this random coordinate in the setup function, and use this in the draw function:
function setup() x = math.random(WIDTH) y = math.random(HEIGHT) end function draw() background(0) sprite("Asteroid", x, y) end
All step one does is place a sprite on the screen (note every line which starts – is a comment and doesn’t get executed
--# Step1 -- Snake tutorial -- by West --1. Place a sprite on the screen function setup() displayMode(FULLSCREEN) end function draw() background(40, 40, 50) --place the sprite at position 400,400 on the screen. Chose this sprite as it sort of looks like a snakes head sprite("Space Art:Part Green Hull 4",400,400) end
so without the comments it’s only
function setup() displayMode(FULLSCREEN) end function draw() background(40, 40, 50) sprite("Space Art:Part Green Hull 4",400,400) end
Step 2 is to add variables to hold the position x and y of the sprite and build on the last step (added x and y in setup and changed the call in the sprite instruction to point at x and y instead of 400,400
--# Step2 -- Snake tutorial -- by West --2. Add x and y variables to control the position function setup() displayMode(FULLSCREEN) --variable x to control the horizontal position of the snake head x=300 --variable y to control the vertical position of the snake head y=500 end function draw() background(40, 40, 50) --place the sprite at position x,y - you can change these in setup and later we will be changing these values in the draw loop to generate movement sprite("Space Art:Part Green Hull 4",x,y) end
There are several more steps in the link which builds up a very simple snake game. This might get you started.
@Mene - I had the same problem when I started with Codea, but once you understand that, and you’ve done it now, it becomes quite natural. You learn where to put your code based on what Codea invokes and when (which events: screen refresh, touch, keyboard typing, etc). @Kjell illustrated it very well.
I’m not a Codea expert nor a game developer, I don’t expect to be coding 3D games or shaders (at least not any time soon) but I’ve done a couple of simple games (puzzles) for my kids and recently I started playing with the physics module, which was a complete mistery when I first saw the examples. That’s how you eat an elephant, one bite at a time.
It can be a learning curve. I guess compared to “old school” programming, one of the things you do when you want to start making games is create the idea of a game loop. A game loop means you have a standard bit of stuff that every cycle does your logic and then redraws the screen. In old school terms on the likes of BASIC when you wanted to move in the direction of games you had to create the game loop first so you had a structure to work with.
Codea, as well as providing lua and the APIs to the iPad has a game loop built in as it’s standard approach. So you don’t have to build any of that, but it does mean that if you are using Codea, you are always in a game loop context, ie draw gets called as fast as it can be which if your code isn’t heavy will be 60x per second.
The next challenge with this, is that you have to be careful with game logic that it doesn’t assume 60 fps, because when it’s working hard it might be less. But that’s yet another more in depth topic…
Well, I’m an oldie and I come from programming simple games in basic in the mid 1980’s. (BBC basic, C64 basic etc)
There are quite a few of us in that boat. I grew up on BBC Basic and C64 as well and to me Codea feels like the closest thing to that that I know of. That’s not to say that it is the same - the difference you highlight being one of the biggest - but there’s much about it that feels similar.
So keep at it! I’d also recommend that you try West and Ignatz’s tutorials to get a hang of the logic.