Why my enemy doesn't work?

Hello good afternoon I wanted to ask for help on a game I’m developing :smiley:

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;
     }
  }
}
1 Like

-a- your code would look much better if formatted using

</> code button

( and this way killed 2 “*” so it can not run )


-b- now i try to run it in
WIN 10 // processing IDE 3.5.3
and get a error

cannot convert from element type Object to PVector

in the class snake
you make a arraylist hist
but here you think it is a PVector

  for (PVector p : hist) {

try to repair that first and repair your code posting please

2 Likes

Hello sorry i’m a beginner in processimg
can you explain to me where I use “</> code button”

and what can I change about this “cannot convert from element type Object to PVector”

Sorry and thanks:slightly_smiling_face:

When you type a message on this forum, you have various options right above the field where you type your text. If you click the button called Preformatted text (which is shaped like </>), you get the following:

type or paste code here

Where it says type or paste code here, you can place your code from Processing. This makes sure that the text is displayed as code and not as readable text. It makes it easier for us to take a look at it (and copy it without any errors)

1 Like

@Tiemen
Thank you very much for your help i’ll be more focused next time

@kll Thank you very much for your help :slightly_smiling_face:

Here it is the code with what I managed to change!
but the enemy still dosen’t working :disappointed:

Thank you so much

//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);
  
    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 = new ArrayList<PVector>();
  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 (ArrayList 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 (ArrayList p : hist) {
     image (amarelo, p.x, p.y, grid, grid);
   // image (cat, pos.x, pos.y, grid, grid);
    //for (ArrayList 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;
     }
  }
}

here i get a ERROR

cannot convert from element type PVector to ArrayList

try

//    for (ArrayList p : hist) {
    for (PVector p : hist) {

2 times

Hello again
already changed but still not working and appeared a new error
:frowning_face:

//game//
PImage amarelo;
PImage plastico;
PImage caixa;
PImage rug;
//PImage cat;
//PImage fish;
//PImage dog;
//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");
 // fish = loadImage("food.png");
 // cat = loadImage("grumpy.png");
 // dog = loadImage("enemy.png");
 // rug = loadImage("Background.png");
 
  size(960, 960);
  snake = new Snake();
  food = new PVector();
  enemy =new PVector();
  newFood();
  newEnemy();
  //frameRate(8);
  
 // nhom = new SoundFile(this,"bite.wav");
  //nhom.play();
//  nhom.amp(0.3);
  
  //ups = new SoundFile(this,"sad.wav");
  //ups.amp(0.1);
  
}

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 = new ArrayList<PVector>();
  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 (ArrayList p : hist) {
    for (PVector p : hist) {
      for (ArrayList p : hist) {
    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 (ArrayList p : hist) {
    for (PVector p : hist) {
      for (ArrayList p : hist) {
    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;
     
     }
}

No need for the sad face emoji, you’re almost there. Kll’s suggestion works, following his advice will get your snake game to work. It seems like you got overenthusiastic however and added a bit too much for loops :slight_smile:

At two different locations you’re nesting for loops, while you only have to use the line of code that Kll suggested. So instead of 4x for loops, reduce it to a single for loop.

In addition the curly brackets got a bit messy. Remember that each time you use a { , it needs to be paired with a closing curly bracket at the right location. If you use the Processing IDE, the auto format function (under edit) will likely be helpful.

Good luck!

2 Likes

Hello again @Tiemen
Thanks a lot for the help :smiley:

I used the line of code that @Kll suggested
and the game open .

But the game elements don’t move and I can’t control the “snake”

//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);
 
   
    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 = new ArrayList<PVector>();
  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 (ArrayList p : hist) {
    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 (ArrayList p : hist) {
    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;
     }
  }
}
     }

the processing IDE 3.5.3 editor has good features:

  • [ctrl][t] format your code for better readable / and understand its logic structure

  • if you place the ( like by mouse ) cursor AFTER a { , looks like {|
    then there will be a indication where the corresponding closing } is by showing [}]

-a- please take the

void keyPressed() {

out of the class by moving the last }
up under the

show() function end

-b- might be that


    if (frameCount % speed == 0) {
      snake.update();
    }
    snake.show();
    snake.eat();

should be

    if (frameCount % speed == 0) {
      snake.update();
      snake.show();
      snake.eat();
    }

-c- but all that is very difficult as we can not play with the code,
as long you not provide the 4 picture files…
we are kind of blind.

2 Likes

Hello again @kll
Thanks a lot for the help :slightly_smiling_face:

I already did and changed the points you said
here are the images used
images

Thank you again :smiley:

//GAME//
PImage amarelo;
PImage plastico;
PImage caixa;
PImage rug;
//PImage cat;
//PImage fish;
//PImage dog;
//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");
  // fish = loadImage("food.png");
  // cat = loadImage("grumpy.png");
  // dog = loadImage("enemy.png");
  // rug = loadImage("Background.png");

  size(960, 960);
  snake = new Snake();
  food = new PVector();
  enemy =new PVector();
  newFood();
  newEnemy();
  //frameRate(8);

  // nhom = new SoundFile(this,"bite.wav");
  //nhom.play();
  //  nhom.amp(0.3);

  //ups = new SoundFile(this,"sad.wav");
  //ups.amp(0.1);
}

void draw() {
  background(rug);
  fill(255);
  // if (!dead) {

  //

  //if (frameCount % speed == 0) {
  //  snake.update();
  // }
  //snake.show();
  //snake.eat();


  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 = new ArrayList<PVector>();
  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 (ArrayList p : hist) {
    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 (ArrayList p : hist) {
    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;
     }
  }
}
     }

thanks, the image download worked.

that way of movement control actually is a visibility control, its blinking!


actually the enemy detect and set dead worked,
but you disabled the ENTER KEY by a missing ‘}’
and disabled the dead by //
here something what works littlebit better

// https://discourse.processing.org/t/why-my-enemy-doesnt-work/13494/12

//GAME//
PImage amarelo;
PImage plastico;
PImage caixa;
PImage rug;
//PImage cat;
//PImage fish;
//PImage dog;
//PImage rug;

int grid = 80; //tamanho da grelha
PVector food;
PVector enemy;
int speed = 10;
boolean dead = false;
int highscore = 0;
Snake snake;

void setup() {
  plastico = loadImage("plastico2.png");
  amarelo = loadImage("amarelo1.png");
  caixa = loadImage("caixa3.png");
  rug = loadImage("Background.png");
  // fish = loadImage("food.png");
  // cat = loadImage("grumpy.png");
  // dog = loadImage("enemy.png");
  // rug = loadImage("Background.png");

  size(960, 960);
  snake = new Snake();
  food = new PVector();
  enemy =new PVector();
  newFood();
  newEnemy();
  frameRate(30);

  // nhom = new SoundFile(this,"bite.wav");
  //nhom.play();
  //  nhom.amp(0.3);

  //ups = new SoundFile(this,"sad.wav");
  //ups.amp(0.1);
}

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 = new ArrayList<PVector>();
  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);
    }
  }
}



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 (key == ENTER || key == RETURN) {
      //println("enter");
      snake = new Snake();
      newFood();
      speed = 10;
      dead = false;
    }
  }
}

1 Like

Hello again @kll

Thank you very much :smiley:
but now what can I do to get the game started again after touching the enemy and losing ?
and the snake keeps flashing what can I do ?

The code is the same as the last one you sent me, I tried to change some things but it didn’t work, so it’s still the same

Thank you :slightly_smiling_face:

this makes the blinking.
sorry, need to take out the show after the update, after enable the !dead line,
try again you original?

    if (frameCount % speed == 0) {
      snake.update();
      snake.eat();
    }
    snake.show();

the restart question i not understand, as here that last code
“ENTER” ( for restart) works now,
( and the code is not same!)

1 Like

Thank you so much for your help and patience and understanding @kll @Tiemen :grinning::smile:

the game is finally working the way it wanted :grinning: :partying_face: :tada:

But I wanted to ask you one thing if you can help me

when placing the game on openprocessing.org with the images in files
when i open the game it gives the following error:

“Uncaught Error using image in background (): PImage not loaded.Show stack”

Here is the link where the game is

Thank you again :smiley:

ProcessingJS.org/reference/preload/

/* @pjs preload="panda.png,candy-icon.png"; */

hist.add(pos.copy());

ProcessingJS.org/reference/PVector_get_/

1 Like

Excuse me @GoToLoop
I did not understand! :neutral_face:

Have you clicked & read the 3 links from my previous reply? :link:
Can you be more specific about what exactly you haven’t understood? :face_with_thermometer:

yes :slight_smile:
but my code is JAVA and not P5js.

I don’t understand where I have to change the code
Thank you!