We are about to switch to a new forum software. Until then we have removed the registration on this forum.
i'm trying to recreate the old arcade snake game and i have ran into a bit of on issue, because i need to make the square "eat" the apple the little red circle, so that then the spuare hits the apple it disappears and reappears in another random spot in the canvas.
but what confuses me the most is that with this code i made it work, but now it does not and i can't figure out why. i hope you all want to help me.
here are the code
float SnakeheadX, SnakeheadY; int direction; float SnakeSpeed; int SnakeSize; int foodsize;
int AppleX=(round(random(47))+1)16; int AppleY=(round(random(47))+1)16;
color food = color(255,0,0);
void setup() {
size(800,800); frameRate(12);
SnakeheadX = 400; SnakeheadY = 400; SnakeSpeed=7; SnakeSize=19; foodsize=12;
direction=0; }
void draw() {
background (180); fill(60,60,180); rect( SnakeheadX,SnakeheadY,SnakeSize,SnakeSize);
strokeWeight(1); fill(food); ellipse(AppleX,AppleY,foodsize,foodsize);
movedirection(); outside(); eat(); }
void keyPressed() {
if (key == 'd') { direction = 1; } else if (key == 'a') { direction = 2; } else if (key == 'w') { direction = 3; } else if (key == 's') { direction = 4; } }
void movedirection() {
if (direction>0) { if (direction == 1) { SnakeheadX = SnakeheadX + SnakeSpeed; } else if (direction == 2) { SnakeheadX = SnakeheadX - SnakeSpeed; } else if (direction == 3) { SnakeheadY = SnakeheadY - SnakeSpeed; } else if (direction == 4) { SnakeheadY = SnakeheadY + SnakeSpeed; } } }
void outside() { if ((SnakeheadX < 0)||(SnakeheadX > 800)||(SnakeheadY < 0)||(SnakeheadY > 800)) { fill (0); rect (0,0,800,800);
fill (255); textSize (64); text ("GAMEOVER",220,400); } }
void eat() {
if (SnakeheadX + 1 == AppleX && SnakeheadY + 1 == AppleY) { AppleX = (round(random(47))+1)16; AppleY = (round(random(47))+1)16; }
if (SnakeheadX + 2 == AppleX && SnakeheadY + 2 == AppleY) { AppleX = (round(random(47))+1)16; AppleY = (round(random(47))+1)16; }
if (SnakeheadX + 3 == AppleX && SnakeheadY + 3 == AppleY) { AppleX = (round(random(47))+1)16; AppleY = (round(random(47))+1)16; }
if (SnakeheadX + 4 == AppleX && SnakeheadY + 4 == AppleY) { AppleX = (round(random(47))+1)16; AppleY = (round(random(47))+1)16; }
if (SnakeheadX + 5 == AppleX && SnakeheadY + 5 == AppleY) { AppleX = (round(random(47))+1)16; AppleY = (round(random(47))+1)16; }
if (SnakeheadX + 6 == AppleX && SnakeheadY + 6 == AppleY) { AppleX = (round(random(47))+1)16; AppleY = (round(random(47))+1)16; }
if (SnakeheadX + 7 == AppleX && SnakeheadY + 7 == AppleY) { AppleX = (round(random(47))+1)16; AppleY = (round(random(47))+1)16; }
if (SnakeheadX + 8 == AppleX && SnakeheadY + 8 == AppleY) { AppleX = (round(random(47))+1)16; AppleY = (round(random(47))+1)16; }
if (SnakeheadX + 9 == AppleX && SnakeheadY + 9 == AppleY) { AppleX = (round(random(47))+1)16; AppleY = (round(random(47))+1)16; }
if (SnakeheadX + 10 == AppleX && SnakeheadY + 10 == AppleY) { AppleX = (round(random(47))+1)16; AppleY = (round(random(47))+1)16; }
if (SnakeheadX + 11 == AppleX && SnakeheadY + 11 == AppleY) { AppleX = (round(random(47))+1)16; AppleY = (round(random(47))+1)16; }
if (SnakeheadX + 12 == AppleX && SnakeheadY + 12 == AppleY) { AppleX = (round(random(47))+1)16; AppleY = (round(random(47))+1)16; }
if (SnakeheadX + 13 == AppleX && SnakeheadY + 13 == AppleY) { AppleX = (round(random(47))+1)16; AppleY = (round(random(47))+1)16; }
if (SnakeheadX + 14 == AppleX && SnakeheadY + 14 == AppleY) { AppleX = (round(random(47))+1)16; AppleY = (round(random(47))+1)16; }
if (SnakeheadX + 15 == AppleX && SnakeheadY + 15 == AppleY) { AppleX = (round(random(47))+1)16; AppleY = (round(random(47))+1)16; }
if (SnakeheadX + 16 == AppleX && SnakeheadY + 16 == AppleY) { AppleX = (round(random(47))+1)16; AppleY = (round(random(47))+1)16; }
if (SnakeheadX + 17 == AppleX && SnakeheadY + 17 == AppleY) { AppleX = (round(random(47))+1)16; AppleY = (round(random(47))+1)16; }
if (SnakeheadX + 18 == AppleX && SnakeheadY + 18 == AppleY) { AppleX = (round(random(47))+1)16; AppleY = (round(random(47))+1)16; }
if (SnakeheadX + 19 == AppleX && SnakeheadY + 19 == AppleY) { AppleX = (round(random(47))+1)16; AppleY = (round(random(47))+1)16; } }
Answers
A few of points about using this forum
1) Do not create duplicate discussions. You posted this question twice so I deleted the other one.
2) Discussion titles like "Please help me" and "I am having trouble with my code" are useless. Virtually every post on here is someone asking for help so make the title more informative. I have modified the title.
3) Please edit your post above to format the code so that we can read it. To edit your post click on the little gear icon. Make sure you have a blank line just before and just after your code then use the mouse to select the entire code block. Click on the C button or press Ctrl + O , this will indent your code. Save the changes.
Once your code is formatted then you are likely to get some response.
He's not kidding about the formatting. This would have been a helpful post if I could stand looking at your code. Instead my eyes hurt now.
In eat you also have a lot of lines that are similar.
Shouldn’t it be enough that the snake head is on the apple? Why add other values...?
(You are adding the same to x and y which means you check a diagonal and not the entire neighborhood of the snake head)
Look at dist() in the reference