Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
GameObjects/Snake/Snake.cpp
- Committer:
- AhmedPlaymaker
- Date:
- 2019-05-05
- Revision:
- 77:5c6bd659c32d
- Parent:
- 71:4bd2b27693f3
- Child:
- 79:35cb65c52d25
File content as of revision 77:5c6bd659c32d:
#include "Snake.h" Snake::Snake() { } Snake::~Snake() { } //The Snake Sprite. int snake_sprite[3][3] = { {0,1,0}, {1,0,1}, {0,1,0}, }; void Snake::init(int length, int speed) { _length = length; if(length >= 10) { _length = 10; //to stop the snake length virtually at 10 when it goes past it. } _speed = speed;// change this according to the options selected m = 0; //Variable used to allow a starting location for the player. } void Snake::draw(N5110 &lcd, int length) { _length = length; if(m == 0) { Vector2D p = {WIDTH/2, HEIGHT - 3}; //Spawns player sprite near the middle of the screen. Snake::set_pos(p); m = m+1; } //printf("SPRITE %d %d \n", _x[0], _y[0]); for(int i=0; i<=_length-1; i++) { lcd.drawSprite(_x[i],_y[i],3,3,(int *)snake_sprite); //Function used to draw the sprite. } } void Snake::set_pos(Vector2D p) { _x[0] = p.x; //Spawns player sprite near the middle of the screen. _y[0] = p.y; for(int i=0; i<=13; i++) { _x[i+1] = _x[i]; _y[i+1] = _y[i] - 3; } } Vector2D Snake::get_pos(int snakeIndex) { Vector2D snakepos; //Stores the snake position. for(int i = (snakeIndex + 1); i<=10; i++) { if(_length == i) { snakepos.x = _x[i - (snakeIndex + 1)]; snakepos.y = _y[i - (snakeIndex + 1)]; return snakepos; } } if(_length >= 10) { snakepos.x = _x[9 - snakeIndex]; snakepos.y = _y[9 - snakeIndex]; return snakepos; } snakepos.x = NULL; snakepos.y = NULL; return snakepos; //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); } void Snake::update(Direction d, int* b) { Snake::chainSnakeTogether(b); Snake::mooveSnake(d, b); Snake::_setSnakeLimits(); } void Snake::chainSnakeTogether(int* b) { //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 //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. for(int i=0; i<=14; i++) { if((_length > i)&&(_x[i-1] != _x[i])) { if ((_x[i-1] > _x[i])&&(b[i] == 1)&&(b[i-1] == 1)) { _x[i-1]-=_speed; } if ((_x[i-1] < _x[i])&&(b[i] == 1)&&(b[i-1] == 1)) { _x[i-1]+=_speed; } } } } void Snake::mooveSnake(Direction d, int* b) { //this makes the controls of W/E directions only exclusive to the top beed in the snake for(int i=14; i>=0; i--) { if((_length == i+1)&&(b[i] == 1)) { if (d == E) { _x[i]+= _speed; } if (d == W) { _x[i]-= _speed; } } } } void Snake::_setSnakeLimits() { // the following makes sure that when the length is increased, the snake stays where it was when it ate food. for(int i=2; i<=15; i++) { if(_length < i) { _x[i-1] = _x[i-2]; } } //Limits set so that the snake does not travel off the screen. for(int i=0; i<=14; i++) { if (_x[i] <= 0) { _x[i] = 0; } if (_x[i] > 81) { _x[i] = 81; } } }