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
GameEngine/GameEngine.cpp@14:4356797fd16e, 2020-05-24 (annotated)
- Committer:
- sdlashmar
- Date:
- Sun May 24 15:27:53 2020 +0000
- Revision:
- 14:4356797fd16e
- Parent:
- 13:4fa0d5148216
FINISHED VERSION BEFORE TRYING TO IMPLEMENT TAIL COLLISION FUNCTION
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| sdlashmar | 7:c67a5c6a874f | 1 | #include "GameEngine.h" |
| sdlashmar | 7:c67a5c6a874f | 2 | |
| sdlashmar | 7:c67a5c6a874f | 3 | GameEngine::GameEngine() |
| sdlashmar | 7:c67a5c6a874f | 4 | { |
| sdlashmar | 7:c67a5c6a874f | 5 | |
| sdlashmar | 7:c67a5c6a874f | 6 | } |
| sdlashmar | 7:c67a5c6a874f | 7 | |
| sdlashmar | 7:c67a5c6a874f | 8 | GameEngine::~GameEngine() |
| sdlashmar | 7:c67a5c6a874f | 9 | { |
| sdlashmar | 7:c67a5c6a874f | 10 | |
| sdlashmar | 7:c67a5c6a874f | 11 | } |
| sdlashmar | 7:c67a5c6a874f | 12 | |
| sdlashmar | 7:c67a5c6a874f | 13 | void GameEngine::init(int head_size, int head_speed) |
| sdlashmar | 7:c67a5c6a874f | 14 | { |
| sdlashmar | 14:4356797fd16e | 15 | //initial random coordinates for mouse spawn |
| sdlashmar | 10:3958fb08696d | 16 | srand(time(NULL)); |
| sdlashmar | 14:4356797fd16e | 17 | _mouse_x = rand() %80+4; |
| sdlashmar | 14:4356797fd16e | 18 | _mouse_y = rand() %44+4; |
| sdlashmar | 14:4356797fd16e | 19 | //initialise parameters |
| sdlashmar | 9:f0213e632379 | 20 | _game_over = false; |
| sdlashmar | 7:c67a5c6a874f | 21 | _score = 0; |
| sdlashmar | 8:1e4182ebb063 | 22 | _mouse = 0; |
| sdlashmar | 11:c4b740a970f8 | 23 | _length = 0; |
| sdlashmar | 7:c67a5c6a874f | 24 | _head_size = head_size; |
| sdlashmar | 7:c67a5c6a874f | 25 | _head_speed = head_speed; |
| sdlashmar | 14:4356797fd16e | 26 | //initialise the head |
| sdlashmar | 7:c67a5c6a874f | 27 | head.init(_head_size, _head_speed); |
| sdlashmar | 7:c67a5c6a874f | 28 | |
| sdlashmar | 7:c67a5c6a874f | 29 | } |
| sdlashmar | 7:c67a5c6a874f | 30 | |
| sdlashmar | 7:c67a5c6a874f | 31 | void GameEngine::read_input(Gamepad& pad) |
| sdlashmar | 7:c67a5c6a874f | 32 | { |
| sdlashmar | 14:4356797fd16e | 33 | //read direction from joystick |
| sdlashmar | 7:c67a5c6a874f | 34 | _d = pad.get_direction(); |
| sdlashmar | 7:c67a5c6a874f | 35 | } |
| sdlashmar | 7:c67a5c6a874f | 36 | |
| sdlashmar | 7:c67a5c6a874f | 37 | void GameEngine::update(Gamepad &pad, N5110 &lcd) |
| sdlashmar | 7:c67a5c6a874f | 38 | { |
| sdlashmar | 7:c67a5c6a874f | 39 | head.change_direction(_d); |
| sdlashmar | 14:4356797fd16e | 40 | //need to make sure the current head position is stored before updating as this then becomes the previous head position for updating the snake tail |
| sdlashmar | 11:c4b740a970f8 | 41 | Vector2D prevHead = head.get_pos(); |
| sdlashmar | 14:4356797fd16e | 42 | //tail updated before head |
| sdlashmar | 11:c4b740a970f8 | 43 | tail.update(prevHead, _length); |
| sdlashmar | 7:c67a5c6a874f | 44 | head.update(); |
| sdlashmar | 7:c67a5c6a874f | 45 | check_mouse_eaten(pad); |
| sdlashmar | 7:c67a5c6a874f | 46 | check_wall_collision(pad, lcd); |
| sdlashmar | 7:c67a5c6a874f | 47 | } |
| sdlashmar | 7:c67a5c6a874f | 48 | |
| sdlashmar | 7:c67a5c6a874f | 49 | void GameEngine::draw(N5110 &lcd) |
| sdlashmar | 7:c67a5c6a874f | 50 | { |
| sdlashmar | 7:c67a5c6a874f | 51 | lcd.drawRect(0, 0, WIDTH, HEIGHT, FILL_TRANSPARENT); |
| sdlashmar | 7:c67a5c6a874f | 52 | head.draw(lcd); |
| sdlashmar | 11:c4b740a970f8 | 53 | tail.draw(lcd, _length); |
| sdlashmar | 11:c4b740a970f8 | 54 | |
| sdlashmar | 10:3958fb08696d | 55 | spawn_mouse(lcd); |
| sdlashmar | 7:c67a5c6a874f | 56 | } |
| sdlashmar | 7:c67a5c6a874f | 57 | |
| sdlashmar | 7:c67a5c6a874f | 58 | void GameEngine::spawn_mouse(N5110 &lcd) |
| sdlashmar | 7:c67a5c6a874f | 59 | { |
| sdlashmar | 14:4356797fd16e | 60 | // draw the mouse from given x and y coordinates |
| sdlashmar | 14:4356797fd16e | 61 | lcd.drawRect(_mouse_x, _mouse_y, 2, 2, FILL_BLACK); |
| sdlashmar | 7:c67a5c6a874f | 62 | } |
| sdlashmar | 7:c67a5c6a874f | 63 | |
| sdlashmar | 7:c67a5c6a874f | 64 | void GameEngine::check_mouse_eaten(Gamepad &pad) |
| sdlashmar | 7:c67a5c6a874f | 65 | { |
| sdlashmar | 7:c67a5c6a874f | 66 | Vector2D headPos = head.get_pos(); |
| sdlashmar | 7:c67a5c6a874f | 67 | int headX = headPos.x; |
| sdlashmar | 7:c67a5c6a874f | 68 | int headY = headPos.y; |
| sdlashmar | 11:c4b740a970f8 | 69 | //printf("head x = %i\n", headX); |
| sdlashmar | 11:c4b740a970f8 | 70 | //printf("head y = %i\n", headY); |
| sdlashmar | 14:4356797fd16e | 71 | //check if the head position is the same as the mouse position, if it is score increases by ten and new mouse coordinates generated |
| sdlashmar | 10:3958fb08696d | 72 | if((headX == _mouse_x || headX == _mouse_x+1 || headX == _mouse_x-1) && (headY == _mouse_y || (headY == _mouse_y+1 || headY == _mouse_y-1))) { |
| sdlashmar | 7:c67a5c6a874f | 73 | _score = _score + 10; |
| sdlashmar | 9:f0213e632379 | 74 | _mouse = 0; |
| sdlashmar | 7:c67a5c6a874f | 75 | pad.tone(750.0, 0.1); |
| sdlashmar | 10:3958fb08696d | 76 | srand(time(NULL)); |
| sdlashmar | 14:4356797fd16e | 77 | _mouse_x = rand() %80+4; |
| sdlashmar | 14:4356797fd16e | 78 | _mouse_y = rand() %44+4; |
| sdlashmar | 11:c4b740a970f8 | 79 | _length++; |
| sdlashmar | 7:c67a5c6a874f | 80 | } |
| sdlashmar | 7:c67a5c6a874f | 81 | } |
| sdlashmar | 7:c67a5c6a874f | 82 | |
| sdlashmar | 7:c67a5c6a874f | 83 | void GameEngine::check_wall_collision(Gamepad &pad, N5110 &lcd) |
| sdlashmar | 7:c67a5c6a874f | 84 | { |
| sdlashmar | 7:c67a5c6a874f | 85 | Vector2D headPos = head.get_pos(); |
| sdlashmar | 7:c67a5c6a874f | 86 | int headX = headPos.x; |
| sdlashmar | 7:c67a5c6a874f | 87 | int headY = headPos.y; |
| sdlashmar | 14:4356797fd16e | 88 | // checks if the snake head has it the wall, if it has then the game over function is run |
| sdlashmar | 8:1e4182ebb063 | 89 | if (headX < 0+2 || headX > WIDTH-3) { |
| sdlashmar | 9:f0213e632379 | 90 | _game_over = true; |
| sdlashmar | 7:c67a5c6a874f | 91 | game_over(lcd, pad); |
| sdlashmar | 7:c67a5c6a874f | 92 | } |
| sdlashmar | 8:1e4182ebb063 | 93 | if (headY < 0+2 || headY > HEIGHT-3) { |
| sdlashmar | 9:f0213e632379 | 94 | _game_over = true; |
| sdlashmar | 7:c67a5c6a874f | 95 | game_over(lcd, pad); |
| sdlashmar | 7:c67a5c6a874f | 96 | } |
| sdlashmar | 7:c67a5c6a874f | 97 | } |
| sdlashmar | 7:c67a5c6a874f | 98 | |
| sdlashmar | 7:c67a5c6a874f | 99 | void GameEngine::game_over(N5110 &lcd, Gamepad &pad) |
| sdlashmar | 7:c67a5c6a874f | 100 | { |
| sdlashmar | 7:c67a5c6a874f | 101 | pad.tone(NOTE_E5, 0.1); |
| sdlashmar | 9:f0213e632379 | 102 | wait(0.1); |
| sdlashmar | 7:c67a5c6a874f | 103 | pad.tone(NOTE_D5, 0.1); |
| sdlashmar | 9:f0213e632379 | 104 | wait(0.1); |
| sdlashmar | 7:c67a5c6a874f | 105 | pad.tone(NOTE_C5, 0.1); |
| sdlashmar | 9:f0213e632379 | 106 | while(_game_over == true){ |
| sdlashmar | 9:f0213e632379 | 107 | lcd.clear(); |
| sdlashmar | 12:cb3a81adf48b | 108 | lcd.printString(" GAME OVER! ", 0, 0); |
| sdlashmar | 7:c67a5c6a874f | 109 | char buffer[14]; |
| sdlashmar | 7:c67a5c6a874f | 110 | sprintf(buffer, "%2d", _score); |
| sdlashmar | 9:f0213e632379 | 111 | lcd.printString(" You scored: ", 2, 2); |
| sdlashmar | 9:f0213e632379 | 112 | lcd.printString(buffer, 8, 4); |
| sdlashmar | 9:f0213e632379 | 113 | lcd.refresh(); |
| sdlashmar | 9:f0213e632379 | 114 | }; |
| sdlashmar | 7:c67a5c6a874f | 115 | } |
| sdlashmar | 7:c67a5c6a874f | 116 | |
| sdlashmar | 7:c67a5c6a874f | 117 | |
| sdlashmar | 7:c67a5c6a874f | 118 |