# Problems with Creating Snake's Clone on Java

I'm creating Snake's clone. My app have too classes: Snake.java and Board.java.

Right now I have three problems:

1) There are two constants - BOARDS_WIDTH, BOARDS_HEIGHT. They are meant to be dimensions for playground where snake moves and collects apples. It's dynamic and I can set them as I want. For example 640 x 480, 160 x 160 or whatever. Only thing I need to keep in my mind is that both numbers must divide with 16. Why? Because constant UNIT is set to 16 x 16 and, for example, when board's dimensions are set to 160 x 160, it means that there would be 10 x 10 units large playground (160 / 16 = 10). Problem: playground is always two pixels (not units) larger than it meant to be. Like if I have set dimensions to 160 x 160, actual dimensions will be 162 x 162! Here are screenshot...

If you measure this image... after pill (apple, if you like) there are two extra pixels.

2) Collisions. I think - it's because problem #1. You see, there are code lines...

if( x[0] < 0 || x[0] > BOARDS_WIDTH || y[0] < 0 || y[0] > BOARDS_HEIGHT ) {

inGame = false;

}


They should prevent snake from going into walls. Actual result is that snake goes few units in the wall - then only the game stops... i want it to stop exactly when snake collide with the wall!

3) How to make snake start to move only when users clicks any of arrows? Right now it's like snake starts to move her body, but her head is stopped. After joints * DELAY snake's all joints are under her head! Ha, ha...

• You did not give enough information to help you with the first problem. You set something to be 160 and its 162, with no code I have no idea what you are doing wrong. Also you have 3 completely different problems. Split them up, because the second question is easy to answer, but I don't know the rest, so I cannot really submit an answer. Commented May 8, 2011 at 19:45
• @AttackingHobo Looks like there's two source links in the first line. The Board's just a JPanel. Still this should definitely be split up! Commented May 8, 2011 at 22:01

## 2 Answers

The reason that your getting an extra 2 pixels added on is that these pixels are added in to form the frame around your game. (Frame width is 1 pixel, add the frame on both sides and there's your extra 2)

I know this because I had a similar problem myself a while back.

edit

Just thought I'd add on to my answer, if you want a window with a width of say.. 160 you would just deduct 2 pixels from this value. Therefore a width specified at 158 pixels would give you your desired 160 width. If however you simply want the gameplay area to be 160 pixels wide then what you have at present is fine.

• Just thought I'd add on to my answer, if you want a window with a width of say.. 160 you would just deduct 2 pixels from this value. Therefore a width specified at 158 pixels would give you your desired 160 width. If however you simply want the gameplay area to be 160 pixels wide then what you have at present is fine. Commented May 9, 2011 at 15:35
• You can edit your answer instead of making it a comment. Commented Apr 18, 2012 at 16:36

1) You need to add setResizable() before pack() method,

2) You need to implement so to say "collision prediction" to reach desired effect,

3) You need to start timer only after any keyEvent is happened;