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