Results 1 to 10 of 10
Thread: Snake game movement
- 11-18-2008, 05:18 AM #1
Member
- Join Date
- Nov 2008
- Location
- Aberystwyth
- Posts
- 5
- Rep Power
- 0
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.
thanks :DLast edited by BeerMonkey; 11-18-2008 at 05:23 AM.
- 11-18-2008, 06:11 AM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 13
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.
- 11-18-2008, 06:20 AM #3
Member
- Join Date
- Nov 2008
- Location
- Aberystwyth
- Posts
- 5
- Rep Power
- 0
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[15][14] = "*";
grid[15][13] = "*";
grid[15][12] = "*";
}
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.
- 11-18-2008, 06:27 AM #4
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 13
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.
- 11-18-2008, 06:34 AM #5
Member
- Join Date
- Nov 2008
- Location
- Aberystwyth
- Posts
- 5
- Rep Power
- 0
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
- 11-18-2008, 07:09 AM #6
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 13
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.
- 11-18-2008, 10:47 AM #7
Member
- Join Date
- Nov 2008
- Location
- Aberystwyth
- Posts
- 5
- Rep Power
- 0
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
- 11-18-2008, 06:48 PM #8
Member
- Join Date
- Nov 2008
- Location
- Aberystwyth
- Posts
- 5
- Rep Power
- 0
still cant get this to work :S anyone have any ideas?
- 11-18-2008, 09:35 PM #9
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 13
You have headX and headY. You just need to change the values (and anything else necessary) given certain input. Consider the following pseudocode:
Java Code:move(arrow) { switch(arrow) { case up: case down: case left: case right: } }
- 11-27-2008, 01:48 PM #10
Member
- Join Date
- Nov 2008
- Posts
- 1
- Rep Power
- 0
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?
Similar Threads
-
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 05:49 AM -
Detecting user movement of a JFrame
By dklett in forum AWT / SwingReplies: 4Last Post: 08-27-2008, 08:01 AM -
game
By amith in forum AWT / SwingReplies: 0Last Post: 05-19-2008, 06:16 PM -
[SOLVED] File Movement/Manipulation
By Leprechaun in forum New To JavaReplies: 2Last Post: 04-23-2008, 01:39 AM -
Movement of balls
By BlitzA in forum New To JavaReplies: 8Last Post: 01-09-2008, 04:30 PM
Bookmarks