Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Game.cpp
00001 #include "Game.h" 00002 00003 Game::Game() 00004 { 00005 00006 } 00007 00008 Game::~Game() 00009 { 00010 00011 } 00012 00013 void Game::init() 00014 { 00015 // set character coordinates and health // 00016 baby.init(x, y); 00017 _health = 10; 00018 00019 // initialise all coin coordinates // 00020 coin1.init(41, 42); 00021 coin2.init(41, 23); 00022 coin3.init(78, 42); 00023 coin4.init(22, 34); 00024 coin5.init(22, 12); 00025 coin6.init(59, 12); 00026 coin7.init(59, 34); 00027 coin8.init(78, 14); 00028 coin9.init(78, 4); 00029 coin10.init(41, 4); 00030 coin11.init(4, 4); 00031 coin12.init(78, 23); 00032 coin13.init(13, 14); 00033 00034 // initialise all enemy coordinates // 00035 enemyA.init(28, 18); 00036 enemy1.init(58, 31); 00037 enemyB.init(67, 20); 00038 enemy2.init(67, 12); 00039 enemyC.init(1, 1); 00040 00041 // Reset incrementers // 00042 enemyA.resetIncrementer(); 00043 enemy1.resetIncrementer(); 00044 enemyB.resetIncrementer(); 00045 enemy2.resetIncrementer(); 00046 enemyC.resetIncrementer(); 00047 } 00048 00049 int Game::get_health() 00050 { 00051 return _health; 00052 } 00053 00054 void Game::direc(Gamepad &pad) 00055 { 00056 dir = pad.get_direction(); 00057 } 00058 00059 void Game::display_health(N5110 &lcd) 00060 { 00061 // displays updating health value on screen // 00062 char buffer[14]; 00063 sprintf(buffer,"%d",_health); 00064 lcd.printString(buffer,0,5); 00065 } 00066 00067 void Game::drawSprite(N5110 &lcd) 00068 { 00069 // draws maze // 00070 maze.drawSprite(lcd); 00071 00072 // draws all coins // 00073 coin1.drawSprite(lcd); 00074 coin2.drawSprite(lcd); 00075 coin3.drawSprite(lcd); 00076 coin4.drawSprite(lcd); 00077 coin5.drawSprite(lcd); 00078 coin6.drawSprite(lcd); 00079 coin7.drawSprite(lcd); 00080 coin8.drawSprite(lcd); 00081 coin9.drawSprite(lcd); 00082 coin10.drawSprite(lcd); 00083 coin11.drawSprite(lcd); 00084 coin12.drawSprite(lcd); 00085 coin13.drawSprite(lcd); 00086 00087 // draws all enemies // 00088 enemyA.drawSpriteA(lcd); 00089 enemy1.drawSprite1(lcd); 00090 enemyB.drawSpriteB(lcd); 00091 enemy2.drawSprite2(lcd); 00092 enemyC.drawSpriteC(lcd); 00093 00094 baby.drawSprite(lcd); 00095 } 00096 00097 void Game::movement(N5110 &lcd, Gamepad &pad) 00098 { 00099 // all sprite movement // 00100 baby.movement(dir, lcd); 00101 enemyA.movementA(lcd); 00102 enemy1.movement1(lcd); 00103 enemyB.movementB(lcd); 00104 enemy2.movement2(lcd); 00105 enemyC.movementC(lcd); 00106 } 00107 00108 void Game::collect(N5110 &lcd, Gamepad &pad) 00109 { 00110 // detects whether the coin needs to be respawned // 00111 int x = baby.get_x_char(); 00112 int y = baby.get_y_char(); 00113 coin1.spawn(x, y, lcd, pad); 00114 coin2.spawn(x, y, lcd, pad); 00115 coin3.spawn(x, y, lcd, pad); 00116 coin4.spawn(x, y, lcd, pad); 00117 coin5.spawn(x, y, lcd, pad); 00118 coin6.spawn(x, y, lcd, pad); 00119 coin7.spawn(x, y, lcd, pad); 00120 coin8.spawn(x, y, lcd, pad); 00121 coin9.spawn(x, y, lcd, pad); 00122 coin10.spawn(x, y, lcd, pad); 00123 coin11.spawn(x, y, lcd, pad); 00124 coin12.spawn(x, y, lcd, pad); 00125 coin13.spawn(x, y, lcd, pad); 00126 00127 } 00128 00129 void Game::win(N5110 &lcd) 00130 { 00131 // detects a win and displays win screen // 00132 if (coin == 1) { 00133 lcd.clear(); 00134 lcd.printString("YOU",30,2); 00135 lcd.printString("WIN",30,3); 00136 lcd.printString(":)",30,3); 00137 lcd.refresh(); 00138 wait(2); 00139 } 00140 } 00141 00142 void Game::damage(N5110 &lcd, Gamepad &pad) 00143 { 00144 // if collision with enemy detected -1 live // 00145 int x = baby.get_x_char(); 00146 int y = baby.get_y_char(); 00147 if (enemyA.collidePlayer(x, y, pad) == true || 00148 enemy1.collidePlayer(x, y, pad) == true || 00149 enemyB.collidePlayer(x, y, pad) == true || 00150 enemy2.collidePlayer(x, y, pad) == true || 00151 enemyC.collidePlayer(x, y, pad) == true) { 00152 _health--; 00153 wait(0.1); 00154 } 00155 } 00156 00157 void Game::death(N5110 &lcd) 00158 { 00159 // detects a loss and displays game over screen // 00160 if (_health == 0) { 00161 lcd.clear(); 00162 lcd.printString("PUT",30,1); 00163 lcd.printString("ON",30,2); 00164 lcd.printString("TIMEOUT",30,3); 00165 lcd.printString(":(",30,4); 00166 lcd.refresh(); 00167 wait(2); 00168 } 00169 } 00170 00171 void Game::UI(N5110 &lcd, Gamepad &pad) 00172 { 00173 // function to call on main menu in main // 00174 menu.main(lcd,pad); 00175 }
Generated on Wed Jul 13 2022 21:10:10 by
