Kern Fowler / Mbed 2 deprecated Donkey_Kong_Game

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Barrel.cpp Source File

Barrel.cpp

00001 /*
00002 ELEC2645 Project
00003 Barrel.cpp
00004 Class file for Barrel in Donkey Kong game.
00005 */
00006 
00007 #include "Barrel.h"
00008 
00009 // Constructor - Doesn't require any setup.
00010 Barrel::Barrel()
00011 {
00012 
00013 }
00014 
00015 // Deconstructor - Doesn't require any setup.
00016 Barrel::~Barrel()
00017 {
00018 
00019 }
00020 
00021 // External variables to be used inside and out of the class.
00022 int barrel_x = 30; // Barrel's coordinates.
00023 int barrel_y = 0;
00024 int barrel_speed = 0; // Barrel's dropspeed.
00025 int barrel_min = 0; // Barrel's min and max spawn ppositions - limited by screen size.
00026 int barrel_max = 76;
00027 float barrel_time = 0; // Time Barrel has been on screen, for score.
00028 int running = 1; // Player score.
00029 
00030 int game_barrel[4][8] = { // Barrel sprite.
00031     {0,1,1,1,1,1,1,0,},
00032     {1,1,1,1,1,1,1,1,},
00033     {1,1,1,1,1,1,1,1,},
00034     {0,1,1,1,1,1,1,0,},
00035 };
00036 
00037 // Spawns a barrel at a random x location, then slowly falls down screen. When reaches bottom it restarts. If collision with player leads to gameover.
00038 void Barrel::barrel_drop(Gamepad &pad, N5110 &lcd, Donkey &dky) {
00039     lcd.drawSprite(barrel_x,barrel_y,4,8,(int *)game_barrel); // Draws the barrel sprite on screen with correct coordinates.
00040     lcd.refresh();
00041     wait_ms(50);
00042     barrel_y = barrel_y + 1 + barrel_time;
00043     if (barrel_y > 44) { // If barrel reaches bottom of the screen, the resets.
00044         barrel_y = 0;
00045         barrel_x = rand() % (barrel_max + 1 - barrel_min) + barrel_min;
00046         barrel_time = barrel_time + 0.1; // Moves barrel slowly down screen.
00047     }
00048     if ((barrel_y >= 34) & ((barrel_x + 7) >= donkeykong_x) & (barrel_x <= (donkeykong_x + 15))) { // If barrel collides with player then sets running to 0, causing gameover.
00049         running = 0;
00050         //printf("Barrel Hit")
00051     }
00052 }