Snake game movement
Hi guys, im doing a snake project and im not sure how to go about getting it to move,
im using a 2D array to draw the 31x31 grid.
i have the snake drawn on my grid and i know in theory how to make it move but im not sure how to link what i want it to do to a keypress.
(think we have to use the arrow keys) altho i will check that if it makes it easier to use aswd instead.
if you need anymore info or even any code pm or post.
You can post your code if you want, but I think it'd be better to give a general idea of how you want to implement it. It's a pretty simple program compared to lots of other things, so coding it once you know what you want to do shouldn't be hard.
well i have my snake printed in my grid using:
public static void snakePos()
int headX = 15;
int headY = 15;
grid[headY][headX] = "H";
grid = "*";
grid = "*";
grid = "*";
im initialy just starting with the head getting that going before i start to think about rest
say i press the up arrow i want it to take one from the headY co-ord to make the snake move up in the grid. im just not sure how to relate that action to the keypress or even how to check that a key has been pressed.
Your implementation so far is really basic and hasn't considered much about movement of the snake at all. Before thinking about key presses, have you though about how you know where the head will be moving next? It's a simple solution to keep track of the direction the snake is heading or the next square head will move onto. From there, key presses should be no problem.
How about what happens to the tail? This is one that probably needs more thought than the head and key presses, though it shouldn't be too much.
um well i was thinking of storing the new position and current position of the snake and just changing all of the tail parts new position to the current position of the part ahead of it. i dont understand what you mean about the direction. it needs to go in which ever direction the keypress tells it too ?
thanks mate :D
Is your snake constantly moving, or is it only moving when a key is pressed? If it's constantly moving, then you need to know where it is moving, even if a key isn't being pressed.
no only on keypress, as i am doing it in terminal i will need to keep reprinting the grid each time a key is pressed (unless anyone knows how otherwise) :D
still cant get this to work :S anyone have any ideas?
You have headX and headY. You just need to change the values (and anything else necessary) given certain input. Consider the following pseudocode:
Lol your doing the same assignment as me :p
I'm kind of stuck with the movement as well. I have got a single character to move about the grid, but I'm struggling to find a way to keep track of all the tail positions.
I'm trying to implement it so that each time I make a move, I take the end element of the tail away, and add it to the previous head position. I'm not sure how to pass the end tail co-ordinate on to the next tail position though.
Have you gotten any further with yours?