float sunX, sunY; // Position of sun float xMarcus, yMarcus; // Position and speed of Guy float dx, dy; float xgold, ygold; //position of gold float Gx,Gy; int score=100; // SETUP: Define screen size, set modes. void setup() { size(800,600); reset(); }// Guy follows the gold, while sun moves across the sky. void reset() { sunX= width/2; // Sun half-way across the screen. sunY= 50; xMarcus= width/3; // Start Guy in center. yMarcus= height/2; dx= random( 2,4 ); // Random speed. dy= random( -3, +1 ); xgold= width/3; //Positon of Gold ygold=height/3; } // DRAW: sky & sun plus Guy void draw() { chasegold(); scene(); action(); show(); messages(); // (Display the messages last.) } // SCENE: sky, sun, house. void scene() { background( 120, 200, 250 ); // Blue sky fill( 255, 255, 0 ); ellipse( sunX, sunY, 30, 30 ); // Yellow sun fill( 255, 0, 0 ); rect( 200,200, 200,100); // Red house triangle( 200,200, 400,200, 300,100 ); fill(100,30,40); rect(250,250,50,200); //Door fill( 20,255,30); rect(0,300, 800, 300); //Grass fill(85,42,42); rect(600,200, 50,100); //Tree fill(20,230,40); //Tree bush ellipse(625,200,100,100); //Tree fill(230,20,20); ellipse(650,170,20,20); //Apple fill(230,20,20); ellipse(600,210,20,20); //Apple fill(230,20,20); ellipse(635,200,20,20); //Apple fill(230,20,20); ellipse(620,220,20,20); //Apple fill(230,20,20); ellipse(615,192,20,20); //Apple } // MESSAGES. void messages() { fill(0); text( "Dynamic Scene of Guy moving and chasing gold", width/4, 15 ); text( "Guy follows the gold, while sun moves across the sky.", width/4, 30 ); text( "Click to reset Guy.\n Press 's' key to lower the sun, 'q' to quit.", 15, height/2 ); // Name text( "Young.M", 10, height-10 ); textSize(22); fill(250, 250, 250); text( "Project 1.", width/50, 20 ); fill( 0, 0, 0 ); textSize(20); fill(250, 50, 0); } // ACTION: sun moves (then resets to random height) void action() { if (sunX > width) { sunX= 0; sunY= random( 20, 120 ); } sunX= sunX + 1; // Move the Guy xMarcus= xMarcus + dx; yMarcus= yMarcus + dy; xgold= xgold+ Gx; ygold=ygold+ Gy; xMarcus= xMarcus + (xgold-xMarcus) /30; yMarcus= yMarcus + (ygold-yMarcus) / 25; if (xgold > width) { xgold= random( 20, 120 ); } } ////Events void chasegold() { if (dist(xMarcus, yMarcus, xgold, ygold) < 10) { ////Marcus chases gold xgold= random(10, 650); ygold= random(10, 400); score = score+1; } } // SHOW: Guy follows gold void show() { // Draw Guy fill( 0, 0, 0 ); rect( xMarcus,yMarcus, 60, 80 ); // Blue Guy fill(0,0,220); ellipse(xMarcus+60,yMarcus+80,40,20); ellipse(xMarcus+20,yMarcus+80,40,20); fill(0,0,0); rect( xMarcus+25, yMarcus-20, 20, 20 ); // Head on top ellipse(xMarcus+30,yMarcus-30,70,70); fill( 255 ); rect( xMarcus+15, yMarcus-25, 12, 12 ); // Eyes. rect( xMarcus+35, yMarcus-25, 12, 12 ); fill( 250, 50, 0 ); ellipse( xMarcus+15, yMarcus-25, 4, 4 ); ellipse( xMarcus+35, yMarcus-25, 4, 4 ); fill(255,215,0); ellipse(xgold,ygold, 40,30); //Gold Nugget } // EVENT HANDLERS //// void mousePressed() { reset(); // Set the position (x,y) xgold= mouseX; ygold= mouseY; } void keyPressed() { if (key == 'q') { exit(); } if (key == 'r') { reset(); } if (key == 's') { sunY= sunY + 50; } }