![](/media/cache/profiles/LinkedIn_0NghoZJ.jpeg.50x50_q85.jpg)
Snake vs Block Game to be run upon K64F.
GameObjects/Snake/Snake.cpp@77:5c6bd659c32d, 2019-05-05 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Sun May 05 16:02:24 2019 +0000
- Revision:
- 77:5c6bd659c32d
- Parent:
- 71:4bd2b27693f3
- Child:
- 79:35cb65c52d25
now the default motion of the snake is called directly from update, and this doesn't make it dependent on other 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 | 68:b9cfd27987ac | 20 | void Snake::init(int length, int speed) |
AhmedPlaymaker | 7:48ba87cd79b5 | 21 | { |
AhmedPlaymaker | 68:b9cfd27987ac | 22 | _length = length; |
AhmedPlaymaker | 71:4bd2b27693f3 | 23 | if(length >= 10) { |
AhmedPlaymaker | 71:4bd2b27693f3 | 24 | _length = 10; //to stop the snake length virtually at 10 when it goes past it. |
AhmedPlaymaker | 71:4bd2b27693f3 | 25 | } |
AhmedPlaymaker | 71:4bd2b27693f3 | 26 | |
AhmedPlaymaker | 68:b9cfd27987ac | 27 | _speed = speed;// change this according to the options selected |
AhmedPlaymaker | 30:461231877c89 | 28 | m = 0; //Variable used to allow a starting location for the player. |
AhmedPlaymaker | 7:48ba87cd79b5 | 29 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 30 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 31 | |
AhmedPlaymaker | 77:5c6bd659c32d | 32 | void Snake::draw(N5110 &lcd, int length) |
AhmedPlaymaker | 33:249cf423fb18 | 33 | { |
AhmedPlaymaker | 41:4edac50f010d | 34 | _length = length; |
AhmedPlaymaker | 63:205f0ca48473 | 35 | if(m == 0) { |
AhmedPlaymaker | 70:7caab8069b9b | 36 | |
AhmedPlaymaker | 68:b9cfd27987ac | 37 | Vector2D p = {WIDTH/2, HEIGHT - 3}; //Spawns player sprite near the middle of the screen. |
AhmedPlaymaker | 68:b9cfd27987ac | 38 | Snake::set_pos(p); |
AhmedPlaymaker | 63:205f0ca48473 | 39 | |
AhmedPlaymaker | 33:249cf423fb18 | 40 | m = m+1; |
AhmedPlaymaker | 22:ee698f66146f | 41 | } |
AhmedPlaymaker | 63:205f0ca48473 | 42 | //printf("SPRITE %d %d \n", _x[0], _y[0]); |
AhmedPlaymaker | 63:205f0ca48473 | 43 | |
AhmedPlaymaker | 70:7caab8069b9b | 44 | for(int i=0; i<=_length-1; i++) { |
AhmedPlaymaker | 70:7caab8069b9b | 45 | lcd.drawSprite(_x[i],_y[i],3,3,(int *)snake_sprite); //Function used to draw the sprite. |
AhmedPlaymaker | 8:890b986b16a4 | 46 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 47 | } |
AhmedPlaymaker | 19:05cc9f801468 | 48 | |
AhmedPlaymaker | 24:1c118b071430 | 49 | |
AhmedPlaymaker | 68:b9cfd27987ac | 50 | void Snake::set_pos(Vector2D p) |
AhmedPlaymaker | 68:b9cfd27987ac | 51 | { |
AhmedPlaymaker | 68:b9cfd27987ac | 52 | _x[0] = p.x; //Spawns player sprite near the middle of the screen. |
AhmedPlaymaker | 68:b9cfd27987ac | 53 | _y[0] = p.y; |
AhmedPlaymaker | 68:b9cfd27987ac | 54 | |
AhmedPlaymaker | 68:b9cfd27987ac | 55 | for(int i=0; i<=13; i++) { |
AhmedPlaymaker | 68:b9cfd27987ac | 56 | _x[i+1] = _x[i]; |
AhmedPlaymaker | 68:b9cfd27987ac | 57 | _y[i+1] = _y[i] - 3; |
AhmedPlaymaker | 68:b9cfd27987ac | 58 | } |
AhmedPlaymaker | 68:b9cfd27987ac | 59 | } |
AhmedPlaymaker | 68:b9cfd27987ac | 60 | |
AhmedPlaymaker | 70:7caab8069b9b | 61 | Vector2D Snake::get_pos(int snakeIndex) |
AhmedPlaymaker | 7:48ba87cd79b5 | 62 | { |
AhmedPlaymaker | 70:7caab8069b9b | 63 | Vector2D snakepos; //Stores the snake position. |
AhmedPlaymaker | 71:4bd2b27693f3 | 64 | |
AhmedPlaymaker | 70:7caab8069b9b | 65 | for(int i = (snakeIndex + 1); i<=10; i++) { |
AhmedPlaymaker | 41:4edac50f010d | 66 | if(_length == i) { |
AhmedPlaymaker | 70:7caab8069b9b | 67 | snakepos.x = _x[i - (snakeIndex + 1)]; |
AhmedPlaymaker | 70:7caab8069b9b | 68 | snakepos.y = _y[i - (snakeIndex + 1)]; |
AhmedPlaymaker | 24:1c118b071430 | 69 | return snakepos; |
AhmedPlaymaker | 24:1c118b071430 | 70 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 71 | } |
AhmedPlaymaker | 41:4edac50f010d | 72 | if(_length >= 10) { |
AhmedPlaymaker | 70:7caab8069b9b | 73 | snakepos.x = _x[9 - snakeIndex]; |
AhmedPlaymaker | 70:7caab8069b9b | 74 | snakepos.y = _y[9 - snakeIndex]; |
AhmedPlaymaker | 63:205f0ca48473 | 75 | return snakepos; |
AhmedPlaymaker | 37:ee47699915b8 | 76 | } |
AhmedPlaymaker | 70:7caab8069b9b | 77 | snakepos.x = NULL; |
AhmedPlaymaker | 70:7caab8069b9b | 78 | snakepos.y = NULL; |
AhmedPlaymaker | 24:1c118b071430 | 79 | return snakepos; |
AhmedPlaymaker | 7:48ba87cd79b5 | 80 | //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); |
AhmedPlaymaker | 13:9785f2404045 | 81 | } |
AhmedPlaymaker | 13:9785f2404045 | 82 | |
AhmedPlaymaker | 41:4edac50f010d | 83 | void Snake::update(Direction d, int* b) |
AhmedPlaymaker | 13:9785f2404045 | 84 | { |
AhmedPlaymaker | 71:4bd2b27693f3 | 85 | Snake::chainSnakeTogether(b); |
AhmedPlaymaker | 71:4bd2b27693f3 | 86 | |
AhmedPlaymaker | 71:4bd2b27693f3 | 87 | Snake::mooveSnake(d, b); |
AhmedPlaymaker | 63:205f0ca48473 | 88 | |
AhmedPlaymaker | 71:4bd2b27693f3 | 89 | Snake::_setSnakeLimits(); |
AhmedPlaymaker | 71:4bd2b27693f3 | 90 | } |
AhmedPlaymaker | 71:4bd2b27693f3 | 91 | |
AhmedPlaymaker | 71:4bd2b27693f3 | 92 | void Snake::chainSnakeTogether(int* b) |
AhmedPlaymaker | 71:4bd2b27693f3 | 93 | { |
AhmedPlaymaker | 25:e827f1a8fadc | 94 | //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 | 95 | //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 | 71:4bd2b27693f3 | 96 | for(int i=0; i<=14; i++) { |
AhmedPlaymaker | 71:4bd2b27693f3 | 97 | if((_length > i)&&(_x[i-1] != _x[i])) { |
AhmedPlaymaker | 71:4bd2b27693f3 | 98 | if ((_x[i-1] > _x[i])&&(b[i] == 1)&&(b[i-1] == 1)) { |
AhmedPlaymaker | 71:4bd2b27693f3 | 99 | _x[i-1]-=_speed; |
AhmedPlaymaker | 8:890b986b16a4 | 100 | } |
AhmedPlaymaker | 71:4bd2b27693f3 | 101 | if ((_x[i-1] < _x[i])&&(b[i] == 1)&&(b[i-1] == 1)) { |
AhmedPlaymaker | 71:4bd2b27693f3 | 102 | _x[i-1]+=_speed; |
AhmedPlaymaker | 22:ee698f66146f | 103 | } |
AhmedPlaymaker | 21:e41126528cc6 | 104 | } |
AhmedPlaymaker | 21:e41126528cc6 | 105 | } |
AhmedPlaymaker | 71:4bd2b27693f3 | 106 | } |
AhmedPlaymaker | 71:4bd2b27693f3 | 107 | |
AhmedPlaymaker | 71:4bd2b27693f3 | 108 | void Snake::mooveSnake(Direction d, int* b) |
AhmedPlaymaker | 71:4bd2b27693f3 | 109 | { |
AhmedPlaymaker | 63:205f0ca48473 | 110 | //this makes the controls of W/E directions only exclusive to the top beed in the snake |
AhmedPlaymaker | 22:ee698f66146f | 111 | for(int i=14; i>=0; i--) { |
AhmedPlaymaker | 63:205f0ca48473 | 112 | if((_length == i+1)&&(b[i] == 1)) { |
AhmedPlaymaker | 63:205f0ca48473 | 113 | |
AhmedPlaymaker | 63:205f0ca48473 | 114 | if (d == E) { |
AhmedPlaymaker | 63:205f0ca48473 | 115 | _x[i]+= _speed; |
AhmedPlaymaker | 22:ee698f66146f | 116 | } |
AhmedPlaymaker | 63:205f0ca48473 | 117 | |
AhmedPlaymaker | 63:205f0ca48473 | 118 | if (d == W) { |
AhmedPlaymaker | 63:205f0ca48473 | 119 | _x[i]-= _speed; |
AhmedPlaymaker | 63:205f0ca48473 | 120 | } |
AhmedPlaymaker | 63:205f0ca48473 | 121 | |
AhmedPlaymaker | 63:205f0ca48473 | 122 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 123 | } |
AhmedPlaymaker | 71:4bd2b27693f3 | 124 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 125 | |
AhmedPlaymaker | 71:4bd2b27693f3 | 126 | void Snake::_setSnakeLimits() |
AhmedPlaymaker | 71:4bd2b27693f3 | 127 | { |
AhmedPlaymaker | 71:4bd2b27693f3 | 128 | // the following makes sure that when the length is increased, the snake stays where it was when it ate food. |
AhmedPlaymaker | 7:48ba87cd79b5 | 129 | |
AhmedPlaymaker | 32:3a3bdeffdf62 | 130 | for(int i=2; i<=15; i++) { |
AhmedPlaymaker | 63:205f0ca48473 | 131 | |
AhmedPlaymaker | 63:205f0ca48473 | 132 | if(_length < i) { |
AhmedPlaymaker | 63:205f0ca48473 | 133 | _x[i-1] = _x[i-2]; |
AhmedPlaymaker | 63:205f0ca48473 | 134 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 135 | } |
AhmedPlaymaker | 63:205f0ca48473 | 136 | |
AhmedPlaymaker | 25:e827f1a8fadc | 137 | //Limits set so that the snake does not travel off the screen. |
AhmedPlaymaker | 22:ee698f66146f | 138 | for(int i=0; i<=14; i++) { |
AhmedPlaymaker | 63:205f0ca48473 | 139 | |
AhmedPlaymaker | 63:205f0ca48473 | 140 | if (_x[i] <= 0) { |
AhmedPlaymaker | 63:205f0ca48473 | 141 | _x[i] = 0; |
AhmedPlaymaker | 63:205f0ca48473 | 142 | } |
AhmedPlaymaker | 63:205f0ca48473 | 143 | |
AhmedPlaymaker | 63:205f0ca48473 | 144 | if (_x[i] > 81) { |
AhmedPlaymaker | 63:205f0ca48473 | 145 | _x[i] = 81; |
AhmedPlaymaker | 63:205f0ca48473 | 146 | } |
AhmedPlaymaker | 8:890b986b16a4 | 147 | } |
AhmedPlaymaker | 70:7caab8069b9b | 148 | } |