We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm recreating the old game snake but I have no clue how to have it so that the snake will move on its own (except when you change it's direction obviously). I only need help with the movement, other than that i only have the basic code.
Answers
sorry, forgot to upload what i have. here's the code
Please edit your post (gear icon in the top right corner of your post), select your code and hit ctrl+o to format it. Make sure there is an empty line above and below your code.
Kf
Do you mean an AI -- so that the computer can play snake and try to live as long as possible?
no, I'm fairly new to this coding language so my code may seem a bit crude. But i want it so that the snake will keep moving in the same direction until you press any of the arrow keys to make it move directions
It's just a case of remembering the last key that was pressed and applying that until it changes.
Add a direction variable, change that in your keyPressed method. Move the rest of the moving code, modified to use direction rather than key, to draw so that it's executed every frame. (This will possibly be way too fast).
Think of better names.
Oh hey! I made a snake game a while back and ran into all the obstacles you did. I'll answer any questions you have :) Here's all my code if you need help. Just note I did mine in an OOP sense, meaning I made 'objects' which are their own little identities that I can spawn up whenever I want instead of typing everything in the draw and setup method.
I don't mean to sound too harsh, but from what I'm seeing from your current methods, I believe snake might need to wait until you learn a few more concepts. Not to fret! The Processing tutorial section has many well written pages to get you on your way to the god of all snakes ... inside your program of course. I'll help introduce you to these concepts after the initial example.
Because the way I structured it is wayyy different than how you've done it, I'll just make a separate example that is more of what you're doing :
Hopefully the comments can explain most of it. The move(); line inside the draw will do all of the code inside of the move() { } method. It's a bit buggy, not too interested to figure out why, but hopefully it will lead you on the right path :)
The main problem with your code is putting all of that code within the keyPressed method. When you do that, that code will only activate whenever you press the key down. I basically did the whole move thing inside the draw method, meaning every loop it's going to move whatever direction we currently have it set to.
So you may be asking from my very mean comment earlier (sorry :( I tried to word it nicely since I hope everyone learns to become programming wizards, I hope one day to be also) what else do you need to learn, and how do I know you need to learn it?
Well here's a few things : Objects - This will teach you how to make a body square. This way you don't have to keep writing out XX1 and XX2 and make a new variable for every snake and every food position yourself. It would become incredibly taxing to write after a while. Arrays - These store a bunch of variables into one! (Not quite like the PVector I showed earlier, since that stored one x and y while arrays can store a near limitless amount of the same variable) This way you can add a new body when you eat a food sprite, and is basically essential to make your snake bodies move to where each body takes the previous position of the one in front of it. I'm not sure if this game would be possible without these useful things!
Daniel Shiffman made some awesome videos over arrays and objects! Very fun guy to watch and could be better if you like the video format instead.
thanks so much dino! but i've got down what i didn't have but my all it does is make my snake just move really fast in the direction that you chose, it will stop when you let go of one of the keys here's what i have now:
Ah :) It's because you put the {}'s around the
if (keyPressed)
around all the lines below itIf there is only one statement below an if statement you don't have to put the curly braces. People bash me for it because it can cause problems when bug fixing but I've never had trouble with it.
So you can either do
or
So the final draw method will look like this :
And that should fix it! Before I move onto the snake's speed I should note that if you select all your code and press Ctrl + T it will automatically indent all the code which will get rid of the nasty
line in your draw method :) Just a tip I just learned! Thought it might be useful. Anyways ... onto speed!
I also noticed that it goes really fast. There's two solutions I can think of. The first would be to change the framerate to
frameRate(30);
This will make it choppier, but will make the snake a lot slower (Also in your sketch you set frameRate to 60. Processing sets it to 60 by default c;)The other solution would be to set the snakeSpeed to 5 (half of what it is now) and keep the frameRate and 60. (Method 1 in one second would move forward 10 pixels 30 times, 30 * 10 = 300 pixels/sec | Method 2 in one second would move forward 5 pixels 60 times, 60 * 5 = 300 pixels/sec) Both of these methods will move the same speed per second, and this method will feel a lot lot smoother since the second method is 60 FPS instead of 30. The problem of course is that the snake doesn't move in the same grid format of its head, since now every move it's going only half of its head instead of a full length. A solution to this could be to only accept a change in direction every other frame (this way it will go two lengths every time in a given directon no matter if you press your key down to go somewhere else) Because of how fast the program is refreshing, the user can't really feel the input lag. So how could we make it so it only changes direction every half frame? My solution is to change the
if (keyPressed)
line to:(And also get rid of the frameRate line since we're going to run this bad boy at 60 fps :d)
Just in case you don't know, the % (modulus operator) is like division except the number it outputs is the remainder from the division. So like 7 % 3 is 1 because 7 / 3 is 2 with 1 left over. Also, frameCount counts how many times the draw loop has looped basically, so after 1 second it should be around 60 (it's really hard to get exactly 60) and after 2 seconds it will be around 120, meaning the draw loop will have executed all the code inside of it 120 times. So we take this frameCount and divide it by 2, and if the remainder doesn't equal 0 (aka if it's 1 aka if frameCount is odd) only then will we update the direction with the key we press down. Why != instead of ==? Well honestly I just played around with it and saw if it was set to == then the snake is unaligned with the food and stuff and when I set it to != I saw that it was aligned with the food.
I actually have no idea why != works. I would think the first frameCount would move forward 5, making it unalligned with the grid we kind of set up, so only on the second frame would it be alligned, thus acceptable to move, so the 4th, 6th, 8th ... would also be like this which means on even numbers which means % 2 == 0 but it didn't seem to work out like that :d If someone could explain why I'd be interested)
Now honestly ... we kinda got lucky with the numbers we chose to make this all work. If you wanted to make the speed like 2, then you could change the line to
frameCount % 5 == 0
Because then every 5th will accept input but then this could be noticable by the user if their key doesn't register at the right time so then it all gets messy.Once again thanks so much Dino. the new problem i have is that the heads y axis is a little off which causes the food to be a little higher than the head, this means that the the only way to collect the food is to get the x axis right and then move up or down on it. here's the code now