Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
Snake/Snake.cpp
- Committer:
- AhmedPlaymaker
- Date:
- 2019-03-27
- Revision:
- 9:d1d79d4ee673
- Parent:
- 8:890b986b16a4
- Child:
- 12:1e601b176437
File content as of revision 9:d1d79d4ee673:
#include "Snake.h" Snake::Snake() { } Snake::~Snake() { } //The Snake Sprite. int snake_sprite[3][3] = { {0,1,0}, {1,0,1}, {0,1,0}, }; int m = 0; //Variable used to allow a starting location for the player. void Snake::init() { _speed = 0.15;// change this according to the options selected } void Snake::draw(N5110 &lcd, int length) { if(m == 0){ _x = WIDTH/2; //Spawns player sprite near the middle of the screen. _xa = _x; _xb = _xa; _xc = _xb; _xd = _xc; _xe = _xd; _xf = _xe; _xg = _xf; _xh = _xg; _xi = _xh; _xj = _xi; _xk = _xj; _xl = _xk; _xm = _xl; _xn = _xm; _y = HEIGHT - 3; m = m+1; } //printf("SPRITE %d %d \n", _x, _y); if(length == 0) { NVIC_SystemReset(); //Software Reset. } if(length == 1) { lcd.drawSprite(_x,_y,3,3,(int *)snake_sprite); //Function used to draw the sprite. } if(length == 2) { lcd.drawSprite(_x,_y,3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_xa,_y-3,3,3,(int *)snake_sprite); } if(length == 3) { lcd.drawSprite(_x,_y,3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_xa,_y-3,3,3,(int *)snake_sprite); lcd.drawSprite(_xb,_y-6,3,3,(int *)snake_sprite); } if(length == 4) { lcd.drawSprite(_x,_y,3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_xa,_y-3,3,3,(int *)snake_sprite); lcd.drawSprite(_xb,_y-6,3,3,(int *)snake_sprite); lcd.drawSprite(_xc,_y-9,3,3,(int *)snake_sprite); } if(length == 5) { lcd.drawSprite(_x,_y,3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_xa,_y-3,3,3,(int *)snake_sprite); lcd.drawSprite(_xb,_y-6,3,3,(int *)snake_sprite); lcd.drawSprite(_xc,_y-9,3,3,(int *)snake_sprite); lcd.drawSprite(_xd,_y-12,3,3,(int *)snake_sprite); } if(length == 6) { lcd.drawSprite(_x,_y,3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_xa,_y-3,3,3,(int *)snake_sprite); lcd.drawSprite(_xb,_y-6,3,3,(int *)snake_sprite); lcd.drawSprite(_xc,_y-9,3,3,(int *)snake_sprite); lcd.drawSprite(_xd,_y-12,3,3,(int *)snake_sprite); lcd.drawSprite(_xe,_y-15,3,3,(int *)snake_sprite); } if(length == 7) { lcd.drawSprite(_x,_y,3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_xa,_y-3,3,3,(int *)snake_sprite); lcd.drawSprite(_xb,_y-6,3,3,(int *)snake_sprite); lcd.drawSprite(_xc,_y-9,3,3,(int *)snake_sprite); lcd.drawSprite(_xd,_y-12,3,3,(int *)snake_sprite); lcd.drawSprite(_xe,_y-15,3,3,(int *)snake_sprite); lcd.drawSprite(_xf,_y-18,3,3,(int *)snake_sprite); } if(length == 8) { lcd.drawSprite(_x,_y,3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_xa,_y-3,3,3,(int *)snake_sprite); lcd.drawSprite(_xb,_y-6,3,3,(int *)snake_sprite); lcd.drawSprite(_xc,_y-9,3,3,(int *)snake_sprite); lcd.drawSprite(_xd,_y-12,3,3,(int *)snake_sprite); lcd.drawSprite(_xe,_y-15,3,3,(int *)snake_sprite); lcd.drawSprite(_xf,_y-18,3,3,(int *)snake_sprite); lcd.drawSprite(_xg,_y-21,3,3,(int *)snake_sprite); } if(length == 9) { lcd.drawSprite(_x,_y,3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_xa,_y-3,3,3,(int *)snake_sprite); lcd.drawSprite(_xb,_y-6,3,3,(int *)snake_sprite); lcd.drawSprite(_xc,_y-9,3,3,(int *)snake_sprite); lcd.drawSprite(_xd,_y-12,3,3,(int *)snake_sprite); lcd.drawSprite(_xe,_y-15,3,3,(int *)snake_sprite); lcd.drawSprite(_xf,_y-18,3,3,(int *)snake_sprite); lcd.drawSprite(_xg,_y-21,3,3,(int *)snake_sprite); lcd.drawSprite(_xh,_y-24,3,3,(int *)snake_sprite); } if(length == 10) { lcd.drawSprite(_x,_y,3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_xa,_y-3,3,3,(int *)snake_sprite); lcd.drawSprite(_xb,_y-6,3,3,(int *)snake_sprite); lcd.drawSprite(_xc,_y-9,3,3,(int *)snake_sprite); lcd.drawSprite(_xd,_y-12,3,3,(int *)snake_sprite); lcd.drawSprite(_xe,_y-15,3,3,(int *)snake_sprite); lcd.drawSprite(_xf,_y-18,3,3,(int *)snake_sprite); lcd.drawSprite(_xg,_y-21,3,3,(int *)snake_sprite); lcd.drawSprite(_xh,_y-24,3,3,(int *)snake_sprite); lcd.drawSprite(_xi,_y-27,3,3,(int *)snake_sprite); } if(length == 11) { lcd.drawSprite(_x,_y,3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_xa,_y-3,3,3,(int *)snake_sprite); lcd.drawSprite(_xb,_y-6,3,3,(int *)snake_sprite); lcd.drawSprite(_xc,_y-9,3,3,(int *)snake_sprite); lcd.drawSprite(_xd,_y-12,3,3,(int *)snake_sprite); lcd.drawSprite(_xe,_y-15,3,3,(int *)snake_sprite); lcd.drawSprite(_xf,_y-18,3,3,(int *)snake_sprite); lcd.drawSprite(_xg,_y-21,3,3,(int *)snake_sprite); lcd.drawSprite(_xh,_y-24,3,3,(int *)snake_sprite); lcd.drawSprite(_xi,_y-27,3,3,(int *)snake_sprite); lcd.drawSprite(_xj,_y-30,3,3,(int *)snake_sprite); } if(length == 12) { lcd.drawSprite(_x,_y,3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_xa,_y-3,3,3,(int *)snake_sprite); lcd.drawSprite(_xb,_y-6,3,3,(int *)snake_sprite); lcd.drawSprite(_xc,_y-9,3,3,(int *)snake_sprite); lcd.drawSprite(_xd,_y-12,3,3,(int *)snake_sprite); lcd.drawSprite(_xe,_y-15,3,3,(int *)snake_sprite); lcd.drawSprite(_xf,_y-18,3,3,(int *)snake_sprite); lcd.drawSprite(_xg,_y-21,3,3,(int *)snake_sprite); lcd.drawSprite(_xh,_y-24,3,3,(int *)snake_sprite); lcd.drawSprite(_xi,_y-27,3,3,(int *)snake_sprite); lcd.drawSprite(_xj,_y-30,3,3,(int *)snake_sprite); lcd.drawSprite(_xk,_y-33,3,3,(int *)snake_sprite); } if(length == 13) { lcd.drawSprite(_x,_y,3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_xa,_y-3,3,3,(int *)snake_sprite); lcd.drawSprite(_xb,_y-6,3,3,(int *)snake_sprite); lcd.drawSprite(_xc,_y-9,3,3,(int *)snake_sprite); lcd.drawSprite(_xd,_y-12,3,3,(int *)snake_sprite); lcd.drawSprite(_xe,_y-15,3,3,(int *)snake_sprite); lcd.drawSprite(_xf,_y-18,3,3,(int *)snake_sprite); lcd.drawSprite(_xg,_y-21,3,3,(int *)snake_sprite); lcd.drawSprite(_xh,_y-24,3,3,(int *)snake_sprite); lcd.drawSprite(_xi,_y-27,3,3,(int *)snake_sprite); lcd.drawSprite(_xj,_y-30,3,3,(int *)snake_sprite); lcd.drawSprite(_xk,_y-33,3,3,(int *)snake_sprite); lcd.drawSprite(_xl,_y-36,3,3,(int *)snake_sprite); } if(length == 14) { lcd.drawSprite(_x,_y,3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_xa,_y-3,3,3,(int *)snake_sprite); lcd.drawSprite(_xb,_y-6,3,3,(int *)snake_sprite); lcd.drawSprite(_xc,_y-9,3,3,(int *)snake_sprite); lcd.drawSprite(_xd,_y-12,3,3,(int *)snake_sprite); lcd.drawSprite(_xe,_y-15,3,3,(int *)snake_sprite); lcd.drawSprite(_xf,_y-18,3,3,(int *)snake_sprite); lcd.drawSprite(_xg,_y-21,3,3,(int *)snake_sprite); lcd.drawSprite(_xh,_y-24,3,3,(int *)snake_sprite); lcd.drawSprite(_xi,_y-27,3,3,(int *)snake_sprite); lcd.drawSprite(_xj,_y-30,3,3,(int *)snake_sprite); lcd.drawSprite(_xk,_y-33,3,3,(int *)snake_sprite); lcd.drawSprite(_xl,_y-36,3,3,(int *)snake_sprite); lcd.drawSprite(_xm,_y-39,3,3,(int *)snake_sprite); } if(length == 15) { lcd.drawSprite(_x,_y,3,3,(int *)snake_sprite); //Function used to draw the sprite. lcd.drawSprite(_xa,_y-3,3,3,(int *)snake_sprite); lcd.drawSprite(_xb,_y-6,3,3,(int *)snake_sprite); lcd.drawSprite(_xc,_y-9,3,3,(int *)snake_sprite); lcd.drawSprite(_xd,_y-12,3,3,(int *)snake_sprite); lcd.drawSprite(_xe,_y-15,3,3,(int *)snake_sprite); lcd.drawSprite(_xf,_y-18,3,3,(int *)snake_sprite); lcd.drawSprite(_xg,_y-21,3,3,(int *)snake_sprite); lcd.drawSprite(_xh,_y-24,3,3,(int *)snake_sprite); lcd.drawSprite(_xi,_y-27,3,3,(int *)snake_sprite); lcd.drawSprite(_xj,_y-30,3,3,(int *)snake_sprite); lcd.drawSprite(_xk,_y-33,3,3,(int *)snake_sprite); lcd.drawSprite(_xl,_y-36,3,3,(int *)snake_sprite); lcd.drawSprite(_xm,_y-39,3,3,(int *)snake_sprite); lcd.drawSprite(_xn,_y-42,3,3,(int *)snake_sprite); } } Vector2D Snake::get_pos(int length) { if(length == 1) { Vector2D snakepos = {_x,_y}; //Obtains the snake position. return snakepos; } else if(length == 2) { Vector2D snakepos = {_xa,(_y-3)}; //Obtains the snake position. return snakepos; } else if(length==3) { Vector2D snakepos = {_xb,(_y-6)}; //Obtains the snake position. return snakepos; } else if(length==4) { Vector2D snakepos = {_xc,(_y-9)}; //Obtains the snake position. return snakepos; } else if(length==5) { Vector2D snakepos = {_xd,(_y-12)}; //Obtains the snake position. return snakepos; } else if(length==6) { Vector2D snakepos = {_xe,(_y-15)}; //Obtains the snake position. return snakepos; } else if(length==7) { Vector2D snakepos = {_xf,(_y-18)}; //Obtains the snake position. return snakepos; } else if(length==8) { Vector2D snakepos = {_xg,(_y-21)}; //Obtains the snake position. return snakepos; } else if(length==9) { Vector2D snakepos = {_xh,(_y-24)}; //Obtains the snake position. return snakepos; } else if(length==10) { Vector2D snakepos = {_xi,(_y-27)}; //Obtains the snake position. return snakepos; } else if(length==11) { Vector2D snakepos = {_xj,(_y-30)}; //Obtains the snake position. return snakepos; } else if(length==12) { Vector2D snakepos = {_xk,(_y-33)}; //Obtains the snake position. return snakepos; } else if(length==13) { Vector2D snakepos = {_xl,(_y-36)}; //Obtains the snake position. return snakepos; } else if(length == 14) { Vector2D snakepos = {_xm,(_y-39)}; //Obtains the snake position. return snakepos; } else if(length==15) { Vector2D snakepos = {_xn,(_y-42)}; //Obtains the snake position. return snakepos; } //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); return {0,0}; } void Snake::update(Direction d,float mag,int length) { _speed = 1; //Speed changes depending on how much you push the joystick.(As Of Now) if(length > 1) { if (_x > _xa) { _x-=_speed; } if (_x < _xa) { _x+=_speed; } if(length > 2) { if (_xa > _xb) { _xa-=_speed; } if (_xa < _xb) { _xa+=_speed; } if(length > 3) { if (_xb > _xc) { _xb-=_speed; } if (_xb < _xc) { _xb+=_speed; } if(length > 4) { if (_xc > _xd) { _xc-=_speed; } if (_xc < _xd) { _xc+=_speed; } if(length > 5) { if (_xd > _xe) { _xd-=_speed; } if (_xd < _xe) { _xd+=_speed; } if(length > 6) { if (_xe > _xf) { _xe-=_speed; } if (_xe < _xf) { _xe+=_speed; } if(length > 7) { if (_xf > _xg) { _xf-=_speed; } if (_xf < _xg) { _xf+=_speed; } if(length > 8) { if (_xg > _xh) { _xg-=_speed; } if (_xg < _xh) { _xg+=_speed; } if(length > 9) { if (_xh > _xi) { _xh-=_speed; } if (_xh < _xi) { _xh+=_speed; } if(length > 10) { if (_xi > _xj) { _xi-=_speed; } if (_xi < _xj) { _xi+=_speed; } if(length > 11) { if (_xj > _xk) { _xj-=_speed; } if (_xj < _xk) { _xj+=_speed; } if(length > 12) { if (_xk > _xl) { _xk-=_speed; } if (_xk < _xl) { _xk+=_speed; } if(length > 13) { if (_xl > _xm) { _xl-=_speed; } if (_xl < _xm) { _xl+=_speed; } if(length > 14) { if (_xm > _xn) { _xm-=_speed; } if (_xm < _xn) { _xm+=_speed; } } } } } } } } } } } } } } } // Diagonal speeds are /2 to prevent player from going double the speed. if(length == 15) { if (d == E) { _xn+= _speed; } if (d == W) { _xn-= _speed; } } if(length == 14) { if (d == E) { _xm+= _speed; } if (d == W) { _xm-= _speed; } } if(length == 13) { if (d == E) { _xl+= _speed; } if (d == W) { _xl-= _speed; } } if(length == 12) { if (d == E) { _xk+= _speed; } if (d == W) { _xk-= _speed; } } if(length == 11) { if (d == E) { _xj+= _speed; } if (d == W) { _xj-= _speed; } } if(length == 10) { if (d == E) { _xi+= _speed; } if (d == W) { _xi-= _speed; } } if(length == 9) { if (d == E) { _xh+= _speed; } if (d == W) { _xh-= _speed; } } if(length == 8) { if (d == E) { _xg+= _speed; } if (d == W) { _xg-= _speed; } } if(length == 7) { if (d == E) { _xf+= _speed; } if (d == W) { _xf-= _speed; } } if(length == 6) { if (d == E) { _xe+= _speed; } if (d == W) { _xe-= _speed; } } if(length == 5) { if (d == E) { _xd+= _speed; } if (d == W) { _xd-= _speed; } } if(length == 4) { if (d == E) { _xc+= _speed; } if (d == W) { _xc-= _speed; } } if(length == 3) { if (d == E) { _xb+= _speed; } if (d == W) { _xb-= _speed; } } if(length == 2) { if (d == E) { _xa+= _speed; } if (d == W) { _xa-= _speed; } } if(length == 1) { if (d == E) { _x+= _speed; } if (d == W) { _x-= _speed; } } // the following makes sure that when the length is increased, the snake stays where it was when it ate food. if(length < 2) { _xa = _x; } else if(length < 3) { _xb = _xa; } else if(length < 4) { _xc = _xb; } else if(length < 5) { _xd = _xc; } else if(length < 6) { _xe = _xd; } else if(length < 7) { _xf = _xe; } else if(length < 8) { _xg = _xf; } else if(length < 9) { _xh = _xg; } else if(length < 10) { _xi = _xh; } else if(length < 11) { _xj = _xi; } else if(length < 12) { _xk = _xj; } else if(length < 13) { _xl = _xk; } else if(length < 14) { _xm = _xl; } else if(length < 15) { _xn = _xm; } //Limits set so that the sprite does not travel off the screen. if (_y <= 0) { _y = 0; } if (_x <= 0) { _x = 0; } if (_xa <= 0) { _xa = 0; } if (_xb <= 0) { _xb = 0; } if (_xc <= 0) { _xc = 0; } if (_xd <= 0) { _xd = 0; } if (_xe <= 0) { _xe = 0; } if (_xf <= 0) { _xf = 0; } if (_xg <= 0) { _xg = 0; } if (_xh <= 0) { _xh = 0; } if (_xi <= 0) { _xi = 0; } if (_xj <= 0) { _xj = 0; } if (_xk <= 0) { _xk = 0; } if (_xl <= 0) { _xl = 0; } if (_xm <= 0) { _xm = 0; } if (_xn <= 0) { _xn = 0; } if (_x > 81) { _x = 81; } if (_xa > 81) { _xa = 81; } if (_xb > 81) { _xb = 81; } if (_xc > 81) { _xc = 81; } if (_xd > 81) { _xd = 81; } if (_xe > 81) { _xe = 81; } if (_xf > 81) { _xf = 81; } if (_xg > 81) { _xg = 81; } if (_xh > 81) { _xh = 81; } if (_xi > 81) { _xi = 81; } if (_xj > 81) { _xj = 81; } if (_xk > 81) { _xk = 81; } if (_xl > 81) { _xl = 81; } if (_xm > 81) { _xm = 81; } if (_xn > 81) { _xn = 81; } }