Hello good afternoon I wanted to ask for help on a game I’m developing
It’s a snake game but the enemy isn’t working it was supposed to touch the enemy and the game is over. And the game does not open in openprocessing.
//game//
PImage amarelo;
PImage plastico;
PImage caixa;
PImage rug;
int grid = 80; //tamanho da grelha
PVector food;
PVector enemy;
int speed = 12;
boolean dead = true;
int highscore = 0;
Snake snake;
void setup() {
plastico = loadImage("plastico2.png");
amarelo = loadImage("amarelo1.png");
caixa = loadImage("caixa3.png");
rug = loadImage("Background.png");
size(960, 960);
snake = new Snake();
food = new PVector();
enemy =new PVector();
newFood();
newEnemy();
//frameRate(8);
}
void draw() {
background(rug);
fill(255);
// if (!dead) {
//
if (frameCount % speed == 0) {
snake.update();
}
snake.show();
snake.eat();
//fill(245, 242, 195);
image(plastico, food.x, food.y, grid, grid);
image(caixa, enemy.x, enemy.y, grid, grid);
// image(fish, food.x, food.y, grid, grid);
// image(dog, enemy.x, enemy.y, grid, grid);
textAlign(LEFT);
textSize(15);
fill(255);
text("Score: " + snake.len, 10, 20);
//} else {
// textSize(25);
//textAlign(CENTER, CENTER);
text("Snake Game\nEnter to start" + "\nHighscore: " + highscore, width/2, height/2);
}
//}
void newFood() {
food.x = floor(random(width));
food.y = floor(random(height));
food.x = floor(food.x/grid) * grid;
food.y = floor(food.y/grid) * grid;
}
void newEnemy() {
enemy.x = floor(random(width));
enemy.y = floor(random(height));
enemy.x = floor(enemy.x/grid) * grid;
enemy.y = floor(enemy.y/grid) * grid;
}
//snake//
class Snake {
PVector pos;
PVector vel;
ArrayList<PVector> hist;
int len;
int moveX = 0;
int moveY = 0;
Snake() {
pos = new PVector(0, 0);
vel = new PVector();
hist = new ArrayList<PVector>();
len = 0;
}
void update() {
hist.add(pos.copy());
pos.x += vel.x*grid;
pos.y += vel.y*grid;
moveX = int(vel.x);
moveY = int(vel.y);
pos.x = (pos.x + width) % width;
pos.y = (pos.y + height) % height;
if (hist.size() > len) {
hist.remove(0);
}
for (PVector p : hist) {
if (p.x == pos.x && p.y == pos.y) {
dead = true;
// ups.play();
if (len > highscore) highscore = len;
}
}
if (pos.x == enemy.x && pos.y == enemy.y) {
dead = true;
// ups.play();
if (len > highscore) highscore = len;
}
}
void eat() {
if (pos.x == food.x && pos.y == food.y) {
len++;
// nhom.play();
if (speed > 5) speed--;
newFood();
newEnemy();
}
}
void show() {
image (amarelo, pos.x, pos.y, grid, grid);
for (PVector p : hist) {
image (amarelo, p.x, p.y, grid, grid);
// image (cat, pos.x, pos.y, grid, grid);
//for (PVector p : hist) {
//image (cat, p.x, p.y, grid, grid);
}
}
}
void keyPressed() {
if (keyCode == LEFT && snake.moveX != 1) {
snake.vel.x = -1;
snake.vel.y = 0;
} else if (keyCode == RIGHT && snake.moveX != -1) {
snake.vel.x = 1;
snake.vel.y = 0;
} else if (keyCode == UP && snake.moveY != 1) {
snake.vel.y = -1;
snake.vel.x = 0;
} else if (keyCode == DOWN && snake.moveY != -1) {
snake.vel.y = 1;
snake.vel.x = 0;
}
if (dead) {
if (keyCode == ENTER){
snake = new Snake();
newFood();
speed = 10;
dead = false;
}
}
}