Modify a text file: $/monsters.pde
$/monsters.pde
//// Practice - '1/11/9 float xHero=320, yHero=240; float xMonsters=100, yMonsters=100; float dx=2, dy=1; int nMonsters=0; boolean hit; //// True if monster is near hero. void setup() { //// size( 640, 480 ); } void draw() { background(255); if (key == 'y') background(255,255,0); text( "The last key was: " + key, 200,100); fill(0); //-- text( "@@@ There are " + nMonsters + " monsters!", 400,20); //// Check if hero is hit by monsters. hit= isOver( xHero,yHero, xMonsters,yMonsters, 40); //// Hero moveHero(); drawHero(); //// Monsters moveMonsters(); drawMonsters(); } void keyPressed() { //// background(0); } void mouseClicked() { //// When mouse is clicked, add another monster (over 20, down 10); nMonsters += 1; } boolean isOver( float x1, float y1, float x2, float y2, float margin) { //// Return true iff (x1,y1) is within 50 of (x2,y2). return dist(x1,y1, x2,y2) < margin ; } void moveHero() { //// hero foloows mouse. xHero= mouseX; yHero= mouseY; } void drawHero() { // Head if (hit) { fill(0,255,0); // Green, if hit! } else { fill(255,127,127); } ellipseMode(CENTER); ellipse(xHero, yHero, 50, 50); // Body. if (hit) { fill(0,255,0); } else { fill(0,255,255); } rectMode(CENTER); rect(xHero, yHero+75, 100, 100); } void moveMonsters() { //// Move the monsters -- bounce off walls. //-- text( "@@@ moveMonsters was called", 300, 60); //-- text( "@@@ dx=" + dx, 300, 80); //-- text( "@@@ dy=" + dy, 300, 100); xMonsters += dx; if (xMonsters>width) dx= -2; if (xMonsters<0) dx= +2; yMonsters += dy; if (yMonsters>height) dy= -1; if (yMonsters<0) dy= +1; } void drawMonsters() { //// Use a loop to draw them all. fill(0); text( "@@@ There are " + nMonsters + " monsters!", 400,40); int j; float x=xMonsters, y=yMonsters; for ( j=0; j