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@6:6c9453397f4a, 2020-05-23 (annotated)
- Committer:
- JoeShotton
- Date:
- Sat May 23 20:01:00 2020 +0000
- Revision:
- 6:6c9453397f4a
- Parent:
- 5:06fa7674622a
- Child:
- 7:dd84e0fab346
Implemented screen transition function
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 | 5:06fa7674622a | 26 | void SnakeEngine::init(FXOS8700CQ &mag){ |
JoeShotton | 5:06fa7674622a | 27 | _food.init(mag); |
JoeShotton | 6:6c9453397f4a | 28 | _body.init(); |
JoeShotton | 4:ea3fa51c4386 | 29 | //_food.rand_pos(_food._x, _food._y); |
JoeShotton | 4:ea3fa51c4386 | 30 | } |
JoeShotton | 4:ea3fa51c4386 | 31 | |
JoeShotton | 4:ea3fa51c4386 | 32 | void SnakeEngine::move_body(Gamepad &pad, N5110 &lcd, bool &death){ |
JoeShotton | 4:ea3fa51c4386 | 33 | _body.snake_movement(pad); |
JoeShotton | 4:ea3fa51c4386 | 34 | _body.draw_body(lcd); |
JoeShotton | 4:ea3fa51c4386 | 35 | //printf("Moving!"); |
JoeShotton | 4:ea3fa51c4386 | 36 | _food.do_food(lcd); |
JoeShotton | 4:ea3fa51c4386 | 37 | snake_food_collision(pad, _body._length); |
JoeShotton | 4:ea3fa51c4386 | 38 | if (_body._state > 0){ |
JoeShotton | 4:ea3fa51c4386 | 39 | _body.snake_snake_collision(pad, death); |
JoeShotton | 4:ea3fa51c4386 | 40 | } |
JoeShotton | 4:ea3fa51c4386 | 41 | } |
JoeShotton | 4:ea3fa51c4386 | 42 | |
JoeShotton | 4:ea3fa51c4386 | 43 | void SnakeEngine::snake_food_collision(Gamepad &pad, int &_length) { |
JoeShotton | 4:ea3fa51c4386 | 44 | if (_food._x == _body._x_head && _food._y == _body._y_head){ |
JoeShotton | 4:ea3fa51c4386 | 45 | //printf("FOOD!"); |
JoeShotton | 4:ea3fa51c4386 | 46 | _food.rand_pos(pad, _food._x, _food._y); |
JoeShotton | 4:ea3fa51c4386 | 47 | pad.led(3,0.9); |
JoeShotton | 4:ea3fa51c4386 | 48 | _length += 5; |
JoeShotton | 4:ea3fa51c4386 | 49 | score++; |
JoeShotton | 4:ea3fa51c4386 | 50 | } |
JoeShotton | 4:ea3fa51c4386 | 51 | } |
JoeShotton | 4:ea3fa51c4386 | 52 | |
JoeShotton | 6:6c9453397f4a | 53 | void SnakeEngine::transition_black(N5110 &lcd) { |
JoeShotton | 6:6c9453397f4a | 54 | //lcd.clear(); |
JoeShotton | 6:6c9453397f4a | 55 | for (int j = 0; j < 21; j += 4) { |
JoeShotton | 6:6c9453397f4a | 56 | printf("j: %d\n", j); |
JoeShotton | 6:6c9453397f4a | 57 | for (int i = 1; i < 84 - (2*j); i += 2) { |
JoeShotton | 6:6c9453397f4a | 58 | lcd.drawRect(j, j, i + 1, 4, FILL_BLACK); |
JoeShotton | 6:6c9453397f4a | 59 | lcd.drawRect(83 - j - i, 44 - j, i + 1, 4, FILL_BLACK); |
JoeShotton | 6:6c9453397f4a | 60 | wait_ms(5); |
JoeShotton | 6:6c9453397f4a | 61 | lcd.refresh(); |
JoeShotton | 6:6c9453397f4a | 62 | } |
JoeShotton | 6:6c9453397f4a | 63 | for (int i = 1; i < 43 - (2*j); i += 2) { |
JoeShotton | 6:6c9453397f4a | 64 | lcd.drawRect(80 - j, 4 + j, 4, i,FILL_BLACK); |
JoeShotton | 6:6c9453397f4a | 65 | lcd.drawRect(j, 44 - i - j, 4, i,FILL_BLACK); |
JoeShotton | 6:6c9453397f4a | 66 | wait_ms(5); |
JoeShotton | 6:6c9453397f4a | 67 | lcd.refresh(); |
JoeShotton | 6:6c9453397f4a | 68 | } |
JoeShotton | 6:6c9453397f4a | 69 | } |
JoeShotton | 6:6c9453397f4a | 70 | wait_ms(500); |
JoeShotton | 6:6c9453397f4a | 71 | } |
JoeShotton | 6:6c9453397f4a | 72 | |
JoeShotton | 6:6c9453397f4a | 73 | void SnakeEngine::transition_white(N5110 &lcd) { |
JoeShotton | 6:6c9453397f4a | 74 | //lcd.clear(); |
JoeShotton | 6:6c9453397f4a | 75 | for (int j = 0; j < 21; j += 4) { |
JoeShotton | 6:6c9453397f4a | 76 | printf("j: %d\n", j); |
JoeShotton | 6:6c9453397f4a | 77 | for (int i = 1; i < 84 - (2*j); i += 2) { |
JoeShotton | 6:6c9453397f4a | 78 | lcd.drawRect(j, j, i + 1, 4, FILL_WHITE); |
JoeShotton | 6:6c9453397f4a | 79 | lcd.drawRect(83 - j - i, 44 - j, i + 1, 4, FILL_WHITE); |
JoeShotton | 6:6c9453397f4a | 80 | wait_ms(5); |
JoeShotton | 6:6c9453397f4a | 81 | lcd.refresh(); |
JoeShotton | 6:6c9453397f4a | 82 | } |
JoeShotton | 6:6c9453397f4a | 83 | for (int i = 1; i < 43 - (2*j); i += 2) { |
JoeShotton | 6:6c9453397f4a | 84 | lcd.drawRect(80 - j, 4 + j, 4, i,FILL_WHITE); |
JoeShotton | 6:6c9453397f4a | 85 | lcd.drawRect(j, 44 - i - j, 4, i,FILL_WHITE); |
JoeShotton | 6:6c9453397f4a | 86 | wait_ms(5); |
JoeShotton | 6:6c9453397f4a | 87 | lcd.refresh(); |
JoeShotton | 6:6c9453397f4a | 88 | } |
JoeShotton | 6:6c9453397f4a | 89 | } |
JoeShotton | 6:6c9453397f4a | 90 | //wait_ms(500); |
JoeShotton | 6:6c9453397f4a | 91 | } |