Modify a text file: $/loops2.pde
$/loops2.pde
//// CST 112 practice: loops //// 7417:bam String title= "CST 112 practice: loops"; String author= "Prof.BAM"; String news= "(...)"; float top, bottom=400; Button left= new Button( "LEFT", 100, 430 ); Button shoot= new Button( "SHOOT", 200, 430 ); Button right= new Button( "RIGHT", 300, 430 ); Button go= new Button( "GO", 500, 430, 255,100,0 ); Rifle rifle= new Rifle(); Bullet slug= new Bullet(); Target big= new Target(); void setup() { size(640, 480); top=height/6; bottom=height*5/6; // rifle.x= width*2/5; rifle.y= bottom; } void draw() { scene(); targets(); gun(); varmits(); buttons(); } void scene() { background( 255, 200, 255 ); fill(100, 200, 100); rect( 0, height/6, width, height*2/3 ); float top=height/6, bottom=height*5/6; //// Waving grass //// stroke( 0, 150, 0 ); strokeWeight(3); float x; float y= height-20; for ( x=20; x
0) { slug.move(); slug.show(); } } //// Targets void targets() { big.show(); big.move(); // for (float x=100; x<600; x+=50) { smallTarget( x, 25 ); } } // Small targets void smallTarget( float x, float y ) { fill(255); fill(255, 255, 0); ellipse(x, y, 30, 30); fill(0); ellipse(x, y, 15, 15); } // Varmits! void varmits() { float x=100; float y=300; fill( 200, 100, 0 ); ellipse( x, y, 50, 25 ); ellipse( x+20, y-15, 25, 15 ); for (float i=x-20; i
x+w) return false; if (yy < y || yy > y+h) return false; return true; } } class Rifle { float x, y; // void show() { fill(150, 100, 0); rect(x, y-60, 12, 80); rect(x-15, y+10, 15, 10); fill(0); rect(x-10, y, 10, 5); ellipse(x+6, y-60, 10, 5); } } class Bullet { float x, y; float speed=8; void show() { fill(0); ellipse( x+10,y, 10,10 ); } void move() { if (y<0) return; y = y - speed; // Bullet moves up. //// Make bullet "disappear", when it reaches the top. if (y<=top-5) { y= -100; news= "(missed)"; } } } class Target { float x=-100,y=0; float speed= 0.5; void move() { if (x>0) x += speed; if (x>width) x= -100; } void show() { fill(255); stroke(0); ellipse(x, y, 80, 80); fill(255, 0, 0); ellipse(x, y, 60, 60); fill(255, 255, 0); ellipse(x, y, 40, 40); fill(0); ellipse(x, y, 20, 20); } }