Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Java Snake Game Problem

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • 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: 4612
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 4612
47
  • Mark post as helpful
  • send pies
  • 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
  • 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: 4612
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you might hit an issue with diagonals, if they're allowed (not the case with traditional snake, though).
 
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic