Results 1 to 7 of 7
  1. #1
    Growler is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default Snake Game Applet

    Hello everyone.. first post here.

    I've been working on my final project for Obj Oriented Design in Java...
    I am supposed to make a snake game (how original.. right?)...

    So far, I've got everything working... except... when the snake eats the piece of food, it doesn't grow.

    I understand what I should do conceptually.. but not sure how to code it, or which class to put the code in.

    What I want to do conceptually is:
    Create a loop, whenever the head of the snake(the part that is invoking move methods etc...) equals the position of the food, generate a new segment.

    I don't want it to exit the loop until the segment is added. When head position == food position again, re-run the loop and add another segment.

    I think I need bodySegment to know the position of the head at the time it eats the food... then somehow generate the segment... and follow the head around.

    So I've created a Snake class(the head)... and a BodySegment class, which should know about the position of the head when it eats the food... and a Tail class.

    In SnakePanel, the class that paints the snake to the screen, and invokes the move methods, I've added an ActionListener:

    Java Code:
    [COLOR="DarkOrchid"]   private class moveListener implements ActionListener { //actionlistener for timer
            public void actionPerformed(ActionEvent e) {
                _snake.move(); //this should animate the snake
                //need to check if the food an snake are in same location
                /*int aNumber;
                aNumber = (int)(Math.floor(Math.random()*2)+1);
                if (aNumber == */
                if((_snake.getX() == _foodPosX)&&(_snake.getY() == _foodPosY)) {
                    _foodPosX = ((int)(Math.random()*(getWidth()/_snake.getWidth())))
                    *((int)_snake.getWidth()); //casting height and width as int~ truncates the decimal
                    _foodPosY = ((int)(Math.random()*(getHeight()/_snake.getHeight())))
                    *((int)_snake.getHeight());
                    System.out.println("Food moved to " + _foodPosX + " " + _foodPosY);
                   
                                _eat.play();
                [/COLOR]
    Any idea on how I can code this? Or what I should do next?


    Thanks!!!
    Last edited by Growler; 07-11-2010 at 04:36 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One step at a time. Are you able to identify when the snake head reaches a cell with food? Does the code have a println statement that shows you that this works?

  3. #3
    Growler is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    Yeah. That's provided within the ActionListener:
    Java Code:
    [COLOR="Purple"]
    int aNumber;
    aNumber = (int)(Math.floor(Math.random()*2)+1);
    if (aNumber == */
    if((_snake.getX() == _foodPosX)&&(_snake.getY() == _foodPosY)) {
    _foodPosX = ((int)(Math.random()*(getWidth()/_snake.getWidth())))
    *((int)_snake.getWidth()); //casting height and width as int~ truncates the decimal
    _foodPosY = ((int)(Math.random()*(getHeight()/_snake.getHeight())))
    *((int)_snake.getHeight());[/COLOR]
    [COLOR="Green"]System.out.println("Food moved to " + _foodPosX + " " + _foodPosY);[/COLOR]
    I've provided this println statement which prints to console window telling me when snake head reaches the food position.
    Last edited by Growler; 07-11-2010 at 04:35 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your code is very difficult to read. Please edit your post (the edit button is on the lower right part of your post), and add code tags around your code. The tag [code] should be above your code, and [/code] should be below your code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]
    Much luck!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Does your Snake class have an addSegment() method? To me, one possible logic is to have the grid tell the snake when it reaches a food-containing grid cell, perhaps by calling a public method of snake, say, eat(), and then within that method, snake calls its own addSegment() method which adds a segment to the tail end of the snake.

  6. #6
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Emm...
    First of all it is not clear the snake principle... Is it a swing components stragedy or a graphics 2D thing?
    But anyway I think you should write a method which adds a point to the beginning of the snake as soon as its eats a point.
    So to solve this give us more information please.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    What you probably already have noticed there is no need to redraw the entire snake when it moves one step, i.e. you draw a new head part, redraw the old head part to be a 'neck' or 'body' part, you wipe out the tail part and redraw the next-to-tail as the new tail part. Let me enumerate those steps:

    1) draw new head
    2) redraw old head as body part
    3) wipe out tail part
    4) redraw next-to-tail part as new tail

    Now when the head part hits food (or any other bonus object for that matter) the snake collects n points. For the next n steps of movement simply skip step 3) and 4). The snake will grow. You have to keep track of every body part in your controller so the snake 'array' (or ArrayList) grows as well at every movement step.

    kind regards,

    Jos

Similar Threads

  1. Snake game in java
    By freaky in forum New To Java
    Replies: 5
    Last Post: 04-20-2010, 06:34 PM
  2. Java Applet Help - Making "Snake" Game
    By Alphimeda in forum Java Applets
    Replies: 15
    Last Post: 04-04-2010, 05:39 PM
  3. Snake Game
    By mustachMan in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 10:35 PM
  4. Snake game movement
    By BeerMonkey in forum New To Java
    Replies: 9
    Last Post: 11-27-2008, 12:48 PM
  5. An Applet Game
    By T3000 in forum New To Java
    Replies: 1
    Last Post: 11-18-2008, 04:18 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
  •