Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
GameObjects/Snake/Snake.cpp@104:17040265b7b4, 2019-05-09 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Thu May 09 14:52:19 2019 +0000
- Revision:
- 104:17040265b7b4
- Parent:
- 87:871d9fecb593
Final Submission. I have read and agreed with Statement of Academic Integrity.
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 | 87:871d9fecb593 | 59 | _x[0] = p.x; //Spawns first of the snake sprite near the middle of the screen. |
AhmedPlaymaker | 87:871d9fecb593 | 60 | _y[0] = p.y; //Spawns first of thesnake sprite near the bottom of the screen. |
AhmedPlaymaker | 68:b9cfd27987ac | 61 | |
AhmedPlaymaker | 87:871d9fecb593 | 62 | for(int i=1; i<=9; i++) { |
AhmedPlaymaker | 87:871d9fecb593 | 63 | _x[i] = _x[i-1]; //Makes them all get spawned at the same x position initially. (copies the previous snake bead) |
AhmedPlaymaker | 87:871d9fecb593 | 64 | _y[i] = _y[i-1] - 3; //Makes them all get spawned 3 sprites appart in the y axis initially. (copies the previous snake bead) |
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 | 87:871d9fecb593 | 72 | for(int i = snakeIndex; i<=9; i++) { |
AhmedPlaymaker | 87:871d9fecb593 | 73 | if(_length == (i+1)) { |
AhmedPlaymaker | 87:871d9fecb593 | 74 | snakepos.x = _x[i - snakeIndex]; //Gets the index from SnakevsBlock.cpp which is basically 0 for the top beed, and 9 for the 10th beed |
AhmedPlaymaker | 87:871d9fecb593 | 75 | snakepos.y = _y[i - snakeIndex]; //and saves it in x[?] and y[?] which is 0 for the bottom beed and higher indexes for higher beeds. |
AhmedPlaymaker | 24:1c118b071430 | 76 | return snakepos; |
AhmedPlaymaker | 24:1c118b071430 | 77 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 78 | } |
AhmedPlaymaker | 41:4edac50f010d | 79 | if(_length >= 10) { |
AhmedPlaymaker | 87:871d9fecb593 | 80 | snakepos.x = _x[9 - snakeIndex]; //we know that at length = 10+ the snake doesn't look any longer, so subtracting the index from 9 will imply |
AhmedPlaymaker | 87:871d9fecb593 | 81 | snakepos.y = _y[9 - snakeIndex]; //the top beed is _x[9], _y[9] and the last beed (index 9) is _x[0], _y[0] |
AhmedPlaymaker | 63:205f0ca48473 | 82 | return snakepos; |
AhmedPlaymaker | 37:ee47699915b8 | 83 | } |
AhmedPlaymaker | 87:871d9fecb593 | 84 | snakepos.x = -100; //to make sure these coordinates do not interfere with any collisions as this is a default value if conditions do not satisfy, |
AhmedPlaymaker | 87:871d9fecb593 | 85 | snakepos.y = -100; //(NULL is in range of collisions in this game and is therefore not used. |
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 | 87:871d9fecb593 | 90 | void Snake::update(Direction d, int* immobile_bead_n) |
AhmedPlaymaker | 13:9785f2404045 | 91 | { |
AhmedPlaymaker | 87:871d9fecb593 | 92 | _d = d; //Saves the direction of movement in a private struct _d from the SnakevsBlock.cpp. |
AhmedPlaymaker | 87:871d9fecb593 | 93 | |
AhmedPlaymaker | 87:871d9fecb593 | 94 | Snake::chainSnakeTogether(immobile_bead_n); //chains the snake beads together in hysteresis. |
AhmedPlaymaker | 71:4bd2b27693f3 | 95 | |
AhmedPlaymaker | 87:871d9fecb593 | 96 | Snake::moveSnake(immobile_bead_n); //makes the top bead in the snake move according to the user input direction. |
AhmedPlaymaker | 63:205f0ca48473 | 97 | |
AhmedPlaymaker | 87:871d9fecb593 | 98 | Snake::_setSnakeLimits(); //makes the snake movement limited to the nokia screen and ensure spawn position is where we expected. |
AhmedPlaymaker | 71:4bd2b27693f3 | 99 | } |
AhmedPlaymaker | 71:4bd2b27693f3 | 100 | |
AhmedPlaymaker | 87:871d9fecb593 | 101 | void Snake::chainSnakeTogether(int* immobile_bead_n) |
AhmedPlaymaker | 71:4bd2b27693f3 | 102 | { |
AhmedPlaymaker | 25:e827f1a8fadc | 103 | //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 | 87:871d9fecb593 | 104 | //the immobile_bead_n[i] makes sure that the snake beed doesn't move if that beed is deactivated by colliding with a barrier. immobile_bead_n[i] also signifies the specific beed number by i. |
AhmedPlaymaker | 79:35cb65c52d25 | 105 | for(int i=1; i<=9; i++) { |
AhmedPlaymaker | 71:4bd2b27693f3 | 106 | if((_length > i)&&(_x[i-1] != _x[i])) { |
AhmedPlaymaker | 87:871d9fecb593 | 107 | if ((_x[i-1] > _x[i])&&(immobile_bead_n[i] == 1)&&(immobile_bead_n[i-1] == 1)) { |
AhmedPlaymaker | 71:4bd2b27693f3 | 108 | _x[i-1]-=_speed; |
AhmedPlaymaker | 8:890b986b16a4 | 109 | } |
AhmedPlaymaker | 87:871d9fecb593 | 110 | if ((_x[i-1] < _x[i])&&(immobile_bead_n[i] == 1)&&(immobile_bead_n[i-1] == 1)) { |
AhmedPlaymaker | 71:4bd2b27693f3 | 111 | _x[i-1]+=_speed; |
AhmedPlaymaker | 22:ee698f66146f | 112 | } |
AhmedPlaymaker | 21:e41126528cc6 | 113 | } |
AhmedPlaymaker | 21:e41126528cc6 | 114 | } |
AhmedPlaymaker | 71:4bd2b27693f3 | 115 | } |
AhmedPlaymaker | 71:4bd2b27693f3 | 116 | |
AhmedPlaymaker | 87:871d9fecb593 | 117 | void Snake::moveSnake(int* immobile_bead_n) |
AhmedPlaymaker | 71:4bd2b27693f3 | 118 | { |
AhmedPlaymaker | 63:205f0ca48473 | 119 | //this makes the controls of W/E directions only exclusive to the top beed in the snake |
AhmedPlaymaker | 79:35cb65c52d25 | 120 | for(int i=0; i<=9; i++) { |
AhmedPlaymaker | 87:871d9fecb593 | 121 | if((_length == i+1)&&(immobile_bead_n[i] == 1)) { |
AhmedPlaymaker | 63:205f0ca48473 | 122 | |
AhmedPlaymaker | 87:871d9fecb593 | 123 | if (_d == E) { |
AhmedPlaymaker | 63:205f0ca48473 | 124 | _x[i]+= _speed; |
AhmedPlaymaker | 22:ee698f66146f | 125 | } |
AhmedPlaymaker | 63:205f0ca48473 | 126 | |
AhmedPlaymaker | 87:871d9fecb593 | 127 | if (_d == W) { |
AhmedPlaymaker | 63:205f0ca48473 | 128 | _x[i]-= _speed; |
AhmedPlaymaker | 63:205f0ca48473 | 129 | } |
AhmedPlaymaker | 63:205f0ca48473 | 130 | |
AhmedPlaymaker | 63:205f0ca48473 | 131 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 132 | } |
AhmedPlaymaker | 71:4bd2b27693f3 | 133 | } |
AhmedPlaymaker | 7:48ba87cd79b5 | 134 | |
AhmedPlaymaker | 71:4bd2b27693f3 | 135 | void Snake::_setSnakeLimits() |
AhmedPlaymaker | 71:4bd2b27693f3 | 136 | { |
AhmedPlaymaker | 71:4bd2b27693f3 | 137 | // the following makes sure that when the length is increased, the snake stays where it was when it ate food. |
AhmedPlaymaker | 7:48ba87cd79b5 | 138 | |
AhmedPlaymaker | 79:35cb65c52d25 | 139 | for(int i=1; i<=9; i++) { |
AhmedPlaymaker | 63:205f0ca48473 | 140 | |
AhmedPlaymaker | 79:35cb65c52d25 | 141 | if(_length < (i+1)) { |
AhmedPlaymaker | 79:35cb65c52d25 | 142 | _x[i] = _x[i-1]; |
AhmedPlaymaker | 63:205f0ca48473 | 143 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 144 | } |
AhmedPlaymaker | 63:205f0ca48473 | 145 | |
AhmedPlaymaker | 25:e827f1a8fadc | 146 | //Limits set so that the snake does not travel off the screen. |
AhmedPlaymaker | 79:35cb65c52d25 | 147 | for(int i=0; i<=9; i++) { |
AhmedPlaymaker | 63:205f0ca48473 | 148 | |
AhmedPlaymaker | 63:205f0ca48473 | 149 | if (_x[i] <= 0) { |
AhmedPlaymaker | 63:205f0ca48473 | 150 | _x[i] = 0; |
AhmedPlaymaker | 63:205f0ca48473 | 151 | } |
AhmedPlaymaker | 63:205f0ca48473 | 152 | |
AhmedPlaymaker | 63:205f0ca48473 | 153 | if (_x[i] > 81) { |
AhmedPlaymaker | 63:205f0ca48473 | 154 | _x[i] = 81; |
AhmedPlaymaker | 63:205f0ca48473 | 155 | } |
AhmedPlaymaker | 8:890b986b16a4 | 156 | } |
AhmedPlaymaker | 70:7caab8069b9b | 157 | } |