The Snake Game help!
in
Programming Questions
•
18 days ago
Hello there! I'm very new to programming and I'm struggling with creating the Snake Game for class and I can't figure out what exactly is the problem. So far the snake would hit the food and another block would appear but when it does so it only appears in one direction and doesn't follow the path. I would appreciate help since I'm stumped.
// snake variablesint snakesize=1; //size of the snakeint MAXLENGTH=10;int headx[]=new int[MAXLENGTH]; //xlocationint heady[]=new int[MAXLENGTH]; //ylocationint i;
//FOOD VARIABLESint yumx=(round(random(49))+1)*10;int yumy=(round(random(49))+1)*10;
//SPEEDint speed;int xspeed,yspeed;
void setup (){size (500,500);headx[0] = 250;heady[0] = 250;// SNAKE HEAD BEGINS UNMOVING.speed=1;xspeed=0;yspeed=0;}
void draw (){background (0);fill(255,0,0);ellipseMode(CENTER);ellipse(yumx,yumy,15,15);for(int i =0; i < snakesize; i++){//tailfill(200, 20, 200);rectMode(CENTER);rect(headx[i],heady[i],20,20);
headx[i]=headx[i]+xspeed;heady[i]=heady[i]+yspeed;}// FOODif(snakesize<headx.length){if (headx[0]>=yumx-20 && headx[0]<=yumx+20 && heady[0]>=yumy-20 && heady[0]<yumy+20){yumx=(round(random(49))+1)*10;yumy=(round(random(49))+1)*10;snakesize++;if (i!=1){headx[snakesize -1] = headx[snakesize - 2] - 0;heady[snakesize -1] = heady[snakesize - 2] - 20;}}}}
void keyPressed(){// ARROW KEYS OR LETTER KEYS PRESSED.// RIGHT & LEFT HAND CONTROLS.if(keyCode==UP || key == 'w'){xspeed=0;yspeed=-2;}else if(keyCode==DOWN || key =='s'){xspeed=0;yspeed=2;}
else if(keyCode==LEFT || key=='a'){xspeed=-2;yspeed=0;}else if(keyCode==RIGHT || key =='d'){xspeed=2;yspeed=0;}}
1