//// Bouncing man, bomb starts to fall when 'b' key pressed, hit adds 1 to score //// Add head, eyes, and randomly-moving arms. String title= "CST112 bam/manbomb2 - add head, random arms, eyes "; //// Coordiantes of man, etc. Width, height, etc. float manX,manY, manW,manH; float headR; // Radius of ehad. float manDX, manDY; float bombX,bombY, bombW,bombH; float bombDY; int numhits=0; int score=0; void setup() //// intialize { size( 640,480); initialize(); smooth(); } 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; headR= 15; //// 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,220,255); fill(0); text( score, width-200, 50); text( "Press b key for bomb.", width-200, 70 ); text( title, 10, height-20 ); } 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; } } drawMan(); } void drawMan() //// Draw the man. { //// Draw body: red rectangle. fill(255,0,0); rectMode(CENTER); rect(manX,manY, manW,manH ); //// Show hits on shirt. if (numhits > 0 ) { fill(0); text( numhits, manX-5, manY ); } //// Head. float x,y; x= manX; y= manY - manH/2 - headR; fill( 0,255,0 ); ellipse( x, y, 2*headR, 2*headR ); //// Eyes. fill( 255,200,255 ); ellipse( x-headR/3, y-6, 10,10 ); ellipse( x+headR/3, y-6, 10,10 ); //// Arms stroke(255,0,0); strokeWeight(4); //// X coordinate of left and right side float leftX= manX-manW/2; float rightX= manX+manW/2; //// Y coordinates of top and bottom of rectangle. float topY= manY-manH/2; float bottomY= manY+manH/2; line( leftX,topY, leftX-random(50), topY+random(50) ); line( rightX,topY, rightX+random(50), topY+random(50) ); line( leftX,bottomY, leftX-random(20), bottomY+random(70) ); line( rightX,bottomY, rightX+random(20), bottomY+random(70) ); strokeWeight(0); } 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; bombY= -1; // Bomb disappears. manX=100; manY=100; numhits += 1; } }