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
Fork of ll14zs by
DashEngine/DashEngine.cpp@3:1231a3961984, 2018-05-24 (annotated)
- Committer:
- ll14zs
- Date:
- Thu May 24 17:37:17 2018 +0000
- Revision:
- 3:1231a3961984
- Parent:
- 2:5d3aac7fd3df
Final Submission. I have read and agreed with Statement of Academic Integrity
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ll14zs | 3:1231a3961984 | 1 | #include "DashEngine.h" |
ll14zs | 3:1231a3961984 | 2 | |
ll14zs | 3:1231a3961984 | 3 | void scoreTicker_isr(); |
ll14zs | 3:1231a3961984 | 4 | volatile int g_score_flag=0; |
ll14zs | 3:1231a3961984 | 5 | |
ll14zs | 3:1231a3961984 | 6 | DashEngine::DashEngine() |
ll14zs | 3:1231a3961984 | 7 | { |
ll14zs | 3:1231a3961984 | 8 | |
ll14zs | 3:1231a3961984 | 9 | } |
ll14zs | 3:1231a3961984 | 10 | |
ll14zs | 3:1231a3961984 | 11 | DashEngine::~DashEngine() |
ll14zs | 3:1231a3961984 | 12 | { |
ll14zs | 3:1231a3961984 | 13 | |
ll14zs | 3:1231a3961984 | 14 | } |
ll14zs | 3:1231a3961984 | 15 | |
ll14zs | 3:1231a3961984 | 16 | |
ll14zs | 3:1231a3961984 | 17 | |
ll14zs | 3:1231a3961984 | 18 | |
ll14zs | 3:1231a3961984 | 19 | void DashEngine::init(int car_width,int car_height,int _x,int _y,int level_height) |
ll14zs | 3:1231a3961984 | 20 | { |
ll14zs | 3:1231a3961984 | 21 | |
ll14zs | 3:1231a3961984 | 22 | // initialise the game parameters |
ll14zs | 3:1231a3961984 | 23 | _car_width = car_width; |
ll14zs | 3:1231a3961984 | 24 | _car_height = car_height; |
ll14zs | 3:1231a3961984 | 25 | _car_x = _x; |
ll14zs | 3:1231a3961984 | 26 | _car_y = _y; |
ll14zs | 3:1231a3961984 | 27 | _level_height = level_height; |
ll14zs | 3:1231a3961984 | 28 | |
ll14zs | 3:1231a3961984 | 29 | |
ll14zs | 3:1231a3961984 | 30 | _car.init(_car_width,_car_height,_car_x,_car_y); |
ll14zs | 3:1231a3961984 | 31 | } |
ll14zs | 3:1231a3961984 | 32 | |
ll14zs | 3:1231a3961984 | 33 | void DashEngine::read_input(Gamepad &pad) |
ll14zs | 3:1231a3961984 | 34 | { |
ll14zs | 3:1231a3961984 | 35 | _d = pad.get_direction(); |
ll14zs | 3:1231a3961984 | 36 | pad.check_event(Gamepad::START_PRESSED) == false; |
ll14zs | 3:1231a3961984 | 37 | } |
ll14zs | 3:1231a3961984 | 38 | |
ll14zs | 3:1231a3961984 | 39 | void DashEngine::draw(N5110 &lcd,Gamepad &pad) |
ll14zs | 3:1231a3961984 | 40 | { |
ll14zs | 3:1231a3961984 | 41 | lcd.drawRect(0,0,82,48,0); // Draws in the LCD buffer |
ll14zs | 3:1231a3961984 | 42 | |
ll14zs | 3:1231a3961984 | 43 | _level.draw(lcd); // Level |
ll14zs | 3:1231a3961984 | 44 | |
ll14zs | 3:1231a3961984 | 45 | for(std::vector<Level*>::iterator it = LevelVectors.begin(); it != LevelVectors.end(); it++){ |
ll14zs | 3:1231a3961984 | 46 | (*it)->draw(lcd); //Iterator |
ll14zs | 3:1231a3961984 | 47 | |
ll14zs | 3:1231a3961984 | 48 | _car.draw(lcd); // Car |
ll14zs | 3:1231a3961984 | 49 | //printf("%d\n",drawcar); |
ll14zs | 3:1231a3961984 | 50 | |
ll14zs | 3:1231a3961984 | 51 | } |
ll14zs | 3:1231a3961984 | 52 | |
ll14zs | 3:1231a3961984 | 53 | void DashEngine::addLevel() |
ll14zs | 3:1231a3961984 | 54 | { |
ll14zs | 3:1231a3961984 | 55 | LevelVectors.push_back(new Level()); // Adds a new Level at the end of the vector, after its current last Level. The Level is copied (or moved) to the new Level. |
ll14zs | 3:1231a3961984 | 56 | |
ll14zs | 3:1231a3961984 | 57 | } |
ll14zs | 3:1231a3961984 | 58 | |
ll14zs | 3:1231a3961984 | 59 | void DashEngine::update(N5110 & lcd,Gamepad & pad) |
ll14zs | 3:1231a3961984 | 60 | { |
ll14zs | 3:1231a3961984 | 61 | _level.get.velocity(); |
ll14zs | 3:1231a3961984 | 62 | _car.update(_d); |
ll14zs | 3:1231a3961984 | 63 | score_timer(pad); |
ll14zs | 3:1231a3961984 | 64 | counter++; // Increment counter each cycle |
ll14zs | 3:1231a3961984 | 65 | if(counter == 25){ |
ll14zs | 3:1231a3961984 | 66 | counter =0; |
ll14zs | 3:1231a3961984 | 67 | addLevel(); |
ll14zs | 3:1231a3961984 | 68 | } |
ll14zs | 3:1231a3961984 | 69 | for std::vector<Level*>::iterator it = LevelVectors.begin(); it != LevelVectors.end(); it++){ |
ll14zs | 3:1231a3961984 | 70 | (*it)>>draw(lcd); //Iterator |
ll14zs | 3:1231a3961984 | 71 | } |
ll14zs | 3:1231a3961984 | 72 | |
ll14zs | 3:1231a3961984 | 73 | check_wall_collision(pad); |
ll14zs | 3:1231a3961984 | 74 | //printf("%d\n", wallcollision) |
ll14zs | 3:1231a3961984 | 75 | |
ll14zs | 3:1231a3961984 | 76 | check_level_collision(pad); |
ll14zs | 3:1231a3961984 | 77 | //printf("%d\n", levelcollision) |
ll14zs | 3:1231a3961984 | 78 | |
ll14zs | 3:1231a3961984 | 79 | void DashEngine::check_wall_collision(Gamepad & pad) |
ll14zs | 3:1231a3961984 | 80 | { |
ll14zs | 3:1231a3961984 | 81 | // read current car attributes |
ll14zs | 3:1231a3961984 | 82 | Vector2D car_pos = _car.get_pos(); |
ll14zs | 3:1231a3961984 | 83 | |
ll14zs | 3:1231a3961984 | 84 | // check if hit left wall |
ll14zs | 3:1231a3961984 | 85 | if(car_pos.x <= 1){ // 1 to 1 pixel boundary |
ll14zs | 3:1231a3961984 | 86 | car_pos.x = 1; |
ll14zs | 3:1231a3961984 | 87 | // audio feedback |
ll14zs | 3:1231a3961984 | 88 | pad.tone(750.0,0.1); |
ll14zs | 3:1231a3961984 | 89 | } |
ll14zs | 3:1231a3961984 | 90 | // check if hit right wall |
ll14zs | 3:1231a3961984 | 91 | else if(car_pos.x >= (80)){ |
ll14zs | 3:1231a3961984 | 92 | // audio feedback |
ll14zs | 3:1231a3961984 | 93 | pad.tone(750.0,0.1); |
ll14zs | 3:1231a3961984 | 94 | } |
ll14zs | 3:1231a3961984 | 95 | |
ll14zs | 3:1231a3961984 | 96 | // update car parameters |
ll14zs | 3:1231a3961984 | 97 | _car.set_pos(car_pos); |
ll14zs | 3:1231a3961984 | 98 | } |
ll14zs | 3:1231a3961984 | 99 | |
ll14zs | 3:1231a3961984 | 100 | void DashEngine::check_level_collisions(N5110 &lcd,Gamepad &pad) |
ll14zs | 3:1231a3961984 | 101 | { |
ll14zs | 3:1231a3961984 | 102 | // read current car attributes |
ll14zs | 3:1231a3961984 | 103 | Vector2D car_pos = _car.get_pos(); |
ll14zs | 3:1231a3961984 | 104 | Vector2D car_velocity = _car.get_velocity(); |
ll14zs | 3:1231a3961984 | 105 | |
ll14zs | 3:1231a3961984 | 106 | // see if the pixels below the car are clear |
ll14zs | 3:1231a3961984 | 107 | if (!(lcd.getPixel(car_pos.x, car_pos.y + _car_height+1) || lcd.getPixel(car_pos.x, car_pos.y + _car_height + 2))){ |
ll14zs | 3:1231a3961984 | 108 | |
ll14zs | 3:1231a3961984 | 109 | levelcollision = 0; |
ll14zs | 3:1231a3961984 | 110 | } |
ll14zs | 3:1231a3961984 | 111 | //printf("%d\n",levelcollision=0) |
ll14zs | 3:1231a3961984 | 112 | |
ll14zs | 3:1231a3961984 | 113 | |
ll14zs | 3:1231a3961984 | 114 | if ((lcd.getPixel(car_pos.x, car_pos.y + _car_height+1) || lcd.getPixel(car_pos.x, car_pos.y + _car_height + 2))){ |
ll14zs | 3:1231a3961984 | 115 | |
ll14zs | 3:1231a3961984 | 116 | levelcollision = 1; |
ll14zs | 3:1231a3961984 | 117 | pad.tone(850.0,0.1); |
ll14zs | 3:1231a3961984 | 118 | } |
ll14zs | 3:1231a3961984 | 119 | //printf("%d\n",levelcollision=1) |
ll14zs | 3:1231a3961984 | 120 | |
ll14zs | 3:1231a3961984 | 121 | if ( (levelcollision == 0)){ |
ll14zs | 3:1231a3961984 | 122 | car_velocity.y = 1; |
ll14zs | 3:1231a3961984 | 123 | } |
ll14zs | 3:1231a3961984 | 124 | |
ll14zs | 3:1231a3961984 | 125 | |
ll14zs | 3:1231a3961984 | 126 | if ( (levelcollision == 1)){ |
ll14zs | 3:1231a3961984 | 127 | car_velocity.y = -1; |
ll14zs | 3:1231a3961984 | 128 | } |
ll14zs | 3:1231a3961984 | 129 | |
ll14zs | 3:1231a3961984 | 130 | // update car parameters |
ll14zs | 3:1231a3961984 | 131 | _car.set_velocity(car_velocity); |
ll14zs | 3:1231a3961984 | 132 | _car.set_pos(car_pos); |
ll14zs | 3:1231a3961984 | 133 | } |
ll14zs | 3:1231a3961984 | 134 | |
ll14zs | 3:1231a3961984 | 135 | void DashEngine::score_timer(Gamepad & pad){ |
ll14zs | 3:1231a3961984 | 136 | |
ll14zs | 3:1231a3961984 | 137 | if (g_score_flag==1) { |
ll14zs | 3:1231a3961984 | 138 | |
ll14zs | 3:1231a3961984 | 139 | _car.add_score(); |
ll14zs | 3:1231a3961984 | 140 | } |
ll14zs | 3:1231a3961984 | 141 | } |
ll14zs | 3:1231a3961984 | 142 | |
ll14zs | 3:1231a3961984 | 143 | void DashEngine::check_gameover(N5110 & lcd, Gamepad & pad) |
ll14zs | 3:1231a3961984 | 144 | { |
ll14zs | 3:1231a3961984 | 145 | Vector2D car_pos = _car.get_pos(); |
ll14zs | 3:1231a3961984 | 146 | |
ll14zs | 3:1231a3961984 | 147 | int car_score = _car.get_score(); |
ll14zs | 3:1231a3961984 | 148 | |
ll14zs | 3:1231a3961984 | 149 | //check if car hits the top |
ll14zs | 3:1231a3961984 | 150 | if (car_pos.y <= 1) { |
ll14zs | 3:1231a3961984 | 151 | |
ll14zs | 3:1231a3961984 | 152 | int i=0; |
ll14zs | 3:1231a3961984 | 153 | while(1) { |
ll14zs | 3:1231a3961984 | 154 | i++; |
ll14zs | 3:1231a3961984 | 155 | |
ll14zs | 3:1231a3961984 | 156 | if (i==15){ |
ll14zs | 3:1231a3961984 | 157 | |
ll14zs | 3:1231a3961984 | 158 | // auido feedback |
ll14zs | 3:1231a3961984 | 159 | pad.tone(300.0,0.2); |
ll14zs | 3:1231a3961984 | 160 | wait(0.5); |
ll14zs | 3:1231a3961984 | 161 | pad.tone(500.0,0.4); |
ll14zs | 3:1231a3961984 | 162 | wait(0.5); |
ll14zs | 3:1231a3961984 | 163 | pad.tone(700.0,1.0); |
ll14zs | 3:1231a3961984 | 164 | } |
ll14zs | 3:1231a3961984 | 165 | |
ll14zs | 3:1231a3961984 | 166 | // end of game screen |
ll14zs | 3:1231a3961984 | 167 | lcd.clear(); |
ll14zs | 3:1231a3961984 | 168 | lcd.printString(" GAME OVER! ",0,1); |
ll14zs | 3:1231a3961984 | 169 | lcd.printString(" SCORE: ",0,4); |
ll14zs | 3:1231a3961984 | 170 | |
ll14zs | 3:1231a3961984 | 171 | char buffer1[14]; |
ll14zs | 3:1231a3961984 | 172 | sprintf(buffer1,"%2d",car_score); |
ll14zs | 3:1231a3961984 | 173 | lcd.printString(buffer1,25,4); |
ll14zs | 3:1231a3961984 | 174 | lcd.refresh(); |
ll14zs | 3:1231a3961984 | 175 | } |
ll14zs | 3:1231a3961984 | 176 | }} |