I tried doing a snake game and tried to run my code to make the snake increase as it eats and also at the end it also gives me syntax error could someone help me out here
type or int growth = 1;
final int X_SPEED = 1;
final int Y_SPEED = 1;
final int SNAKE_SIZE=20;
final int SNAKE_LENGTH=3*growth;
int x = width/2;
int y =height/2;
float foodX = random(0, 400);
float foodY = random(0, 400);
int foodSize = 15;
boolean hit = false; //controls hit detection logic for when the snake hits the food
int totalHits = 0; //the game will start at 0 points and in increase by 1 with each food catpure
final int MAX_HITS =5;
boolean borderHit = false;
String restart = "RESTART";
String exit = "EXIT";
//final int SNAKE_LENGTH=3;
int endGameBoxSize = 100;
int endGameBoxX = 250;
int endGameBoxY = 150;
// ---------------------------------------------------------------------------
void setup() {
size(500, 500);
background(220);
y=250;
}
//ellipse(250,290,SNAKE_SIZE,SNAKE_SIZE);
void draw() {
background(220);
//if (x >= 500 || x <= 0) {
// background(0);
//}
//if (y >= 500 || y <= 0) {
// background(0);
//}
moveSnake();
checkIfKeyPressed();
drawTarget();
checkIfHitTarget();
checkIfBorderHit();
displayExitOptions();
}
void moveSnake() {
//int y2=0;
fill(#FFFFFF);
for (int iY=0; iY<SNAKE_LENGTH; iY++) {
if(key == 'w'){
ellipse(x, y-iY*SNAKE_SIZE, SNAKE_SIZE, SNAKE_SIZE);
}
else if(key == 's'){
ellipse(x, y+iY*SNAKE_SIZE, SNAKE_SIZE, SNAKE_SIZE);
}
for (int iX=0; iX<SNAKE_LENGTH; iX++) {
if(key == 'd'){
ellipse(x - iX*SNAKE_SIZE, y, SNAKE_SIZE, SNAKE_SIZE);
}
else if(key == 'a'){
ellipse(x + iX*SNAKE_SIZE, y, SNAKE_SIZE, SNAKE_SIZE);
}
//y2+=20;
}
}
}
void checkIfKeyPressed() {
if (key=='w') {
moveSnake();
y -= Y_SPEED;
}
else if (key=='s') {
moveSnake();
y += Y_SPEED;
}
else if (key=='a') {
moveSnake();
x -= X_SPEED;
}
else if (key=='d') {
moveSnake();
x += X_SPEED;
}
}
void drawTarget(){
fill(0);
rect(foodX,foodY, foodSize,foodSize);
}
void checkIfBorderHit(){
if (x<0+SNAKE_SIZE/2 || x>500-SNAKE_SIZE/2 || y<0+SNAKE_SIZE/2 || y>500-SNAKE_SIZE/2 )
{
borderHit = true;
}
}
void checkIfHitTarget(){
if (dist(x, y, foodX, foodY)<=foodSize-5)
{
foodX = random(0+foodSize/2, 500-foodSize/2);
foodY = random(0+foodSize/2, 500-foodSize/2);
hit = true;
}
if (hit)
{
totalHits += 1;
hit=!hit;
growth++;
}
}
void displayExitOptions(){ //this function runs at the end of the game
if (totalHits==MAX_HITS || borderHit){ //if the total hits match the maximum amount of hits allowed run this code
fill(#621515);
rect(endGameBoxX, endGameBoxY, endGameBoxSize, endGameBoxSize-50); //game over box “RESTART”
fill(0);
text(restart, 250, 150);
fill(#621515);
rect(endGameBoxX, endGameBoxY+200, endGameBoxSize, endGameBoxSize-50); //game over box "EXIT"
fill(0);
text(exit, 250, 350);
x = 250; //return snake to middle of canvas at game over
y = 250;
}
// this if condtion will determing if the mouse is pressed in the restart box and restart the
if ((mouseX >endGameBoxX-endGameBoxSize/2)&&(mouseX<endGameBoxX+endGameBoxSize/2)&&
(mouseY>endGameBoxY-endGameBoxSize/4)&&(mouseY<endGameBoxY+endGameBoxY/4)&&mousePressed)
}
paste code here
Honestly, your code is a mess. And also, not your code. There’s at least a few chunks of code in there that seem like they’ve just been copied from other versions of snake.
Apart from the fact your code is a mess noted by others, see:
What happens when you hit a target?
growth++, but growth is used just in:
final int SNAKE_LENGTH=3*growth;
This is, growth is used JUST ONCE (SNAKE_LENGTH is an immutable final int).
I’ve never programmed a snake game, but in the version included with MsDOS 5.0 / Windows 95, each snake component was an object.
I.E. there was an array of SNAKE_LENGHT components, each one with X and Y.
When the snake advanced, the last component was deleted (array pull) and all components were replaced by the next one. Obviously, as that was done in BASIC, a complex routine for updating the array was used. Java/ Processing simplifies that.
Please don’t start new discussions with the same topic. Just stay in the old discussion about Snake
Thank you!
Keep up the good work!
running version without improvements
final int X_SPEED = 1;
final int Y_SPEED = 1;
final int SNAKE_SIZE=20;
int SNAKE_LENGTH=3;
int x = width/2;
int y =height/2;
float foodX = random(0, 400);
float foodY = random(0, 400);
int foodSize = 15;
boolean hit = false; //controls hit detection logic for when the snake hits the food
int totalHits = 0; //the game will start at 0 points and in increase by 1 with each food catpure
final int MAX_HITS =5;
boolean borderHit = false;
String restart = "RESTART";
String exit = "EXIT";
int endGameBoxSize = 100;
int endGameBoxX = 250;
int endGameBoxY = 150;
// ---------------------------------------------------------------------------
void setup() {
size(500, 500);
background(220);
y=250;
}
void draw() {
background(220);
moveSnake();
checkIfKeyPressed();
drawTarget();
checkIfHitTarget();
checkIfBorderHit();
displayExitOptions();
}
// ---------------------------------------------------------------------------
void moveSnake() {
fill(#FFFFFF);
for (int iY=0; iY<SNAKE_LENGTH; iY++) {
ellipse(x, y-iY*SNAKE_SIZE, SNAKE_SIZE, SNAKE_SIZE);
}
}
void checkIfKeyPressed() {
if (key=='w') {
y -= Y_SPEED;
} else if (key=='s') {
y += Y_SPEED;
} else if (key=='a') {
x -= X_SPEED;
} else if (key=='d') {
x += X_SPEED;
}
}
void drawTarget() {
fill(0);
rect(foodX, foodY, foodSize, foodSize);
}
void checkIfBorderHit() {
if (x<0+SNAKE_SIZE/2 ||
x>500-SNAKE_SIZE/2 ||
y<0+SNAKE_SIZE/2 ||
y>500-SNAKE_SIZE/2 ) {
borderHit = true;
}
}
void checkIfHitTarget() {
if (dist(x, y, foodX, foodY)<=foodSize-5) {
foodX = random(0+foodSize/2, 500-foodSize/2);
foodY = random(0+foodSize/2, 500-foodSize/2);
hit = true;
}
if (hit) {
totalHits += 1;
hit = false;
SNAKE_LENGTH++;
}
}
void displayExitOptions() {
//this function runs at the end of the game
if (totalHits==MAX_HITS || borderHit) { //if the total hits match the maximum amount of hits allowed run this code
fill(#621515);
rect(endGameBoxX, endGameBoxY, endGameBoxSize, endGameBoxSize-50); //game over box “RESTART”
fill(0);
text(restart, 250, 150);
fill(#621515);
rect(endGameBoxX, endGameBoxY+200, endGameBoxSize, endGameBoxSize-50); //game over box "EXIT"
fill(0);
text(exit, 250, 350);
x = 250; //return snake to middle of canvas at game over
y = 250;
}
// this if condtion will determing if the mouse is pressed in the restart box and restart the
if ((mouseX >endGameBoxX-endGameBoxSize/2)&&(mouseX<endGameBoxX+endGameBoxSize/2)&&
(mouseY>endGameBoxY-endGameBoxSize/4)&&(mouseY<endGameBoxY+endGameBoxY/4)&&mousePressed) {
}
}//func
// end of Sketch