Hi Java Masters. Right now i am trying to make snake game . But i encounter problem and stuck in the middle of the process. I tried to look it again and again but cannot find where is my mistake .My questions is;
1. Why is my snake is not moving by Timer .
2. Why the headpart is YELLOW , eventhough i set it as GREEN at first.
There needs to be some valid numbers in the x and y arrays for where you want the snake to be located when the program starts. See how the draw method uses the contents of x and y to draw the snake.
To do that you could write an initSnake method to set the initial location for the snake. Look at the apple method for a hint.
The x and y arrays don't need to be larger than the length of the snake if all they hold is the location of the snake.
For example: The move() method is called every time the Timer fires an event. So as far as I can see, all it does is to set all of the values in the "x" and "y" arrays to the value of the last entry in those arrays. Could you explain what the purpose of that method was supposed to be?
Sorry for the late reply .Finally i managed to maked the snake moved by timer. I just add new variables [int up, down ,left, right] to move the x x and y coordinate of the head part. Like below(i add it in move method)
1. In the constructor:
up = -2;
down = 2;
left = -2;
right = 2;
And the for the second problem(headpart is yellow) i already solved the riddle, thanks to you guys. i figured that the headpart is actually overlapped with the body part, that is why the initial colour is yellow.
now i tried to move the snake by KeyListener but i failed a few time
even if i refer the youtube link(change the code a bit) like bellow, but nothing happen.
Please tell me what part in the code i need to change/add, because i tried for another routes but still nothing happen.
Thank for the reply guys. Now i get it . Based on the comments, i tried to apply it on the existed code and its WORKED.. Thanks a lot
in the constructor
in the move method()
in the keyListener method
And i discovered i can make it move with char / boolean ...Thanks a lot
And one last questions, why is it every time i compile the code , there will be a red rectangle in the most top left of the background? and it
will disappear in 2-3 seconds after i compiled it. I set red rectangle as the snake body part but why is it only this one disappear and placed over there.
I tried to look for the problem in the paint component method and altered the code a bit , but nothing happen.
#Kindly try to complile my old code in the above to see the problem
check for the red rectangle on the top left.
I dont know why it only appear for 1 second and then vanish.
Where in the code is anything drawn with the color red? What are the values of x and y when that code is executed?
If the values of x and y are 0, then the small rectangle will be drawn in the upper left corner.
That code changes the initial location of the snake's body from 0,0 to 400, 400. As the code executes the snake's body will come out of 400,400 like it was coming out of a hole at that location in the board.
Is the problem with the String that is being displayed not going where you want it to on the screen?
Measuring The size of the String requires knowing what font is being used so the number of pixels required can be determined. There are methods to do that.