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
Diff: DashEngine/DashEngine.cpp
- Revision:
- 3:1231a3961984
- Parent:
- 2:5d3aac7fd3df
--- a/DashEngine/DashEngine.cpp Tue May 08 11:15:58 2018 +0000 +++ b/DashEngine/DashEngine.cpp Thu May 24 17:37:17 2018 +0000 @@ -0,0 +1,176 @@ +#include "DashEngine.h" + +void scoreTicker_isr(); +volatile int g_score_flag=0; + +DashEngine::DashEngine() +{ + +} + +DashEngine::~DashEngine() +{ + +} + + + + +void DashEngine::init(int car_width,int car_height,int _x,int _y,int level_height) +{ + +// initialise the game parameters + _car_width = car_width; + _car_height = car_height; + _car_x = _x; + _car_y = _y; + _level_height = level_height; + + + _car.init(_car_width,_car_height,_car_x,_car_y); +} + +void DashEngine::read_input(Gamepad &pad) +{ + _d = pad.get_direction(); + pad.check_event(Gamepad::START_PRESSED) == false; +} + +void DashEngine::draw(N5110 &lcd,Gamepad &pad) +{ + lcd.drawRect(0,0,82,48,0); // Draws in the LCD buffer + + _level.draw(lcd); // Level + + for(std::vector<Level*>::iterator it = LevelVectors.begin(); it != LevelVectors.end(); it++){ + (*it)->draw(lcd); //Iterator + + _car.draw(lcd); // Car + //printf("%d\n",drawcar); + + } + +void DashEngine::addLevel() +{ + 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. + +} + +void DashEngine::update(N5110 & lcd,Gamepad & pad) +{ + _level.get.velocity(); + _car.update(_d); + score_timer(pad); + counter++; // Increment counter each cycle + if(counter == 25){ + counter =0; + addLevel(); + } + for std::vector<Level*>::iterator it = LevelVectors.begin(); it != LevelVectors.end(); it++){ + (*it)>>draw(lcd); //Iterator + } + + check_wall_collision(pad); + //printf("%d\n", wallcollision) + + check_level_collision(pad); + //printf("%d\n", levelcollision) + +void DashEngine::check_wall_collision(Gamepad & pad) +{ + // read current car attributes + Vector2D car_pos = _car.get_pos(); + + // check if hit left wall + if(car_pos.x <= 1){ // 1 to 1 pixel boundary + car_pos.x = 1; + // audio feedback + pad.tone(750.0,0.1); + } + // check if hit right wall + else if(car_pos.x >= (80)){ + // audio feedback + pad.tone(750.0,0.1); + } + + // update car parameters + _car.set_pos(car_pos); +} + +void DashEngine::check_level_collisions(N5110 &lcd,Gamepad &pad) +{ + // read current car attributes + Vector2D car_pos = _car.get_pos(); + Vector2D car_velocity = _car.get_velocity(); + + // see if the pixels below the car are clear + if (!(lcd.getPixel(car_pos.x, car_pos.y + _car_height+1) || lcd.getPixel(car_pos.x, car_pos.y + _car_height + 2))){ + + levelcollision = 0; + } + //printf("%d\n",levelcollision=0) + + + if ((lcd.getPixel(car_pos.x, car_pos.y + _car_height+1) || lcd.getPixel(car_pos.x, car_pos.y + _car_height + 2))){ + + levelcollision = 1; + pad.tone(850.0,0.1); + } + //printf("%d\n",levelcollision=1) + + if ( (levelcollision == 0)){ + car_velocity.y = 1; + } + + + if ( (levelcollision == 1)){ + car_velocity.y = -1; + } + + // update car parameters + _car.set_velocity(car_velocity); + _car.set_pos(car_pos); +} + +void DashEngine::score_timer(Gamepad & pad){ + + if (g_score_flag==1) { + + _car.add_score(); + } +} + +void DashEngine::check_gameover(N5110 & lcd, Gamepad & pad) +{ + Vector2D car_pos = _car.get_pos(); + + int car_score = _car.get_score(); + + //check if car hits the top + if (car_pos.y <= 1) { + + int i=0; + while(1) { + i++; + + if (i==15){ + + // auido feedback + pad.tone(300.0,0.2); + wait(0.5); + pad.tone(500.0,0.4); + wait(0.5); + pad.tone(700.0,1.0); + } + + // end of game screen + lcd.clear(); + lcd.printString(" GAME OVER! ",0,1); + lcd.printString(" SCORE: ",0,4); + + char buffer1[14]; + sprintf(buffer1,"%2d",car_score); + lcd.printString(buffer1,25,4); + lcd.refresh(); + } +}}