Zikang Qian / Mbed 2 deprecated el17z2q

Dependencies:   mbed

Fork of el17z2q by ELEC2645 (2017/18)

Committer:
yzjdxl
Date:
Mon May 07 22:11:43 2018 +0000
Branch:
GameEngine
Revision:
2:6dc7bc55c1cb
Parent:
1:00a4ea97c4cd
publish

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yzjdxl 1:00a4ea97c4cd 1 #include "GameEngine.h"
yzjdxl 1:00a4ea97c4cd 2
yzjdxl 1:00a4ea97c4cd 3 GameEngine::GameEngine()
yzjdxl 1:00a4ea97c4cd 4 {
yzjdxl 1:00a4ea97c4cd 5
yzjdxl 1:00a4ea97c4cd 6 }
yzjdxl 1:00a4ea97c4cd 7
yzjdxl 1:00a4ea97c4cd 8 GameEngine::~GameEngine()
yzjdxl 1:00a4ea97c4cd 9 {
yzjdxl 1:00a4ea97c4cd 10
yzjdxl 1:00a4ea97c4cd 11 }
yzjdxl 1:00a4ea97c4cd 12
yzjdxl 1:00a4ea97c4cd 13 void GameEngine::init(int bag_width,int bag_height,int coin_size,int speed)
yzjdxl 1:00a4ea97c4cd 14 {
yzjdxl 1:00a4ea97c4cd 15 // initialise the game parameters
yzjdxl 1:00a4ea97c4cd 16 _bag_width = bag_width;
yzjdxl 1:00a4ea97c4cd 17 _bag_height = bag_height;
yzjdxl 1:00a4ea97c4cd 18 _coin_size = coin_size;
yzjdxl 1:00a4ea97c4cd 19 _speed = speed;
yzjdxl 1:00a4ea97c4cd 20
yzjdxl 1:00a4ea97c4cd 21 // y position on screen - HEIGHT is defined in N5110.h
yzjdxl 1:00a4ea97c4cd 22 _py = HEIGHT - _bag_height;
yzjdxl 1:00a4ea97c4cd 23
yzjdxl 1:00a4ea97c4cd 24 // puts bag in middle
yzjdxl 1:00a4ea97c4cd 25 _bag.init(_py,_bag_height,_bag_width);
yzjdxl 1:00a4ea97c4cd 26
yzjdxl 1:00a4ea97c4cd 27 // puts coin in random place
yzjdxl 1:00a4ea97c4cd 28 _coin.init(_coin_size,_speed);
yzjdxl 1:00a4ea97c4cd 29 }
yzjdxl 1:00a4ea97c4cd 30
yzjdxl 1:00a4ea97c4cd 31 void GameEngine::read_input(Gamepad &pad)
yzjdxl 1:00a4ea97c4cd 32 {
yzjdxl 1:00a4ea97c4cd 33 // get the input information from Gamepad
yzjdxl 1:00a4ea97c4cd 34 _direction = pad.get_direction();
yzjdxl 1:00a4ea97c4cd 35 _mag = pad.get_mag();
yzjdxl 1:00a4ea97c4cd 36 }
yzjdxl 1:00a4ea97c4cd 37
yzjdxl 1:00a4ea97c4cd 38 void GameEngine::draw(N5110 &lcd)
yzjdxl 1:00a4ea97c4cd 39 {
yzjdxl 1:00a4ea97c4cd 40 // draw the elements in the LCD buffer
yzjdxl 1:00a4ea97c4cd 41 // score
yzjdxl 1:00a4ea97c4cd 42 print_scores(lcd);
yzjdxl 1:00a4ea97c4cd 43
yzjdxl 1:00a4ea97c4cd 44 // bag
yzjdxl 1:00a4ea97c4cd 45 _bag.draw(lcd);
yzjdxl 1:00a4ea97c4cd 46
yzjdxl 1:00a4ea97c4cd 47 // coin
yzjdxl 1:00a4ea97c4cd 48 _coin.draw(lcd);
yzjdxl 1:00a4ea97c4cd 49 }
yzjdxl 1:00a4ea97c4cd 50
yzjdxl 1:00a4ea97c4cd 51 int GameEngine::update(Gamepad &pad)
yzjdxl 1:00a4ea97c4cd 52 {
yzjdxl 1:00a4ea97c4cd 53 // the situation whether continue or end game, 1 stand for continue and 0 stand for game over
yzjdxl 1:00a4ea97c4cd 54 gameover = 1;
yzjdxl 1:00a4ea97c4cd 55
yzjdxl 1:00a4ea97c4cd 56 // updating bag
yzjdxl 1:00a4ea97c4cd 57 _bag.update(_direction,_mag);
yzjdxl 1:00a4ea97c4cd 58 // updating coin
yzjdxl 1:00a4ea97c4cd 59 _coin.update();
yzjdxl 1:00a4ea97c4cd 60
yzjdxl 1:00a4ea97c4cd 61 // case for adding score
yzjdxl 1:00a4ea97c4cd 62 check_bag_collisions(pad);
yzjdxl 1:00a4ea97c4cd 63 // case for ending game
yzjdxl 1:00a4ea97c4cd 64 game_over(pad);
yzjdxl 1:00a4ea97c4cd 65
yzjdxl 1:00a4ea97c4cd 66 return gameover;
yzjdxl 1:00a4ea97c4cd 67 }
yzjdxl 1:00a4ea97c4cd 68
yzjdxl 1:00a4ea97c4cd 69 // add score case
yzjdxl 1:00a4ea97c4cd 70 void GameEngine::check_bag_collisions(Gamepad &pad)
yzjdxl 1:00a4ea97c4cd 71 {
yzjdxl 1:00a4ea97c4cd 72 // get current coin attributes
yzjdxl 1:00a4ea97c4cd 73 Vector2D coin_position = _coin.get_coin_position();
yzjdxl 1:00a4ea97c4cd 74 // get current bag attributes
yzjdxl 1:00a4ea97c4cd 75 Vector2D bag_position = _bag.get_bag_position();
yzjdxl 1:00a4ea97c4cd 76
yzjdxl 1:00a4ea97c4cd 77 // see if bag has caught the coin by checking for overlap of coin and bag
yzjdxl 1:00a4ea97c4cd 78 if (
yzjdxl 1:00a4ea97c4cd 79 (coin_position.y + _coin_size -5<= _py + _bag_height) && //top
yzjdxl 1:00a4ea97c4cd 80 (coin_position.y + _coin_size -5>= _py) && //bottom
yzjdxl 2:6dc7bc55c1cb 81 (coin_position.x >= bag_position.x -2) && //left
yzjdxl 2:6dc7bc55c1cb 82 (coin_position.x <= bag_position.x + _bag_width +2) //right
yzjdxl 1:00a4ea97c4cd 83 ){
yzjdxl 1:00a4ea97c4cd 84 // game continue and add score when "gameover" return 1
yzjdxl 1:00a4ea97c4cd 85 gameover = 1;
yzjdxl 1:00a4ea97c4cd 86 // adding score process
yzjdxl 1:00a4ea97c4cd 87 _bag.add_score();
yzjdxl 1:00a4ea97c4cd 88
yzjdxl 1:00a4ea97c4cd 89 // reset a new coin
yzjdxl 1:00a4ea97c4cd 90 _coin.init(_coin_size,_speed);
yzjdxl 1:00a4ea97c4cd 91
yzjdxl 1:00a4ea97c4cd 92 // audio feedback
yzjdxl 1:00a4ea97c4cd 93 pad.tone(1000.0,0.5);
yzjdxl 1:00a4ea97c4cd 94 pad.leds_on();
yzjdxl 1:00a4ea97c4cd 95 wait(0.1);
yzjdxl 1:00a4ea97c4cd 96 pad.leds_off();
yzjdxl 1:00a4ea97c4cd 97 }
yzjdxl 1:00a4ea97c4cd 98 }
yzjdxl 1:00a4ea97c4cd 99
yzjdxl 1:00a4ea97c4cd 100 // gameover case
yzjdxl 1:00a4ea97c4cd 101 void GameEngine::game_over(Gamepad &pad)
yzjdxl 1:00a4ea97c4cd 102 {
yzjdxl 1:00a4ea97c4cd 103 // get current coin attributes
yzjdxl 1:00a4ea97c4cd 104 Vector2D coin_position = _coin.get_coin_position();
yzjdxl 1:00a4ea97c4cd 105 // get current bag attributes
yzjdxl 1:00a4ea97c4cd 106 Vector2D bag_position = _bag.get_bag_position();
yzjdxl 1:00a4ea97c4cd 107
yzjdxl 1:00a4ea97c4cd 108 // see if coin fall away by checking for overlap of coin and bottom boundary
yzjdxl 1:00a4ea97c4cd 109 if (coin_position.y + _coin_size >= 48)
yzjdxl 1:00a4ea97c4cd 110 {
yzjdxl 1:00a4ea97c4cd 111 // game over when "gameover" return 0
yzjdxl 1:00a4ea97c4cd 112 gameover = 0;
yzjdxl 1:00a4ea97c4cd 113
yzjdxl 1:00a4ea97c4cd 114 // audio feedback
yzjdxl 1:00a4ea97c4cd 115 pad.tone(1500.0,0.5);
yzjdxl 1:00a4ea97c4cd 116 pad.leds_on();
yzjdxl 1:00a4ea97c4cd 117 wait(0.5);
yzjdxl 1:00a4ea97c4cd 118 pad.leds_off();
yzjdxl 1:00a4ea97c4cd 119 }
yzjdxl 1:00a4ea97c4cd 120 }
yzjdxl 1:00a4ea97c4cd 121
yzjdxl 1:00a4ea97c4cd 122 void GameEngine::print_scores(N5110 &lcd)
yzjdxl 1:00a4ea97c4cd 123 {
yzjdxl 1:00a4ea97c4cd 124 // get scores from bag
yzjdxl 1:00a4ea97c4cd 125 int score = _bag.get_score();
yzjdxl 1:00a4ea97c4cd 126
yzjdxl 1:00a4ea97c4cd 127 // get highest scores from bag
yzjdxl 1:00a4ea97c4cd 128 int Hscore = _bag.get_Hscore();
yzjdxl 1:00a4ea97c4cd 129
yzjdxl 1:00a4ea97c4cd 130 // print to LCD
yzjdxl 1:00a4ea97c4cd 131 char buffer1[14];
yzjdxl 1:00a4ea97c4cd 132 char buffer2[14];
yzjdxl 1:00a4ea97c4cd 133 sprintf(buffer1,"%2d",score);
yzjdxl 1:00a4ea97c4cd 134 sprintf(buffer2,"%2d",Hscore);
yzjdxl 1:00a4ea97c4cd 135
yzjdxl 1:00a4ea97c4cd 136 // current score is shown on right top corner
yzjdxl 1:00a4ea97c4cd 137 lcd.printString(buffer1,68,0);
yzjdxl 1:00a4ea97c4cd 138
yzjdxl 1:00a4ea97c4cd 139 // highest score is shown on right bottom corner
yzjdxl 1:00a4ea97c4cd 140 lcd.printString("HS:",68,4);
yzjdxl 1:00a4ea97c4cd 141 lcd.printString(buffer2,68,5);
yzjdxl 1:00a4ea97c4cd 142 }