New answers tagged slick
I've also developed snake, and ran into the same problem. I run snake at 30fps, yet only 'update' every 10 frames, in between the player still has the freedom to change direction. You're trying to prevent a player from going back where he came from in the input handling. Say you were moving up, your input handling will prevent the player from moving down, ...
Your issues does not seems to be "How do I accept only one directional input per update?" but rather "How do I prevent my snake to go backward?" From the many comments and discussion, you seem to be having a frame-rate that is not appropriate for the game you're developing. Traditionally, Snake is a game with a very low frame-rate, the snake moved only 2-3 ...
Firstly, you need to use a switch case statement and decide which direction overrides the others, for example, if they press all four buttons, which button should be listened to for input? This gets put in order within the switch statements. Edit: For clarity about the above statement. You do need to use a switch case statement (or similar structural logic) ...
My suggestion is to used TiledMap to do so: https://bitbucket.org/kevglass/slick/src/aa6e7b6dce00d9725ef7e4c4fb6a99107f87c218/trunk/Slick/src/org/newdawn/slick/tiled/TiledMap.java?at=development&fileviewer=file-view-default You can also see how tiledMap store and render tiles!
Jumping from 400 to 40 fps means something is really going wrong. image.getSubImage(xCrop, yCrop) I don't actually know much of slick, but I think that this loads the image to the graphics card. Try batching this (by making an instance variable in your class). I hope it helps!
Top 50 recent answers are included