We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello! So I am creating a game similar to a snake game but with one individual shape moving instead of getting bigger. The issue with my code that I'm having is that when the icon hits the wall I can't figure out how to get it to be a full on Game Over where you have to press a button to restart the game. Below I have posted my code so any help that can be given is greatly appreciated!!
Logo l;
int grid = 35;
PImage BH;
PImage SC;
PImage Rink;
PVector Cup;
void setup() {
size(800, 422);
l = new Logo();
frameRate(8);
pickLocation();
BH = loadImage("BH.png");
SC = loadImage("SC.png");
Rink = loadImage("Rink.jpg");
}
void pickLocation() {
int across = width/grid;
int down = height/grid;
Cup = new PVector(floor(random(across)), floor(random(down)));
Cup.mult(grid);
}
void draw() {
background(Rink);
if (l.capture(Cup)) {
pickLocation();
}
l.death();
l.update();
l.show();
image(SC,Cup.x, Cup.y, grid, grid);
}
void keyPressed() {
if (keyCode == UP) {
l.dir(0, -1);
} else if (keyCode == DOWN) {
l.dir(0, 1);
} else if (keyCode == RIGHT) {
l.dir(1, 0);
} else if (keyCode == LEFT) {
l.dir(-1, 0);
}
}
Logo.pde
class Logo {
float x = 0;
float y = 0;
float xspeed = 1;
float yspeed = 0;
int total = 0;
ArrayList<PVector> tail = new ArrayList<PVector>();
Logo() {
}
boolean capture(PVector pos) {
float d = dist(x, y, pos.x, pos.y);
if (d < 1) {
return true;
} else {
return false;
}
}
void dir(float x, float y) {
xspeed = x;
yspeed = y;
}
void death() {
for (int i = 0; i < tail.size(); i++) {
PVector pos = tail.get(i);
float d = dist(x, y, pos.x, pos.y);
if (d < 1) {
println("starting over");
total = 0;
tail.clear();
}
}
}
void update() {
if (total > 0) {
if (total == tail.size() && !tail.isEmpty()) {
tail.remove(0);
}
tail.add(new PVector(x, y));
}
x = x + xspeed*grid;
y = y + yspeed*grid;
x = constrain(x, 0, width-grid);
y = constrain(y, 0, height-grid);
}
void show() {
fill(255);
for (PVector v : tail) {
rect(v.x, v.y, grid, grid);
}
image(BH, x, y, grid, grid);
}
}
Answers
Please note that you created an object Logo and assigned to the letter >> l <<. That is not a good idea as l could be confused with a 1. Plus your code will be hard to follow and to understand. Clarity is more important than size. Important!
It is not possible to run your code since the images are not available :-??
Does your code work and does it start over when a death event happens?
One suggestion is to have a message box. Check the code in this link, which I will post below next: https://forum.processing.org/two/discussion/comment/75357/#Comment_75357
Another way is to black out your screen and print in the middle "game Over" and the screen will be presented for a short period of time, say 3 seconds before it restarts. For that you will need to have a boolean value in draw() checking continuously if to show the "Game Over" screen. Then when a death event happens, you activate the boolean variable. An example will explain this concept better:
I hope this helps,
Kf