Simple problem, implement millis() to keep something on the screen longer
in
Programming Questions
•
2 years ago
I've been working on a simple game ad have come across a problem. After i shoot "character", the image of "character" dying just flashes on the screen for a fraction of a second. I would like to figure out a way for the dead character to be displayed for about 1 second.
/code/
PFont fontA;
PImage astroid;
PImage ship;
PImage GameOver;
PImage stars;
PImage explosion;
boolean shoot = false;
int randx()
{
return int(random(600));
}
int[] sphereXCoords = {
randx(), randx(), randx(), randx(), randx()
};
int[] sphereYCoords = {
0, 0, 0, 0, 0
};
void setup()
{
size(600, 620);
astroid = loadImage("darth_vader_and_stormtrooper_lego_minifigures_alarm_clocks_2.png");
ship = loadImage("x-wing.png");
GameOver= loadImage("Game_Over_Screen.png");
stars = loadImage ("stars.png");
explosion = loadImage ("Explosion.png");
}
void draw()
{
background(stars);
fill(color(0, 255, 0));
stroke(color(0, 0, 0));
triangle( mouseX-8, 580, mouseX+8, 580, mouseX, 565);
fill(color(0, 0, 255));
stroke(color(0, 0, 255));
if (shoot==true)
{
sphereKiller(mouseX);
shoot = false;
}
sphereDropper();
gameEnder();
}
void mousePressed()
{
shoot = true;
}
void sphereDropper()
{
stroke(255);
for (int i=0; i<5; i++)
{
/*
ellipse(sphereXCoords[i], sphereYCoords[i]++,
sphereDiameter, sphereDiameter);
*/
image(astroid, sphereXCoords[i], sphereYCoords[i]++, 30, 40);
}
}
void sphereKiller(int shotX)
{
boolean hit = false;
for (int i = 0; i < 5; i++)
{
if ((shotX >= (sphereXCoords[i]-30/2)) &&
(shotX <= (sphereXCoords[i]+30/2)))
{
hit = true;
line(mouseX, 565, mouseX, sphereYCoords[i]);
image(explosion, sphereXCoords[i], sphereYCoords[i],
80, 90);
sphereXCoords[i] = randx();
sphereYCoords[i] = 0;
}
}
if (hit == false)
{
line(mouseX, 565, mouseX, 0);
}
}
void gameEnder()
{
for (int i=0; i< 5; i++)
{
if (sphereYCoords[i]==600)
{
fill(color(255, 0, 0));
background(GameOver);
noLoop();
}
}
}
/code/
PFont fontA;
PImage astroid;
PImage ship;
PImage GameOver;
PImage stars;
PImage explosion;
boolean shoot = false;
int randx()
{
return int(random(600));
}
int[] sphereXCoords = {
randx(), randx(), randx(), randx(), randx()
};
int[] sphereYCoords = {
0, 0, 0, 0, 0
};
void setup()
{
size(600, 620);
astroid = loadImage("darth_vader_and_stormtrooper_lego_minifigures_alarm_clocks_2.png");
ship = loadImage("x-wing.png");
GameOver= loadImage("Game_Over_Screen.png");
stars = loadImage ("stars.png");
explosion = loadImage ("Explosion.png");
}
void draw()
{
background(stars);
fill(color(0, 255, 0));
stroke(color(0, 0, 0));
triangle( mouseX-8, 580, mouseX+8, 580, mouseX, 565);
fill(color(0, 0, 255));
stroke(color(0, 0, 255));
if (shoot==true)
{
sphereKiller(mouseX);
shoot = false;
}
sphereDropper();
gameEnder();
}
void mousePressed()
{
shoot = true;
}
void sphereDropper()
{
stroke(255);
for (int i=0; i<5; i++)
{
/*
ellipse(sphereXCoords[i], sphereYCoords[i]++,
sphereDiameter, sphereDiameter);
*/
image(astroid, sphereXCoords[i], sphereYCoords[i]++, 30, 40);
}
}
void sphereKiller(int shotX)
{
boolean hit = false;
for (int i = 0; i < 5; i++)
{
if ((shotX >= (sphereXCoords[i]-30/2)) &&
(shotX <= (sphereXCoords[i]+30/2)))
{
hit = true;
line(mouseX, 565, mouseX, sphereYCoords[i]);
image(explosion, sphereXCoords[i], sphereYCoords[i],
80, 90);
sphereXCoords[i] = randx();
sphereYCoords[i] = 0;
}
}
if (hit == false)
{
line(mouseX, 565, mouseX, 0);
}
}
void gameEnder()
{
for (int i=0; i< 5; i++)
{
if (sphereYCoords[i]==600)
{
fill(color(255, 0, 0));
background(GameOver);
noLoop();
}
}
}
1