Hi there, I'm trying to make a fly swatter mini game but I can't get it to add more flies after you swat the first one.

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Hi there, I'm trying to make a fly swatter mini game but I can't get it to add more flies after you swat the first one.

Post by answerhappygod »

Hi there, I'm trying to make a fly swatter mini game but I can't
get it to add more flies after you swat the first one. The score
should also update every time you swat a fly. The fly should also
move around but I can't figure out how to get it working. Any help
would be appreciated and it needs to be able to run in processing 4
using java. The first image is what it should looks like.
Here is my current code:
PImage fly,flybye,swatter,swatted;
float[] fX,fY; // fly locations array
float[] swat; // fly swatted binary boolean array, 1 =
swatted, 0 = not swatted
int score=0; // increments when swatted.
void setup(){
size(800,400);
fX=new float[0];
fY=new float[0];
swat=new float[0];
// load images
fly = loadImage("fly.png");
fly.resize(50,50);
flybye = loadImage("flybye.png");
flybye.resize(50,50);
swatter = loadImage("swatter.png");
swatted = loadImage("swatted.png");
fX =append(fX, random(50,750)); //first fly - random
location
fY =append(fY, random(25,375));
swat =append(swat,0); // used as a boolean and matches to
each individual fly, 0 = fly not swatted, 1 = swatted.
}
void populate(){ // draw the flies in memory to the
screen.
for(int i=0;i<fX.length;i++){
if(swat==1){ // if swatted
// resize the fly image and place based on
fx/fy array values
image(flybye, fX[0], fY[0]);
} else { // not swatted
image(fly,fX[0],fY[0]);
}
}
}
void collisionDetect(){
int newFlys = 0;
for(int i=0; i<swat.length;i++){ // bounding box
detection
if ( swat[0] == 1 )
continue;
if(mouseX-37 > fX[0]-40 &&
mouseX-37 < fX[0]+40 && mouseY-30 > fY[0]-40
&& mouseY-30 < fY[0]+40){
swat[0] = 1; //
swatted
newFlys ++;
image(flybye, fX[0],
fY[0]);
score=0;
score ++; //increment
score
}
}
for (int i=0; i < newFlys; ++i){
fX = append(fX, random(50,750)); //new
fly placed in random location when old fly dies.
fY = append(fY, random(50,350));
swat =append(swat,0); // new fly not
swatted
}
}
void draw(){
background(255);
noCursor(); //added to hide cursor for a more fluid
look
populate(); // draw flys to screen.
fill(0);
// set a text size and location for the score.
textSize(22);
text("total score: ",score, 250, 0);
if(mousePressed){ // image swap
collisionDetect();
image(swatted, mouseX-37, mouseY-30); //draw
swatter image to around mouse locaiton - might want to play with
this to get it to look right.
}else{
image(swatter, mouseX-37, mouseY-30); // if not
pressed then alternative image.
}
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply