• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Help With Snake Game

 
Greenhorn
Posts: 22
  • 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 get the rat to stop when its centre falls within 5 pixels of the head of the snake. The head of the snake is the first element in an array of 25 snake objects. There's five classes to this application altogether and I am providing the Snake, Rat and SnakeRatPanel classes to help you on this.

I think the problem is in the moveRat method of the SnakeRatPanel class, as it is here where I am controlling the movement of the rat. The condition
to stop the rat when its centre is within 5 pixels of the snake's head
doesn't do anything.

I appreciate your help on this one!
Below are the three classes: Rat, Snake and SnakeRatPanel:
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I modified it for my taste, and to get a better overview.


Your getX () getY () where without specified object, so 'this' was asumed.
There is no SnakeRatPanel.getX () but a JPanel.getX () and that was called.
I guess you wanted to call rat.getX ();

An additional feature is my real sophisticated debug-code
 
Amil Mehmedagic
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stefan!

This is precisely what I wanted to accomplish! And thankx for the debug code too!
 
I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic