//254 yourguy guy = new yourguy(300,200); int CounterResetValue = 500; int counter; int score = 0; HashSet enemies = new HashSet(); float SCREENSIZE = 400; void setup(){ size(400,400); ellipseMode(CENTER_RADIUS); addBadGuy(); framerate(60); counter = CounterResetValue; textFont(loadFont("Arial-Black-14.vlw"),14); } void addBadGuy(){ float x,y; x = guy.x; y = guy.y; while(abs(guy.x - x ) < 50 ||abs(guy.y - y ) < 50){ if( int(random(2)) == 0){ if( int(random(2)) ==0){ x = 20; } else { x = SCREENSIZE - 20; } y = random(SCREENSIZE - 30)+ 30; } else { if( int(random(2)) ==0){ y = 20; } else { y = SCREENSIZE - 20; } x = random(SCREENSIZE - 30)+ 30; } } enemies.add( new badguy(x,y)); } float lastMouseX, lastMouseY; void mouseReleased(){ if(guy.lives <= 0){ enemies.clear(); guy.lives=3; CounterResetValue = 500; score = 0; counter = CounterResetValue; } } void draw(){ background(100); fill(0); if(guy.lives>0){ text("score:"+score,10,40); score++; } else { text("game over! score:"+score+" click to replay ",10,40); } if(guy.lives>0){ guy.move();} guy.bounds(); guy.draw(); float Xdiff = mouseX - lastMouseX; float Ydiff = mouseY - lastMouseY; float power = sqrt((Xdiff*Xdiff)+(Ydiff*Ydiff)); if(guy.mouseOn()){ guy.hit(Xdiff,Ydiff); } else { guy.unhit(); } lastMouseX = mouseX; lastMouseY = mouseY; Iterator i = enemies.iterator(); boolean enemyHit = false; while(i.hasNext()){ badguy enemy = (badguy) i.next(); if(enemy.hit(guy)){ enemyHit = true; } enemy.adjust(guy); if(guy.lives>0){ enemy.move();} enemy.bounds(); enemy.draw(); if(enemy.dead && enemy.isOffscreen){ i.remove(); } } if(enemyHit){ if(guy.nowHit == false){ guy.nowHit = true; guy.lives--; } } else { guy.nowHit = false; } if(guy.lives > 0){ counter--; } if(enemies.size() == 0){ counter = CounterResetValue; addBadGuy(); } if(counter <=0){ counter = CounterResetValue; addBadGuy(); if(CounterResetValue > 50){ CounterResetValue -= 5; } } } class yourguy{ public boolean nowHit; public int lives = 3; public float x,y,xs,ys; float size=10; float acc = .03; boolean ready; yourguy(float px, float py){ x = px; y = py; } boolean mouseOn(){ return (abs(mouseX-x) < size && abs(mouseY-y) < size); } void move(){ x += xs; y += ys; } void unhit(){ ready = true; } void hit(float dx, float dy){ if(ready){ xs = ((xs * 5 )+ (dx* 5))/ 10; ys = ((ys * 5 )+ (dy* 5))/ 10; ready = false; } } void bounds(){ if(x < size){ x = size; xs *= -.8; } if(x > SCREENSIZE - size){ x = SCREENSIZE - size; xs *= -.8; } if(y < size){ y = size; ys *= -.8; } if(y > SCREENSIZE - size){ y = SCREENSIZE - size; ys *= -.8; } } void draw(){ fill(255); //white circles ellipse(x,y,size,size); fill(0); text(lives,x,y); if(nowHit){ text("OUCH!",x+10,y-10); } } } class badguy{ public boolean dead = false; public boolean isOffscreen; public float x,y,xs,ys; float size=10; float acc = .03; boolean ready; badguy(float px, float py){ x = px; y = py; } public boolean hit(yourguy guy){ if( sqrt( ( (guy.x - x)*(guy.x - x) )+( (guy.y - y)*(guy.y - y) ) ) < (2*size) ){ return true; } return false; } void move(){ x += xs; y += ys; } void adjust(yourguy target){ if(!dead){ if(x < target.x){ xs += acc; } else { xs -= acc; } if(y < target.y){ ys += acc; } else { ys -= acc; } xs = xs * .99; ys = ys * .99; } else { if(x - (size*2) < 0 || (x+ size*2) > SCREENSIZE||y - (size*2) < 0 || (y+ size*2) > SCREENSIZE){ isOffscreen = true; } }//must be dead } void bounds(){ if(x < size){ dead = true; } if(x > SCREENSIZE - size){ dead = true; } if(y < size){ dead = true; } if(y > SCREENSIZE - size){ dead = true; } } void draw(){ if(! dead){ fill(100,20,20); } else { fill(0); } ellipse(x,y,size,size); } }