Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
GameObjects/Snake/Snake.cpp@83:329da564799a, 2019-05-06 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Mon May 06 14:28:35 2019 +0000
- Revision:
- 83:329da564799a
- Parent:
- 81:4c1641e10dcd
- Child:
- 87:871d9fecb593
Now, I have put gamepad and lcd objects from the main() in a pointer, so that i can declare them in init() in every class i use them in and use them as global objects for each class, without having to send their addresses to all of the functions.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AhmedPlaymaker | 7:48ba87cd79b5 | 1 | #include "Snake.h" |
AhmedPlaymaker | 7:48ba87cd79b5 | 2 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 3 | Snake::Snake() |
AhmedPlaymaker | 7:48ba87cd79b5 | 4 | { |
AhmedPlaymaker | 7:48ba87cd79b5 | 5 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 6 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 7 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 8 | Snake::~Snake() |
AhmedPlaymaker | 7:48ba87cd79b5 | 9 | { |
AhmedPlaymaker | 7:48ba87cd79b5 | 10 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 11 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 12 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 13 | //The Snake Sprite. |
AhmedPlaymaker | 8:890b986b16a4 | 14 | int snake_sprite[3][3] = { |
AhmedPlaymaker | 8:890b986b16a4 | 15 | {0,1,0}, |
AhmedPlaymaker | 8:890b986b16a4 | 16 | {1,0,1}, |
AhmedPlaymaker | 8:890b986b16a4 | 17 | {0,1,0}, |
AhmedPlaymaker | 9:d1d79d4ee673 | 18 | }; |
AhmedPlaymaker | 7:48ba87cd79b5 | 19 | |
AhmedPlaymaker | 83:329da564799a | 20 | void Snake::init(N5110 *lcd) |
AhmedPlaymaker | 83:329da564799a | 21 | { |
AhmedPlaymaker | 83:329da564799a | 22 | _lcd = lcd; |
AhmedPlaymaker | 83:329da564799a | 23 | _speed = 1; |
AhmedPlaymaker | 83:329da564799a | 24 | reset = 0; //Variable used to allow a starting location for the player. |
AhmedPlaymaker | 83:329da564799a | 25 | } |
AhmedPlaymaker | 83:329da564799a | 26 | |
AhmedPlaymaker | 83:329da564799a | 27 | void Snake::_setLength(int length) |
AhmedPlaymaker | 7:48ba87cd79b5 | 28 | { |
AhmedPlaymaker | 68:b9cfd27987ac | 29 | _length = length; |
AhmedPlaymaker | 71:4bd2b27693f3 | 30 | if(length >= 10) { |
AhmedPlaymaker | 71:4bd2b27693f3 | 31 | _length = 10; //to stop the snake length virtually at 10 when it goes past it. |
AhmedPlaymaker | 71:4bd2b27693f3 | 32 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 33 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 34 | |
AhmedPlaymaker | 83:329da564799a | 35 | void Snake::_setSpeed(int speed) |
AhmedPlaymaker | 83:329da564799a | 36 | { |
AhmedPlaymaker | 83:329da564799a | 37 | _speed = speed; |
AhmedPlaymaker | 83:329da564799a | 38 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 39 | |
AhmedPlaymaker | 83:329da564799a | 40 | void Snake::draw() |
AhmedPlaymaker | 33:249cf423fb18 | 41 | { |
AhmedPlaymaker | 81:4c1641e10dcd | 42 | if(reset == 0) { |
AhmedPlaymaker | 70:7caab8069b9b | 43 | |
AhmedPlaymaker | 68:b9cfd27987ac | 44 | Vector2D p = {WIDTH/2, HEIGHT - 3}; //Spawns player sprite near the middle of the screen. |
AhmedPlaymaker | 68:b9cfd27987ac | 45 | Snake::set_pos(p); |
AhmedPlaymaker | 63:205f0ca48473 | 46 | |
AhmedPlaymaker | 81:4c1641e10dcd | 47 | reset = reset+1; |
AhmedPlaymaker | 22:ee698f66146f | 48 | } |
AhmedPlaymaker | 63:205f0ca48473 | 49 | //printf("SPRITE %d %d \n", _x[0], _y[0]); |
AhmedPlaymaker | 63:205f0ca48473 | 50 | |
AhmedPlaymaker | 70:7caab8069b9b | 51 | for(int i=0; i<=_length-1; i++) { |
AhmedPlaymaker | 83:329da564799a | 52 | _lcd->drawSprite(_x[i],_y[i],3,3,(int *)snake_sprite); //Function used to draw the sprite. |
AhmedPlaymaker | 8:890b986b16a4 | 53 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 54 | } |
AhmedPlaymaker | 19:05cc9f801468 | 55 | |
AhmedPlaymaker | 24:1c118b071430 | 56 | |
AhmedPlaymaker | 68:b9cfd27987ac | 57 | void Snake::set_pos(Vector2D p) |
AhmedPlaymaker | 68:b9cfd27987ac | 58 | { |
AhmedPlaymaker | 68:b9cfd27987ac | 59 | _x[0] = p.x; //Spawns player sprite near the middle of the screen. |
AhmedPlaymaker | 68:b9cfd27987ac | 60 | _y[0] = p.y; |
AhmedPlaymaker | 68:b9cfd27987ac | 61 | |
AhmedPlaymaker | 79:35cb65c52d25 | 62 | for(int i=0; i<=8; i++) { |
AhmedPlaymaker | 68:b9cfd27987ac | 63 | _x[i+1] = _x[i]; |
AhmedPlaymaker | 68:b9cfd27987ac | 64 | _y[i+1] = _y[i] - 3; |
AhmedPlaymaker | 68:b9cfd27987ac | 65 | } |
AhmedPlaymaker | 68:b9cfd27987ac | 66 | } |
AhmedPlaymaker | 68:b9cfd27987ac | 67 | |
AhmedPlaymaker | 70:7caab8069b9b | 68 | Vector2D Snake::get_pos(int snakeIndex) |
AhmedPlaymaker | 7:48ba87cd79b5 | 69 | { |
AhmedPlaymaker | 70:7caab8069b9b | 70 | Vector2D snakepos; //Stores the snake position. |
AhmedPlaymaker | 71:4bd2b27693f3 | 71 | |
AhmedPlaymaker | 70:7caab8069b9b | 72 | for(int i = (snakeIndex + 1); i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 73 | if(_length == i) { |
AhmedPlaymaker | 70:7caab8069b9b | 74 | snakepos.x = _x[i - (snakeIndex + 1)]; |
AhmedPlaymaker | 70:7caab8069b9b | 75 | snakepos.y = _y[i - (snakeIndex + 1)]; |
AhmedPlaymaker | 24:1c118b071430 | 76 | return snakepos; |
AhmedPlaymaker | 24:1c118b071430 | 77 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 78 | } |
AhmedPlaymaker | 41:4edac50f010d | 79 | if(_length >= 10) { |
AhmedPlaymaker | 70:7caab8069b9b | 80 | snakepos.x = _x[9 - snakeIndex]; |
AhmedPlaymaker | 70:7caab8069b9b | 81 | snakepos.y = _y[9 - snakeIndex]; |
AhmedPlaymaker | 63:205f0ca48473 | 82 | return snakepos; |
AhmedPlaymaker | 37:ee47699915b8 | 83 | } |
AhmedPlaymaker | 70:7caab8069b9b | 84 | snakepos.x = NULL; |
AhmedPlaymaker | 70:7caab8069b9b | 85 | snakepos.y = NULL; |
AhmedPlaymaker | 24:1c118b071430 | 86 | return snakepos; |
AhmedPlaymaker | 7:48ba87cd79b5 | 87 | //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); |
AhmedPlaymaker | 13:9785f2404045 | 88 | } |
AhmedPlaymaker | 13:9785f2404045 | 89 | |
AhmedPlaymaker | 41:4edac50f010d | 90 | void Snake::update(Direction d, int* b) |
AhmedPlaymaker | 13:9785f2404045 | 91 | { |
AhmedPlaymaker | 71:4bd2b27693f3 | 92 | Snake::chainSnakeTogether(b); |
AhmedPlaymaker | 71:4bd2b27693f3 | 93 | |
AhmedPlaymaker | 71:4bd2b27693f3 | 94 | Snake::mooveSnake(d, b); |
AhmedPlaymaker | 63:205f0ca48473 | 95 | |
AhmedPlaymaker | 71:4bd2b27693f3 | 96 | Snake::_setSnakeLimits(); |
AhmedPlaymaker | 71:4bd2b27693f3 | 97 | } |
AhmedPlaymaker | 71:4bd2b27693f3 | 98 | |
AhmedPlaymaker | 71:4bd2b27693f3 | 99 | void Snake::chainSnakeTogether(int* b) |
AhmedPlaymaker | 71:4bd2b27693f3 | 100 | { |
AhmedPlaymaker | 25:e827f1a8fadc | 101 | //this makes all of the snake beeds chained together by making the lower ones drag towards where the top one was in the previous loop |
AhmedPlaymaker | 43:233f93860d08 | 102 | //the b[i] makes sure that the snake beed doesn't move if that beed is deactivated by colliding with a barrier. b[i] also signifies the specific beed number by i. |
AhmedPlaymaker | 79:35cb65c52d25 | 103 | for(int i=1; i<=9; i++) { |
AhmedPlaymaker | 71:4bd2b27693f3 | 104 | if((_length > i)&&(_x[i-1] != _x[i])) { |
AhmedPlaymaker | 71:4bd2b27693f3 | 105 | if ((_x[i-1] > _x[i])&&(b[i] == 1)&&(b[i-1] == 1)) { |
AhmedPlaymaker | 71:4bd2b27693f3 | 106 | _x[i-1]-=_speed; |
AhmedPlaymaker | 8:890b986b16a4 | 107 | } |
AhmedPlaymaker | 71:4bd2b27693f3 | 108 | if ((_x[i-1] < _x[i])&&(b[i] == 1)&&(b[i-1] == 1)) { |
AhmedPlaymaker | 71:4bd2b27693f3 | 109 | _x[i-1]+=_speed; |
AhmedPlaymaker | 22:ee698f66146f | 110 | } |
AhmedPlaymaker | 21:e41126528cc6 | 111 | } |
AhmedPlaymaker | 21:e41126528cc6 | 112 | } |
AhmedPlaymaker | 71:4bd2b27693f3 | 113 | } |
AhmedPlaymaker | 71:4bd2b27693f3 | 114 | |
AhmedPlaymaker | 71:4bd2b27693f3 | 115 | void Snake::mooveSnake(Direction d, int* b) |
AhmedPlaymaker | 71:4bd2b27693f3 | 116 | { |
AhmedPlaymaker | 63:205f0ca48473 | 117 | //this makes the controls of W/E directions only exclusive to the top beed in the snake |
AhmedPlaymaker | 79:35cb65c52d25 | 118 | for(int i=0; i<=9; i++) { |
AhmedPlaymaker | 63:205f0ca48473 | 119 | if((_length == i+1)&&(b[i] == 1)) { |
AhmedPlaymaker | 63:205f0ca48473 | 120 | |
AhmedPlaymaker | 63:205f0ca48473 | 121 | if (d == E) { |
AhmedPlaymaker | 63:205f0ca48473 | 122 | _x[i]+= _speed; |
AhmedPlaymaker | 22:ee698f66146f | 123 | } |
AhmedPlaymaker | 63:205f0ca48473 | 124 | |
AhmedPlaymaker | 63:205f0ca48473 | 125 | if (d == W) { |
AhmedPlaymaker | 63:205f0ca48473 | 126 | _x[i]-= _speed; |
AhmedPlaymaker | 63:205f0ca48473 | 127 | } |
AhmedPlaymaker | 63:205f0ca48473 | 128 | |
AhmedPlaymaker | 63:205f0ca48473 | 129 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 130 | } |
AhmedPlaymaker | 71:4bd2b27693f3 | 131 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 132 | |
AhmedPlaymaker | 71:4bd2b27693f3 | 133 | void Snake::_setSnakeLimits() |
AhmedPlaymaker | 71:4bd2b27693f3 | 134 | { |
AhmedPlaymaker | 71:4bd2b27693f3 | 135 | // the following makes sure that when the length is increased, the snake stays where it was when it ate food. |
AhmedPlaymaker | 7:48ba87cd79b5 | 136 | |
AhmedPlaymaker | 79:35cb65c52d25 | 137 | for(int i=1; i<=9; i++) { |
AhmedPlaymaker | 63:205f0ca48473 | 138 | |
AhmedPlaymaker | 79:35cb65c52d25 | 139 | if(_length < (i+1)) { |
AhmedPlaymaker | 79:35cb65c52d25 | 140 | _x[i] = _x[i-1]; |
AhmedPlaymaker | 63:205f0ca48473 | 141 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 142 | } |
AhmedPlaymaker | 63:205f0ca48473 | 143 | |
AhmedPlaymaker | 25:e827f1a8fadc | 144 | //Limits set so that the snake does not travel off the screen. |
AhmedPlaymaker | 79:35cb65c52d25 | 145 | for(int i=0; i<=9; i++) { |
AhmedPlaymaker | 63:205f0ca48473 | 146 | |
AhmedPlaymaker | 63:205f0ca48473 | 147 | if (_x[i] <= 0) { |
AhmedPlaymaker | 63:205f0ca48473 | 148 | _x[i] = 0; |
AhmedPlaymaker | 63:205f0ca48473 | 149 | } |
AhmedPlaymaker | 63:205f0ca48473 | 150 | |
AhmedPlaymaker | 63:205f0ca48473 | 151 | if (_x[i] > 81) { |
AhmedPlaymaker | 63:205f0ca48473 | 152 | _x[i] = 81; |
AhmedPlaymaker | 63:205f0ca48473 | 153 | } |
AhmedPlaymaker | 8:890b986b16a4 | 154 | } |
AhmedPlaymaker | 70:7caab8069b9b | 155 | } |