Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Why is the snake not moving by TIMER

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

My source:
https://www.youtube.com/watch?v=bI6e6qjJ8JQ&t=1605s

Thank you in advance.


/////////////////////////////////////////////////////////////////////
Frame class





Panel class



 
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are the x and y arrays used for?  They have lots of 0s in them.

Can you explain how the code sets the snakes initial position?
 
don krieg
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the x, y arrays are use to restore  x, y coordinate of the snake.

Actually i dont know how the snake sets its initial position, as i only follow the youtube(above link). Eventhough i follow the flow and changed a bit of the code still stuck in the middle.
 
don krieg
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i dont even know what missed
 
Norm Radder
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 26798
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
don krieg
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;


2. In the move method just add the below code

       if(up == -2) {
y[0] = y[0] - unit;
}
else if(down == 2) {
y[0] = y[0] + unit;
}
else if(right == 2) {
x[0] = x[0] + unit;
}
else if(left == -2) {
x[0] = x[0] - unit;
}


//////////////////////////////////////////

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.



    if(e.getKeyCode() == KeyEvent.VK_UP) {
  if(up != down) {
  up = -2;
  }
}
if(e.getKeyCode() == KeyEvent.VK_DOWN) {
  if(down != up) {
  down = 2;
  }
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
  if(right != left) {
  right = 2;
  }
}
if(e.getKeyCode() == KeyEvent.VK_LEFT) {
  if(left != right) {
  left = -2;
  }
}
 
Norm Radder
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the full text of the code so we can compile and test with it.
 
don krieg
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry my bad. The frame class is same as as above.
The new panel class is below.




 
Bartender
Posts: 4633
182
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is in this piece of code (in the move() method)

Since up is always -2, the snake always goes north, no matter how often you press one of the arrow keys.
 
don krieg
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the reply. Correct me if im wrong . In order to prevent that i need to delete these from the constructor right?





But in order to move the snake by Timer , i need to put only one of the above value in the constructor right?

And how about the KeyPressed part. I keep try and error countless time but nothing happen.
Please teach me what is wrong with the below part.






 
Piet Souris
Bartender
Posts: 4633
182
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

don krieg wrote:But in order to move the snake by Timer , i need to put only one of the above value in the constructor right?


Yes, but that one is a special one: an enum! They are ideal for situations like this. It is bedtime in Holland however, but no doubt someone will step in and tell you more about it.
 
Norm Radder
Master Rancher
Posts: 4465
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The values of 2 and -2 in the 4 direction variables is confusing.  Why are those variables assigned numeric values?  Can they be boolean variables that are only true or false?

How show the results of a key pressed set the variables?  You need to get that to work first.  Add some print statements to show what the variables' values are in  the keyPressed method and in move

Look at what Piet suggested using an enum.  Since the snake only travels in one direction at a time, one of those 4 values can be kept in a single variable.  enum fits that requirement.
 
Piet Souris
Bartender
Posts: 4633
182
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed.

Here is an example of an enum:



Now, suppose that you have a field 'direction' of type Direction, initialize with:
direction = Direction.UP;

You can then determine your x[0] and y[0] as follows:

How would you keep the variable 'direction' in sync with the key presses?
 
don krieg
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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  

 
Norm Radder
Master Rancher
Posts: 4465
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now that it is working it needs to be fixed so it is readable.  All the hardcoded values: 1,2,3,4 have no meaning and are easily misused when coding.  Instead of hardcoding the numbers, use constants:

Note: direction would be a better variable name then vel
 
don krieg
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh i get it. So in order to use my older code ,  i just need to add an enumvariable to control all the(up/down/right/left var). Thank you.

How about my second questions for the red rectangle that appear in the top left background. Do you have any idea about it ?


I already fixed it as below. Kindly please check for the red rectangle on the top left.
I dont know why it only appear for 1 second and then vanish. / Kindly refer to my last comment on the below part

 
Norm Radder
Master Rancher
Posts: 4465
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
don krieg
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so in the code there is something that set x,y coordinate to (0, 0) right.
I tried to added below code and succesfully erased the red rectangle.
Correct me if my method is wrong.


In the constructor

 
Norm Radder
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
don krieg
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the late reply. Yes i already noticed it and replace it with the code below. And i already finished with the entire code. Thanks



But one more problem. why is it the background  y-axis string / ... g.drawString(i * unit, 0,  i * unit , screenHeight) /
in the most right part do not fit perfectly with the length of the backgroud?
 
Norm Radder
Master Rancher
Posts: 4465
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic