Results 1 to 7 of 7
Thread: Snake Game Applet
- 07-11-2010, 04:04 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
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:
Any idea on how I can code this? Or what I should do next?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]
Thanks!!!Last edited by Growler; 07-11-2010 at 04:36 AM.
-
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?
- 07-11-2010, 04:16 AM #3
Member
- Join Date
- Jul 2010
- Posts
- 11
- Rep Power
- 0
Yeah. That's provided within the ActionListener:
I've provided this println statement which prints to console window telling me when snake head reaches the food position.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]Last edited by Growler; 07-11-2010 at 04:35 AM.
-
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:
Much luck!Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
-
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.
- 07-11-2010, 10:03 AM #6
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
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.
- 07-11-2010, 02:47 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
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
-
Snake game in java
By freaky in forum New To JavaReplies: 5Last Post: 04-20-2010, 06:34 PM -
Java Applet Help - Making "Snake" Game
By Alphimeda in forum Java AppletsReplies: 15Last Post: 04-04-2010, 05:39 PM -
Snake Game
By mustachMan in forum New To JavaReplies: 2Last Post: 12-10-2009, 10:35 PM -
Snake game movement
By BeerMonkey in forum New To JavaReplies: 9Last Post: 11-27-2008, 12:48 PM -
An Applet Game
By T3000 in forum New To JavaReplies: 1Last Post: 11-18-2008, 04:18 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks