//// Bouncing man, bomb starts to fall when 'b' key pressed, hit adds 1 to score String title= "CST112 bam/manbomb"; float manX,manY, manW,manH; float manDX, manDY; float bombX,bombY, bombW,bombH; float bombDY; int score=0; void setup() //// intialize { size( 640,480); initialize(); } void initialize() //// initialize values { manX=width/2; manY=height/2; manW= 40; manH= 100; manDX= 3; // Each frame, man moved 3 right, 2 down manDY= 2; //// bombX=0; bombY= -1; // Negative Y means NO BOMB bombW=20; bombH=50; bombDY= 5; // Bomb falls 5 pixels / frame; } void draw() //// scene+score, man, bomb { scene(); man(); bomb(); checkhit(); } void keyPressed() //// Check which key was pressed. { if (key == 'b') { bombY=10; bombX= random( width ); } else if (key == 'r') { score=0; } } void scene() //// sky, grass, sun, score { background(200,200,255); fill(0); text( title, 10, height-20 ); text( score, width-200, 50); } void man() //// Draw man { //// Move man if (key == 'p') { text( "GAME PAUSED", manX+100, manY ); } else { manX += manDX; if (manX > width-manW/2 || manX < 0+manW/2) { manDX= -manDX; score += 1; } //-- if (manX < 0+manW/2 ) manDX= -manDX; manY += manDY; if (manY > height-manH/2 || manY < 0+manH/2 ) { manDY= -manDY; score += 2; } } //// Draw man fill(255,0,0); rectMode(CENTER); rect(manX,manY, manW,manH ); } void bomb() //// Draw bomb { // Check if bomb exists. if (bombY < 0 || bombY>height) { return; } //// Make bomb fall; bombY += bombDY; //// Draw bomb; fill(100,0,100); ellipse( bombX, bombY, 20,80 ); } void checkhit() //// If bomb hits man, do something bad. { if (abs(bombX-manX) < 40 && abs(bombY-manY) < 50 ) { background(0); score -= 50; manX=100; manY=100; } }