ELEC2645 (2019/20)
/
ELEC2645_Project_el17oc1
Owen Cavender 201159294
GameEngine.cpp@16:9500059ad5d8, 2020-05-30 (annotated)
- Committer:
- el17oc
- Date:
- Sat May 30 05:43:50 2020 +0000
- Revision:
- 16:9500059ad5d8
- Parent:
- 15:6857657bb4e2
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 |
---|---|---|---|
el17oc | 8:997f90c88246 | 1 | #include "GameEngine.h" |
el17oc | 8:997f90c88246 | 2 | |
el17oc | 16:9500059ad5d8 | 3 | GameEngine::GameEngine() //constructor |
el17oc | 8:997f90c88246 | 4 | { |
el17oc | 8:997f90c88246 | 5 | |
el17oc | 8:997f90c88246 | 6 | } |
el17oc | 8:997f90c88246 | 7 | |
el17oc | 16:9500059ad5d8 | 8 | GameEngine::~GameEngine() //deconstructor |
el17oc | 8:997f90c88246 | 9 | { |
el17oc | 8:997f90c88246 | 10 | } |
el17oc | 8:997f90c88246 | 11 | |
el17oc | 8:997f90c88246 | 12 | |
el17oc | 9:a69a6a06dddf | 13 | void GameEngine::print_scores(N5110 &lcd, Snake &snake) |
el17oc | 8:997f90c88246 | 14 | { |
el17oc | 14:7fb3c93343b6 | 15 | |
el17oc | 16:9500059ad5d8 | 16 | int score = snake.get_score(); //gets score from snake class object |
el17oc | 8:997f90c88246 | 17 | |
el17oc | 8:997f90c88246 | 18 | char buffer1[14]; |
el17oc | 16:9500059ad5d8 | 19 | sprintf(buffer1,"%2d",score); //sprintf is used to print as the program is displaying a variable |
el17oc | 16:9500059ad5d8 | 20 | lcd.printString(buffer1,0,48); |
el17oc | 8:997f90c88246 | 21 | } |
el17oc | 8:997f90c88246 | 22 | |
el17oc | 8:997f90c88246 | 23 | |
el17oc | 8:997f90c88246 | 24 | |
el17oc | 8:997f90c88246 | 25 | |
el17oc | 16:9500059ad5d8 | 26 | void GameEngine::get_LEDs(Gamepad &pad, Snake &snake) //controls LEDs based on snake position |
el17oc | 8:997f90c88246 | 27 | { |
el17oc | 16:9500059ad5d8 | 28 | pad.leds_off(); //initialise leds |
el17oc | 16:9500059ad5d8 | 29 | Vector2D Snakehead = snake.get_Snakehead(); //call values stored in _x0 and _y0 to identify the snakehead |
el17oc | 14:7fb3c93343b6 | 30 | int _x0 = Snakehead.x; |
el17oc | 16:9500059ad5d8 | 31 | int _y0 = Snakehead.y; |
el17oc | 16:9500059ad5d8 | 32 | //depending where the snakehead is on the screen, different LEDs will turn on indicating its postion |
el17oc | 16:9500059ad5d8 | 33 | // top right led on |
el17oc | 16:9500059ad5d8 | 34 | if (_x0 >= 42 && _y0 <= 16) { //defines paramaters of the top right quadrant of the lcd rectangle and turns on and off the top right LED if the x and y values are in the top right quadrant |
el17oc | 8:997f90c88246 | 35 | |
el17oc | 16:9500059ad5d8 | 36 | pad.led(4, 1); //turn on top right led4 |
el17oc | 16:9500059ad5d8 | 37 | wait(0.2); //wait 0.2 seconds |
el17oc | 16:9500059ad5d8 | 38 | pad.led(4, 0); //turn it off |
el17oc | 8:997f90c88246 | 39 | } |
el17oc | 8:997f90c88246 | 40 | // topleft led on |
el17oc | 16:9500059ad5d8 | 41 | if (_x0 <= 42 && _y0 <= 16 ) { //parameter for top left |
el17oc | 14:7fb3c93343b6 | 42 | |
el17oc | 16:9500059ad5d8 | 43 | pad.led(1, 1); //turns on top left led |
el17oc | 10:ee781d18e0f6 | 44 | wait(0.2); |
el17oc | 10:ee781d18e0f6 | 45 | pad.led(1, 0); |
el17oc | 8:997f90c88246 | 46 | } |
el17oc | 16:9500059ad5d8 | 47 | //bottom left //defines paramaters of the bottom left quadrant of the rectangle on the lcd display and turns on and off the bottom left LED |
el17oc | 14:7fb3c93343b6 | 48 | if (_x0 <=42 && _y0 >= 16 ) { |
el17oc | 8:997f90c88246 | 49 | |
el17oc | 16:9500059ad5d8 | 50 | pad.led(3,1); //turns on bottom left led |
el17oc | 10:ee781d18e0f6 | 51 | wait(0.2); |
el17oc | 10:ee781d18e0f6 | 52 | pad.led(3, 0); |
el17oc | 8:997f90c88246 | 53 | } |
el17oc | 8:997f90c88246 | 54 | //bottom right |
el17oc | 16:9500059ad5d8 | 55 | if (_x0 >= 42 && _y0 >= 16) { //there are some overlap on the symetrical x and y axis where conditions are true in both loops causing leds to coome on at the same time and alternately blink |
el17oc | 8:997f90c88246 | 56 | // top right led on |
el17oc | 8:997f90c88246 | 57 | pad.led(6, 1); |
el17oc | 10:ee781d18e0f6 | 58 | wait(0.2); |
el17oc | 16:9500059ad5d8 | 59 | pad.led(6, 0); //turns of bottom right led |
el17oc | 14:7fb3c93343b6 | 60 | } else { |
el17oc | 8:997f90c88246 | 61 | } |
el17oc | 12:60c856354406 | 62 | } |
el17oc | 12:60c856354406 | 63 | |
el17oc | 16:9500059ad5d8 | 64 | void GameEngine::print_countdown(N5110 &lcd, Snake &snake) //prints counter |
el17oc | 14:7fb3c93343b6 | 65 | { |
el17oc | 12:60c856354406 | 66 | |
el17oc | 14:7fb3c93343b6 | 67 | int countdown = snake.get_countdown(); //Accessing the value of the member variale _countdown |
el17oc | 12:60c856354406 | 68 | |
el17oc | 12:60c856354406 | 69 | char buffer1[14]; |
el17oc | 14:7fb3c93343b6 | 70 | sprintf(buffer1,"%2d",countdown); //printing value onto lcd in each loops. - counter is set to decrement by 1 each loop |
el17oc | 14:7fb3c93343b6 | 71 | lcd.printString(buffer1,0,4); // |
el17oc | 14:7fb3c93343b6 | 72 | // printf(" countdown= %d ", countdown); |
el17oc | 12:60c856354406 | 73 | } |
el17oc | 12:60c856354406 | 74 |