Question
PImage fly,flybye,swatter,swatted; float [] fX,fY; // fly locations array float [] swat; // fly swatted binary boolean array, 1 = swatted, 0 = not swatted
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 if(swat[i]==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(){ //collision detection - detect collision between swatter and fly for(int i=0; i if(mouseX-37 > fX[0]-40 && mouseX-37 < fX[0]+40 && mouseY-30 > fY[0]-40 && mouseY-30 < fY[0]+40){ // condition should look at location of mouse and individual coordinates in fX and fY swat[i] = 1; // swatted 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 score++; //increment score } } } 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. 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. println("Error"); }else{ image(swatter, mouseX-37, mouseY-30); // if not pressed then alternative image. } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started