Results 1 to 10 of 10
  1. #1
    BeerMonkey is offline Member
    Join Date
    Nov 2008
    Location
    Aberystwyth
    Posts
    5
    Rep Power
    0

    Default 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 :D
    Last edited by BeerMonkey; 11-18-2008 at 04:23 AM.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    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.

  3. #3
    BeerMonkey is offline Member
    Join Date
    Nov 2008
    Location
    Aberystwyth
    Posts
    5
    Rep Power
    0

    Red face

    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.

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    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.

  5. #5
    BeerMonkey is offline Member
    Join Date
    Nov 2008
    Location
    Aberystwyth
    Posts
    5
    Rep Power
    0

    Default

    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

  6. #6
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    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.

  7. #7
    BeerMonkey is offline Member
    Join Date
    Nov 2008
    Location
    Aberystwyth
    Posts
    5
    Rep Power
    0

    Default

    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

  8. #8
    BeerMonkey is offline Member
    Join Date
    Nov 2008
    Location
    Aberystwyth
    Posts
    5
    Rep Power
    0

    Default

    still cant get this to work :S anyone have any ideas?

  9. #9
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    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:
       }
    }

  10. #10
    Titanick is offline Member
    Join Date
    Nov 2008
    Posts
    1
    Rep Power
    0

    Default

    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

  1. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  2. Detecting user movement of a JFrame
    By dklett in forum AWT / Swing
    Replies: 4
    Last Post: 08-27-2008, 07:01 AM
  3. game
    By amith in forum AWT / Swing
    Replies: 0
    Last Post: 05-19-2008, 05:16 PM
  4. [SOLVED] File Movement/Manipulation
    By Leprechaun in forum New To Java
    Replies: 2
    Last Post: 04-23-2008, 12:39 AM
  5. Movement of balls
    By BlitzA in forum New To Java
    Replies: 8
    Last Post: 01-09-2008, 03:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •