Interactive images in an array. Need help adding a "hit" counter.
in
Programming Questions
•
5 months ago
I want to make the red dots in this program images. I want to make them a bomb image as they fall, and a explosion when hit with the laser. I can't figure it out, so I have the base in here with the array as ellipse. TIA
Also, what would be a simple way to add a hit counter?
PFont fontA;
float bombDiameter = 8;
boolean shoot = false;
PImage galaxy;
int hit = 0;
int miss = 0;
int randx()
{
return int(random(800));
}
int[] bombXCoords = { randx(), randx(), randx(), randx(), randx() }; // X cood. values for bomb array are random
int[] bombYCoords = { 0, 0, 0, 0, 0 }; // All bombs have a 0 y value to start.
void setup()
{
size(800,700);
galaxy = loadImage("galaxy.jpg");
}
void draw()
{
background(galaxy);
fill(color(0,255,0));
stroke(color(0,255,0));
ellipseMode(CENTER);
ellipse(400, 700, 30, 30);
fill(color(255,0,0));
stroke(color(255,0,0));
if(shoot==true)
{
Laser(mouseX);
shoot = false;
}
Bombs();
gameOver();
float bombDiameter = 8;
boolean shoot = false;
PImage galaxy;
int hit = 0;
int miss = 0;
int randx()
{
return int(random(800));
}
int[] bombXCoords = { randx(), randx(), randx(), randx(), randx() }; // X cood. values for bomb array are random
int[] bombYCoords = { 0, 0, 0, 0, 0 }; // All bombs have a 0 y value to start.
void setup()
{
size(800,700);
galaxy = loadImage("galaxy.jpg");
}
void draw()
{
background(galaxy);
fill(color(0,255,0));
stroke(color(0,255,0));
ellipseMode(CENTER);
ellipse(400, 700, 30, 30);
fill(color(255,0,0));
stroke(color(255,0,0));
if(shoot==true)
{
Laser(mouseX);
shoot = false;
}
Bombs();
gameOver();
}
void mousePressed() // value for mousepressed
{
shoot = true;
}
void Bombs()
{
stroke(255);
fill(255,0,0);
for (int i=0; i<5; i++)
{
ellipse(bombXCoords[i], bombYCoords[i]++,
bombDiameter, bombDiameter);
}
}
void Laser(int shotX) // creates laser
{
boolean strike = false;
for (int i = 0; i < 5; i++)
{
if((shotX >= (bombXCoords[i]-bombDiameter/2)) && // paramiters to define a hit
(shotX <= (bombXCoords[i]+bombDiameter/2)))
{
strike = true;
line(400, 670, mouseX, bombYCoords[i]);
ellipse(bombXCoords[i], bombYCoords[i],
bombDiameter+25, bombDiameter+25);
bombXCoords[i] = randx(); // Moves hit bomb to random x-coordinate
bombYCoords[i] = 0; // Restet hit bomb to 0
}
}
if(strike = true)
hit +=1;
else miss +=1;
text("Hit: " + hit, 700,650);
if(strike == false)
{
line(400, 670, mouseX, 0);
}
}
void gameOver()
{
for (int i=0; i< 5; i++)
{
if(bombYCoords[i]==800)
{
fill(color(255,0,0));
noLoop();
textSize(100);
text("GAME OVER", 100, 350);
}
}
}
void mousePressed() // value for mousepressed
{
shoot = true;
}
void Bombs()
{
stroke(255);
fill(255,0,0);
for (int i=0; i<5; i++)
{
ellipse(bombXCoords[i], bombYCoords[i]++,
bombDiameter, bombDiameter);
}
}
void Laser(int shotX) // creates laser
{
boolean strike = false;
for (int i = 0; i < 5; i++)
{
if((shotX >= (bombXCoords[i]-bombDiameter/2)) && // paramiters to define a hit
(shotX <= (bombXCoords[i]+bombDiameter/2)))
{
strike = true;
line(400, 670, mouseX, bombYCoords[i]);
ellipse(bombXCoords[i], bombYCoords[i],
bombDiameter+25, bombDiameter+25);
bombXCoords[i] = randx(); // Moves hit bomb to random x-coordinate
bombYCoords[i] = 0; // Restet hit bomb to 0
}
}
if(strike = true)
hit +=1;
else miss +=1;
text("Hit: " + hit, 700,650);
if(strike == false)
{
line(400, 670, mouseX, 0);
}
}
void gameOver()
{
for (int i=0; i< 5; i++)
{
if(bombYCoords[i]==800)
{
fill(color(255,0,0));
noLoop();
textSize(100);
text("GAME OVER", 100, 350);
}
}
}
1