class MovingSprite { float x, y ; float xVel, yVel ; PImage theImage ; float msWidth, msHeight ; boolean isHidden ; // true, if currently hidden MovingSprite(String inImageName, float inX, float inY) { xVel = 1 ; yVel = 2 ; theImage = loadImage(inImageName) ; msWidth = theImage.width ; // the width of the MS object msHeight = theImage.height ; // the height of the MS object x = inX ; y = inY ; isHidden = false ; } // setters void setIsHidden(boolean inValue) { isHidden = inValue;} // getter float getX() { return (x) ;} float getY() { return (y) ; } boolean getIsHidden() { return isHidden ;} // return true if hidden boolean isVisible() { return !isHidden ;} void updateLocation() { x = x + xVel ; y = y + yVel ; if ( (x <=0) || ( (x+msWidth) >= width) ) xVel = xVel * -1 ; if ( (y <= 0) || ( (y+msHeight) >= height) ) yVel = yVel * -1 ; } void drawSprite() { if (!isHidden) image(theImage,x,y) ; } boolean containsPoint(float inPx, float inPy) // returns true if MS contains parameter point, else returns false { if ( (inPx > x) && (inPx < (x + msWidth) ) && (inPy > y) && (inPy < (y+msHeight) ) ) return (true) ; else return(false) ; } } //============================= MovingSprite ms1, ms2, ms3, ms4, ms5 ; int numNotHidden ; void setup() { size(200,200) ; numNotHidden = 4 ; ms1 = new MovingSprite("smiley1.png",0,0) ; ms2 = new MovingSprite("smiley1.png",50,50) ; ms3 = new MovingSprite("smiley1.png",100,0) ; ms4 = new MovingSprite("smiley1.png",0,100) ; ms5 = new MovingSprite("smiley1.png",random(width - 100),random(height -100)) ; // ms1.drawSprite() ; } void draw() { background(100) ; ms1.updateLocation() ; ms1.drawSprite() ; ms2.updateLocation() ; ms2.drawSprite() ; ms3.updateLocation() ; ms3.drawSprite() ; ms4.updateLocation() ; ms4.drawSprite() ; // ms5.updateLocation() ; // ms5.drawSprite() ; } void mousePressed() { if ( ms1.isVisible() && ms1.containsPoint(mouseX,mouseY) ) { ms1.setIsHidden(true) ; numNotHidden -= 1 ; // numNotHideen = numNotHidden - 1 ; } if (ms2.isVisible() && ms2.containsPoint(mouseX,mouseY) ) { ms2.setIsHidden(true) ; numNotHidden -= 1 ; } if (ms3.isVisible() && ms3.containsPoint(mouseX,mouseY) ) { ms3.setIsHidden(true) ; numNotHidden -=1 ; } if (ms4.isVisible() && ms4.containsPoint(mouseX,mouseY) ) { ms4.setIsHidden(true) ; numNotHidden -= 1 ; } println(numNotHidden) ; if (numNotHidden == 0) println("game over") ; }