• Post Reply Bookmark Topic Watch Topic
  • New Topic

Snake Two Player Problem  RSS feed

 
Jairo Navarrete
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys long time since I've visited this great site been following this java tutorial on making the snake game, and as I followed along at added the second player. Right up to the point I got into the resetting if I hit borders or myself or the other player, the snakes stopped moving when i tried hitting keys

I would be grateful for any help thanks

problem started up to the point where I added snake2 in the GenerateDefaultSnake method









 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not perform any initialization in the paint method - that stuff belongs in the SnakeCanvas constructor (or in the init method for code in the applet class). AWT will call the paint method whenever it feels like - definitely not what you want.

More generally, I advise strongly to move away from AWT and to use Swing instead. You should do all painting in the paint (for AWT) or paintComponent (for Swing) methods, and then call repaint whenever indicated. Saving a reference to the Graphics object is also not good practice (and may be part of the problem, I'm not sure).
 
Campbell Ritchie
Sheriff
Posts: 55366
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not use ints for the directions. Use an enum. You can use enum elements after switch-case.
I suggest you use fallthrough in that switch to decide which direction to use. You have UP and N both pointing to NORTH, so put the two cases together and add a comment //fallthrough intended.
 
Rob Camick
Ranch Hand
Posts: 2788
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The painting method is for painting. It is NOT for:

1. setting the size of the applet
2. starting a Thread
3. creating new LinkedList objects every time (All you need to do is clear the existing LinkedList).
4. adding a KeyListener.

I see the whole point of you code is to try to bypass the normal painting logic by invoking Move() and Draw(). This make work for a while but you need to remember that if the system ever determines the applet needs to repaint itself the paint(.,.) method is automatically invoked, which will reset your entire game back to its original state. See Painting in AWT and Swing for more complete information on how painting works.

Your code will also not work very well for a two player game since it will only react to the last key pressed. As has already been mentions you should spend time learning Swing and now AWT. When using Swing you would use Key Bindings instead of a KeyListener since they are more flexible. See Motion Using the Keyboard for more information and a complete working example.

You should be following Java naming conventions. Method name should NOT start with an upper case character. If the tutorial you are following uses upper case names then it it another reason not to follow that tutorial. If it doesn't, then the question if why are you making up you own conventions?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!