Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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...

Units problem.

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...

enter image description here

share|improve this question
2  
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. –  AttackingHobo May 8 '11 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! –  Jonathan Hobbs May 8 '11 at 22:01
add comment

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.

share|improve this answer
    
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. –  BigStuuu May 9 '11 at 15:35
    
You can edit your answer instead of making it a comment. –  Byte56 Apr 18 '12 at 16:36
add comment
up vote 0 down vote accepted

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;

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.