//// CST 112 Midterm exam (2011 Nov. 18) for: +++ ADD YOUR NAME HERE +++ //// CST 112 Midterm exam (2011 Nov. 18) for: +++ ADD YOUR NAME HERE +++ //// The name of this file is "midterm-YOURNAME.pde" /* For specifications and coding standards, see: http://www.suffolk.li/cst112/19cst112/midterm.html This code must be fully "modularized" and follow the "coding standards" including: -- meaningful names for variables and functions. -- a comment line preceding the code for each function. To receive credit for this exam, make sure you enter name where required, and name the uploaded file correctly as: "midterm-Lastname.pde" */ //// GLOBAL DATA //////// int score=0; // Score for the game. float xBall,yBall, dxBall,dyBall; // Position of the ball; velocity of ball. float xBullets,yBullets, dxBullets, dyBullets; // Position of the first bullet. float xBomb,yBomb, dxBomb, dyBomb; // Position of the bomb. float xHero,yHero; // Position of our "HERO". int nFrame=0; void setup() { //////// Set up the game: screen size, initialize value. size( 800, 600 ); // Screen size is 800x600. frameRate=30; smooth(); initialize(); } void initialize() { //// Initialize vales for new game. score=0; xBall= 0; yBall= height/2; dxBall=5; dyBall=2; xBullets= 0; yBullets= height/4; dxBullets=50; dyBullets=100; xBomb= width/2; yBomb= 0; dxBomb=0; dyBomb=10; } void draw() { //// Move everything; then draw everything. //// MOVE: ball, Bullets, bomb, hero. nFrame ++; drawScene(); xHero= mouseX; yHero=mouseY; moveBall(); moveBullets(); moveBomb(); moveHero(); // Also show effects of hits, etc. (and update score). //// DRAW: scene + ball, Bullets, bomb, hero. Also show score. drawBall( xBall, yBall ); drawBullets( xBullets, yBullets ); drawBomb( xBomb, yBomb ); drawHero( mouseX, mouseY ); // Hero follows the mouse! // NOTE: This "draw()" function should not contain any code that moves or draws on the screen. // Updating (moving, score-keeping, checking for hits, etc.) and drawing // should be done ONLY in the functions called by "draw()". } void drawScene() { //// Draw the background, show your name, and display the score. background( 192, 255, 255 ); fill( 200,150,100 ); //// Desert + hills. float horizon= height/2; rectMode(CORNER); rect( 0,horizon, width,height/2); // Desert. noStroke(); for (float x=0; xwidth) dxBall = -5; if (xBall<0) dxBall = 5; if (yBall>height) dyBall = -2; if (yBall<0) dyBall = 2; } void moveBullets() { //// Move the flock of Bullets. Take care of hits. //// ++++ ADD YOUR OWN CODE HERE ++++ if (nFrame % 30 == 0) { // New bullets. xBullets=0; // yBullets= random( height/2 ); yBullets= yBall - 40 + random(20); } xBullets= xBullets + dxBullets; } void moveBomb() { //// Move the bomb (downward). Take care of hits. //// ++++ ADD YOUR OWN CODE HERE ++++ xBomb= xBomb +20 - random(40); // Wobbly bomb. yBomb= yBomb + dyBomb; dyBomb += 9.81; //// Check for hits. if (hit(xBomb,yBomb, xHero,yHero, 20)) { //// Bomb hits hero. score -= 20; background(0); } } void moveHero() { //// Hero follows mouse. Nothing to do, here. } boolean hit( float x1, float y1, float x2, float y2, float margin ) { //// Return true if points (x1,y1) and (x2,y2) are close to each other. return dist(x1,y1, x2,y2) < margin; } //////// DRAW: ball, Bullets, bomb, hero. Also show score. void drawBall( float x, float y ){ //// Draw red ball at (x,y); fill(255,0,0); ellipse( x, y, 50,50 ); } void drawBullets( float x, float y ){ //// Draw a bunch of Bullets. //// ++++ REPLACE THIS CODE WITH A LOOP! ++++ for (int j=0; j<5; j++) { drawOneBullet( x, y ); x= x + dxBullets; y= y + dyBullets; } } void drawOneBullet( float x, float y ){ //// Draw a single Bullet. //// ++++ REPLACE THIS WITH OWN CODE HERE ++++ fill(192); rect( x,y, 20,10 ); if (abs(x-xHero)<100 && abs(y-yHero)<20) { //// Bullet hits hero. score -= 10; background(255,127,255); } } void drawBomb( float x, float y ){ //// ++++ REPLACE THIS WITH OWN CODE HERE ++++ fill(127); rect( x,y+50, 15,30 ); } void drawHero( float x, float y ){ //// Hero follows the mouse! fill(0,255,0); //// Green hero. rectMode(CENTER); rect( x, y, 10,30 ); //// +++++ Change the above code to draw correct figure !!!! // //// ++++ ADD YOUR OWN CODE HERE ++++ line( x-5,y-20, x-20,y-30); line( x+5,y-20, x+20,y-30); line( x-5,y+20, x-20,y+40); line( x+5,y+20, x+20,y+40); fill(0,255,0); ellipse(x,y-30, 15,15); } void drawGrass() { //// Draw grass along bottom of screen Also, make it wave with mouse movement. stroke(0,127,0); // Dark green. 20 high, 20 apart. Wave with mouse. float wave= mouseX-pmouseX; // How far did mouse move since last frame? for (float x=0; x