Snake Game help movement
in
Programming Questions
•
20 days ago
I'm trying to make a snake game for class and right now I'm just trying to figure out the movement and this is what I have so far. I know it's a mess and I know it is wrong because it just moves to the right constantly and only shifts up or down using arrow keys. So how would I fix that problem? Also our teacher told us to make a matrix for the snake to move in and to have the food appear in? How exactly would I do that? Oh and also he told us we need to do these things too that I am not really sure how to do so if someone could help me that would be awesome:
-
test for collision with food
-
add to length of snake
-
test for edges, if true, end game
-
boolean for end game
Thank you!! here is my attempt at the moment:
void setup() {
size(300,300);
smooth();
frameRate(30);
}
//Set variables
int dotSize = 10;
int speed = 1;
int snakeX = width/2;
int snakeY = height/2;
//Functions
void drawFood() {
noStroke();
fill(0);
rect(random(300),random(300),dotSize,dotSize);
}
void moveUp() {
snakeY = snakeY - speed;
}
void moveDown() {
snakeY = snakeY + speed;
}
void moveLeft() {
snakeX = snakeX - speed;
}
void moveRight() {
snakeX = snakeX + speed;
}
void drawSnake() {
noStroke();
fill(0);
rect(snakeX,snakeY,dotSize,dotSize);
}
//Game draw
void draw() {
background(255);
drawSnake();
snakeX = snakeX + speed;
}
void keyPressed() {
if (keyCode == 37) {
moveLeft();
}
else if (keyCode == 39) {
moveRight();
}
else if (keyCode == 38) {
moveUp();
}
else if (keyCode == 40) {
moveDown();
}
}
1