This site requires JavaScript, please enable it in your browser!
Greenfoot back
manster2008
manster2008 wrote ...

2011/6/4

Creating Variables Infinitely

manster2008 manster2008

2011/6/4

#
Hi, I'm a bit new... I am making a snake game, and am trying to do it so that everytime a key is pressed, it creates a new variable and puts the x or y coordinate in it. How do I do this without creating like a thousand variables? I would be really grateful if you could help me.
nccb nccb

2011/6/4

#
One way or another you will need to store enough data to remember where they've been in the world. One way to do this would be a two-dimensional array. You could make it contain integers, and have 0 for empty, and 1 for when player 1 has passed through that square. This would make collision detection relatively straightforward, and then you could draw the array onto an image. Another method would be to store it directly in an image, painting when you move through, but then to do collision detection you'd need to pick colours out of the image and examine them. Perhaps the easiest way (especially if you don't know arrays) is just use a grid-based world and create a "SnakeTail" actor in each square (which has an empty act method) once the "SnakeHead" moves out of it. Greenfoot can handle having that many actors, if you don't have a very fine grid, and then you can just see what's in a square using the standard collision detection methods in Greenfoot, to see if there's a SnakeTail there.
You need to login to post a reply.