• 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
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Java Snake Game Problem

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello , I am trying to make the classic snake game in Java and facing some issues , well I am able to do some of the coding but not able to understand some of it and not able to implement some of the logic. The problem that I am facing is that if the snake is going up and I press the down key then the snake move on itself and the games ends , same thing happens on side , it is moving right and I press the left arrow key the snake moves on itself and the game is over . So how and I stop the snake to move on itself . The code when the snake eats one of its body part is



And I am not able to understand the logic behind this piece of code which adds a new snake body part when the snake eats food



the complete program is this

Class with the main function



The JPanel Class



The Snake Class



The Food Class



Thank you and have a nice day : )
 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the changeSnakeDirection code just check if the new value is equal to the old value * -1.
If it is then don't change it.
That should prevent the reversing.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, only allow the value to change if it is currently zero, or is being changed to zero.

Has the same effect, but might look simpler.
 
siddharth sekhar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:In the changeSnakeDirection code just check if the new value is equal to the old value * -1.
If it is then don't change it.
That should prevent the reversing.



It worked , Thanks for your suggestion , you are very good  : )
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you might hit an issue with diagonals, if they're allowed (not the case with traditional snake, though).
 
Looky! I'm being abducted by space aliens! Me and this tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic