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 ll16j23s_test_docs
SnakeEngine/SnakeEngine.cpp@4:ea3fa51c4386, 2020-05-23 (annotated)
- Committer:
- JoeShotton
- Date:
- Sat May 23 15:31:30 2020 +0000
- Revision:
- 4:ea3fa51c4386
- Parent:
- 3:fcd6d70e9694
- Child:
- 5:06fa7674622a
Food and snake collisions implemented, still buggy
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JoeShotton | 3:fcd6d70e9694 | 1 | #include "SnakeEngine.h" |
JoeShotton | 3:fcd6d70e9694 | 2 | |
JoeShotton | 3:fcd6d70e9694 | 3 | SnakeEngine::SnakeEngine() |
JoeShotton | 3:fcd6d70e9694 | 4 | { |
JoeShotton | 3:fcd6d70e9694 | 5 | //constructor |
JoeShotton | 4:ea3fa51c4386 | 6 | score = 0; |
JoeShotton | 3:fcd6d70e9694 | 7 | } |
JoeShotton | 3:fcd6d70e9694 | 8 | |
JoeShotton | 3:fcd6d70e9694 | 9 | SnakeEngine::~SnakeEngine() |
JoeShotton | 3:fcd6d70e9694 | 10 | { |
JoeShotton | 3:fcd6d70e9694 | 11 | //destructor |
JoeShotton | 3:fcd6d70e9694 | 12 | } |
JoeShotton | 3:fcd6d70e9694 | 13 | |
JoeShotton | 3:fcd6d70e9694 | 14 | /* bool collision(int state, int x, int y,) { |
JoeShotton | 3:fcd6d70e9694 | 15 | if ((lcd.getPixel(x, y) == 1 && ((state == 1) || (state == 4))) || (lcd.getPixel(x+1, y+1) == 1 && ((state == 2) || (state == 3)))) { |
JoeShotton | 3:fcd6d70e9694 | 16 | // checks infront of head to see if pixel is set |
JoeShotton | 3:fcd6d70e9694 | 17 | // due to the size of the head, there is an offset for the check for North and Eastward directions |
JoeShotton | 3:fcd6d70e9694 | 18 | pad.led(1,0.9); |
JoeShotton | 3:fcd6d70e9694 | 19 | return true; |
JoeShotton | 3:fcd6d70e9694 | 20 | } else { |
JoeShotton | 3:fcd6d70e9694 | 21 | pad.led(1,0.0); |
JoeShotton | 3:fcd6d70e9694 | 22 | return = false; |
JoeShotton | 3:fcd6d70e9694 | 23 | } |
JoeShotton | 3:fcd6d70e9694 | 24 | } |
JoeShotton | 4:ea3fa51c4386 | 25 | */ |
JoeShotton | 4:ea3fa51c4386 | 26 | void SnakeEngine::init(){ |
JoeShotton | 4:ea3fa51c4386 | 27 | _food.init(); |
JoeShotton | 4:ea3fa51c4386 | 28 | //_food.rand_pos(_food._x, _food._y); |
JoeShotton | 4:ea3fa51c4386 | 29 | } |
JoeShotton | 4:ea3fa51c4386 | 30 | |
JoeShotton | 4:ea3fa51c4386 | 31 | void SnakeEngine::move_body(Gamepad &pad, N5110 &lcd, bool &death){ |
JoeShotton | 4:ea3fa51c4386 | 32 | _body.snake_movement(pad); |
JoeShotton | 4:ea3fa51c4386 | 33 | _body.draw_body(lcd); |
JoeShotton | 4:ea3fa51c4386 | 34 | //printf("Moving!"); |
JoeShotton | 4:ea3fa51c4386 | 35 | _food.do_food(lcd); |
JoeShotton | 4:ea3fa51c4386 | 36 | snake_food_collision(pad, _body._length); |
JoeShotton | 4:ea3fa51c4386 | 37 | if (_body._state > 0){ |
JoeShotton | 4:ea3fa51c4386 | 38 | _body.snake_snake_collision(pad, death); |
JoeShotton | 4:ea3fa51c4386 | 39 | } |
JoeShotton | 4:ea3fa51c4386 | 40 | } |
JoeShotton | 4:ea3fa51c4386 | 41 | |
JoeShotton | 4:ea3fa51c4386 | 42 | void SnakeEngine::snake_food_collision(Gamepad &pad, int &_length) { |
JoeShotton | 4:ea3fa51c4386 | 43 | if (_food._x == _body._x_head && _food._y == _body._y_head){ |
JoeShotton | 4:ea3fa51c4386 | 44 | //printf("FOOD!"); |
JoeShotton | 4:ea3fa51c4386 | 45 | _food.rand_pos(pad, _food._x, _food._y); |
JoeShotton | 4:ea3fa51c4386 | 46 | pad.led(3,0.9); |
JoeShotton | 4:ea3fa51c4386 | 47 | _length += 5; |
JoeShotton | 4:ea3fa51c4386 | 48 | score++; |
JoeShotton | 4:ea3fa51c4386 | 49 | |
JoeShotton | 4:ea3fa51c4386 | 50 | /* |
JoeShotton | 4:ea3fa51c4386 | 51 | for(int i = 1; i < _length; i++) { |
JoeShotton | 4:ea3fa51c4386 | 52 | if (_x_head == _body_x[i] && _y_head == _body_y[i]) { //checks if head coord is the same as any of the body coords |
JoeShotton | 4:ea3fa51c4386 | 53 | pad.led(1,0.9); |
JoeShotton | 4:ea3fa51c4386 | 54 | //printf("DEAD \n"); |
JoeShotton | 4:ea3fa51c4386 | 55 | _state = 0; |
JoeShotton | 4:ea3fa51c4386 | 56 | death = true; |
JoeShotton | 4:ea3fa51c4386 | 57 | //return true; |
JoeShotton | 4:ea3fa51c4386 | 58 | } else { |
JoeShotton | 4:ea3fa51c4386 | 59 | //pad.led(1,0.0); |
JoeShotton | 4:ea3fa51c4386 | 60 | //printf("ALIVE \n"); |
JoeShotton | 4:ea3fa51c4386 | 61 | //return false; |
JoeShotton | 4:ea3fa51c4386 | 62 | } |
JoeShotton | 4:ea3fa51c4386 | 63 | */ |
JoeShotton | 4:ea3fa51c4386 | 64 | |
JoeShotton | 4:ea3fa51c4386 | 65 | |
JoeShotton | 4:ea3fa51c4386 | 66 | } |
JoeShotton | 4:ea3fa51c4386 | 67 | } |
JoeShotton | 4:ea3fa51c4386 | 68 |