int score=0; float horizon; float heroX=20, heroY=300, heroDX=0, heroDY=0; float monsterX=500, monsterY=200, monsterDX=4, monsterDY=3; float birdsX=10, birdsY=100, birdsDX=6, birdsDY=0; float targetX=-20, targetY=-20; // (off-screen) void setup() { //// Init. size( 800, 600 ); horizon= height/3; } void draw() { //// Next frame if (key == '?') { help(); } else { scene(); action(); scoring(); } } void help() { int x=width/2, y=height/2; textSize(12); text( "Click to start hero moving.", x,y ); y +=15; text( "Lose 10 points, when monster hits hero.", x,y ); y +=15; text( "Gain 5 points, when birds finish a flight.", x,y ); } void action() { //// Move & draw creatures. hero(); monsters(); //// Check collisions if ( dist( heroX,heroY, monsterX,monsterY ) < 50 ) { background( 0 ); score -= 10; monsterX= width-20; // Move monster to left side, random height. monsterX= random( horizon, height); } } void hero() { //// Move & draw hero if (heroX<20 || heroX> width-20) heroDX *= -1; // Bounce if (heroY height-20) heroDY *= -1; heroX += heroDX; heroY += heroDY; rectMode( CENTER ); fill( 0, 255, 255 ); rect( heroX, heroY, 50, 80); fill( 255, 255, 255 ); ellipse( heroX, heroY-60, 40,40 ); //// Arms float y= heroY-40; for (int j=0; j<8; j++) { line( heroX-25, y, heroX-45, y ); line( heroX+25, y, heroX+45, y ); y += 10; } } void monsters() { //// Move & draw monster if (monsterX<20 || monsterX> width-20) monsterDX *= -1; if (monsterY height-20) monsterDY *= -1; monsterX += monsterDX; monsterY += monsterDY; fill( 255, 0, 0 ); ellipse( monsterX, monsterY, 50, 80); } void scene() { //// sky, grass, etc. background( 200,200,255 ); //// Grass fill( 100,g,100 ); rectMode( CORNERS ); rect( 0,horizon, width,height ); //// Flowers fill( 200, 200, 0 ); float x= 20, y=height-50; while (x < width-20) { ellipse( x, y, 30,30); line( x, y+15, x, y+40 ); x+= 50; } //// Titles, etc. textSize( 20 ); fill( 0,0,200 ); text( title, width/3, 40 ); textSize( 14 ); text( author, 20, height-100 ); } void scoring() { //// Scoreboard text( "Score is "+score, width-100, 50 ); } void keyPressed() { //// Event handler for keyboard if (key == 'q') { exit(); } else if (key == '+') { heroDX *= 1.1 ; // 10% speedup heroDY *= 1.1 ; } else if (key == '-') { heroDX *= 0.9 ; heroDY *= 0.9 ; } } void mousePressed() { //// Move hero toward mouse-click. //-- if (mouseX>heroX) heroDX= +2; //-- else heroDX= -2; //-- heroDY= (mouseY>heroY) ? +1.5 : -1.5 ; heroDX= (mouseX-heroX) / 60; heroDY= (mouseY-heroY) / 60; // heroDY= (mouseY>heroY) ? +1.5 : -1.5 ; background( 255 ); }