// Buggy Game // By Dave Cross // set up window systems //******************************************************************************** int window_width = 400; int window_height = 400; int window_x = 300; int window_y = 200; string window_title = "Buggy Game"; int animation_speed = 65; // Globals // ******************************************************************************** int i = 0; //universal counter int score = 0; int shroom_count = 30; int bug_count = 10; int reset_flag = 0; //player died!!!! //int wait_duration = 5; //int waitcounter = 0; // bug variables int bug_x_inc = 5; // go right the game is played on a grid of 20 x 20 pixels int bug_x_dec = -5; // go left int bug_y_dec = -20; //bugs should move down //bullet variables int bullet_y_inc = 20; //bullets should move up //shroom variables int max_shroom_x = 380; int max_shroom_y = 380; int min_shroom_x = 0; int min_shroom_y = 40; // player must be able to shoot all mushrooms //ship variables int ship_x_inc = 20; int ship_y_inc = 20; int ship_x_dec = -20; int ship_y_dec = -20; int max_ship_x = 380; int max_ship_y = 100; int min_ship_x = 0; int min_ship_y = 0; //actor declarations pixmap shrooms[shroom_count]; pixmap bugs[bug_count]; pixmap ship_pixmap (x=20, y=20, filename = "ship1.bmp"); rectangle bullets(x=0,y=0,h=15,w=4); textbox display (x=2, y=2, text = "score: " + score); //animation forwards forward animation bullet_animation (rectangle bullet); forward animation bug_animation (pixmap bug); forward animation ship_animation (pixmap ship); int debug = 0; //used for debugging //initialization //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ initialization { for (i=0; i < bug_count; i+= 1) { shrooms[i].x = 400; shrooms[i].y = 400; shrooms[i].filename = "shroom1.bmp"; shrooms[i].visible = false; } for (i=bug_count; i < shroom_count; i +=1) { shrooms[i].x = (random(20)) * 20; shrooms[i].y = ((random(16)) * 20) + 40; shrooms[i].filename = "shroom1.bmp"; shrooms[i].visible = true; } for (i=0; i < bug_count; i += 1) { if (i%2) { bugs[i].filename = "bug1.bmp"; } else { bugs[i].filename = "bug2.bmp"; } bugs[i].x = i*20 + 20; bugs[i].y = 360; bugs[i].visible = true; bugs[i].animation_block = bug_animation; } ship_pixmap.animation_block = ship_animation; bullets.animation_block = bullet_animation; bullets.blue = 1.0; bullets.red = 1.0; bullets.visible = false; print("Arrow keys move the ship"); print("Space fires the missile"); print("Be careful, the bugs will kill you if they even get near you"); print("Shooting a Shroom is worth 10 pts"); print("Shooting a Bug is worth 15 pts"); print("Every Bug that makes it home is costs you 20 pts"); } //Animation Blocks //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // Animation handler for the bullet animation bullet_animation (rectangle bullet) { //debug = bullet.x; //print ("X= ", debug); //debug = bullet.y; //print ("Y= ", debug); if (bullet.visible == true) // if the bullet is out of play then short circuit this step { if (bullet.y >= 380) // if the bullet hits the ceiling start over! { bullet.visible = false; } for (i=0; i < shroom_count; i+=1) //start shroom for { if (bullet touches shrooms[i]) // if the bullet hits a shroom, //then check the color of the shroom { if (shrooms[i].filename == "shroom1.bmp") // if the shroom has never been //hit then advance the color of the //shroom and reset bullet { shrooms[i].filename = "shroom2.bmp"; bullet.visible = false; } else // if the shroom has been hit then erase //it and give the player some points { shrooms[i].visible = false; shrooms[i].x = 400; shrooms[i].y = 400; bullet.visible = false; score += 10; display.text = "score :" +score; } } }// end shroom for for (i=0; i < bug_count; i+=1) //start bug for { if (bullet touches bugs[i]) //if the bullet touches a bug then make //it invisible and put a hidden shroom in its place //note the first ten shroom are default hidden to account //for this. The same index can be used for //bugs and shrooms. If this is poor programming then //thhhppp!!!! { bullet.visible = false; // reset bullet shrooms[i].x = bugs[i].x; // make sure the shroom appears in the rights spot shrooms[i].y = bugs[i].y; bugs[i].visible = false; bugs[i].x = 400; bugs[i].y = 400; // the ol' switcheroo shrooms[i].visible = true; score += 15; display.text = "score :" +score; } }//end bug for bullet.y += bullet_y_inc; }// short circuit this code if the bullet is out of play } animation bug_animation (pixmap bug) { //if ((waitcounter%wait_duration) == 0) //{ // debug = waitcounter%wait_duration; // print("waitcounter%wait_counter: ",debug); //waitcounter += 1; // The first thing we need to know about bugs is that they always move to the left on even rows // and move right on odd rows, so (bug.y/20)%2 should give us a right or a left!! // 1 (true) means we go to the left // ugly, but it should work // let's start by making it look if the bug is walking // if (bug.filename == "bug1.bmp") // { // bug.filename = "bug2.bmp"; // } // else // { // bug.filename = "bug1.bmp"; // } if (bug touches ship) { reset_flag = 1; // player just got croaked!!!! // notice this means all spaces adjacent to the bug are dangerous // as well!! } if (bug.x < 0) { bug.x = 0; } //debug = bug.x; //print("walking ... ",debug); //did the bug make it home? if (bug.y != 0 || bug.x != 380) // if the bug made it home then we need to short //all this stuff and drop the players score { // now we need to check if the bug hit a wall if (bug.x >= 20 && bug.x <= 360) // ok all systems go lets check if a mushroom is touched { for (i=0; i < shroom_count; i +=1) // bug shroom collision checker { if (bug touches shrooms[i] && shrooms[i].visible) // ok we touched //a shroom, now what? { if (bug.y/20 == shrooms[i].y/20) // uhoh its on our row, did we hit it? { if (bug.x < shrooms[i].x) // it's on our left { if ((bug.y/20)%2) // crap we hit it! { bug.y += bug_y_dec; // lets drop a row and try again //NOTE: if we drop into a shroom funny // things will happen i = shroom_count; // let's "bug-out" of the loop } } // whew we missed it else { // it's on our right! if (!((bug.y/20)%2)) // crap we hit it! { bug.y += bug_y_dec; // lets drop a row and try again i = shroom_count; // let's "bug-out" of the loop //print("bug hit shroom on right"); } } // whew we missed it! } // end shroom row if } // end shroom touch if } // end shroom collision checker for if ((bug.y/20)%2) // ok we are not touching a wall or a shroom, //since we are on a odd row, lets go left!!! { // debug = (bug.y/20)%2; //print("i'm on row parity: ",debug); bug.x += bug_x_dec; // debug = bug.x; //print("walking left ",debug); } else // go right! { bug.x += bug_x_inc; // debug = bug.x; //print("walking right ",debug); } } // end wall hit if else { // Ok at this point we are touching a wall do we drop down or just keep going? if ((bug.y/20)%2) // we are on an odd row and moving left { if (bug.x <=0) // we hit the left wall! { // debug = (bug.y/20)%2; // since we are moving left, lets drop //print("i'm on row parity: ",debug); bug.y += bug_y_dec; //print("hit wall and dropping down"); } else { // debug = (bug.y/20)%2; //we are touching the other wall //print("i'm on row parity: ",debug); bug.x += bug_x_dec; //print("touching right wall, but moving on!"); } } else { if (bug.x >=380) // we hit the right wall! { // debug = (bug.y/20)%2; //we are moving right, better drop //print("i'm on row parity: ",debug); bug.y += bug_y_dec; //print("hit wall and dropping down"); } else { // debug = (bug.y/20)%2; // we are touching the other wall //print("i'm on row parity: ",debug); bug.x += bug_x_inc; //print("touching left wall, but moving on!"); } } } // end wall hitting else }//end bug not home if else { // okay the bug made it home, decrement the players score and remove the bug bug.visible = false; score += -20; display.text = "score :" +score; }//end bug home else //} //else //{ //debug = waitcounter; // print("waiting on a ", debug); // waitcounter += 1; // remember at the top we did a mod to skip frames, wrap it up here //end bug animation } animation ship_animation (pixmap ship) { if (reset_flag == 1) { ship.filename = "boom.bmp"; // that's it for this game!!! } else { if (bullets.visible == false) { ship.filename = "ship1.bmp"; // if the player has a bullet then light up front of ship } else { ship.filename = "ship2.bmp"; // else no bullet } } } // end ship animation // Call back block //################################################################################# on downarrow { if (!reset_flag) { if (ship.y > min_ship_y) { ship.y += ship_y_dec; } else { ship.y = min_ship_y; } } } on uparrow { if (!reset_flag) { if (ship.y < max_ship_y) { ship.y += ship_y_inc; } else { ship.y = max_ship_y; } } } on leftarrow { if (!reset_flag) { if (ship_pixmap.x > min_ship_x) { ship_pixmap.x += ship_x_dec; } else { ship_pixmap.x = min_ship_x; } } } on rightarrow { if (!reset_flag) { if (ship_pixmap.x < max_ship_x) { ship_pixmap.x += ship_x_inc; } else { ship_pixmap.x = max_ship_x; } } } on space { if (!reset_flag) { if (bullets.visible) // is the bullet in flight? { // print("space pressed"); } else { bullets.x = ship_pixmap.x + 10; bullets.y = ship_pixmap.y + 20; bullets.visible = true; // fire the bullet // print("bullet fired!!!!"); } } } // if the bullet is visible, do nothing // end program