![](/media/cache/profiles/LinkedIn_0NghoZJ.jpeg.50x50_q85.jpg)
Snake vs Block Game to be run upon K64F.
Diff: Snake/Snake.cpp
- Revision:
- 18:b391caa5754c
- Parent:
- 17:54e13faeaf64
- Child:
- 19:05cc9f801468
diff -r 54e13faeaf64 -r b391caa5754c Snake/Snake.cpp --- a/Snake/Snake.cpp Fri Apr 05 06:28:13 2019 +0000 +++ b/Snake/Snake.cpp Mon Apr 08 05:57:04 2019 +0000 @@ -23,7 +23,7 @@ void Snake::init() { - _speed = 0.15;// change this according to the options selected + _speed = 1;// change this according to the options selected } @@ -60,7 +60,22 @@ _yk = _y-33; _yl = _y-36; _ym = _y-39; - _yn = _y-42; + _yn = _y-42; + b0 = 1; + b1 = 1; + b2 = 1; + b3 = 1; + b4 = 1; + b5 = 1; + b6 = 1; + b7 = 1; + b8 = 1; + b9 = 1; + b10 = 1; + b11 = 1; + b12 = 1; + b13 = 1; + b14 = 1; m = m+1; } //printf("SPRITE %d %d \n", _x, _y); @@ -234,9 +249,72 @@ } return length; } - - - +/* +//helps to set the position automatically when we want to restrict it's movement when coliding with lines. +void Snake::set_pos(Vector2D s) +{ + if(length == 1) { + _fx = s.x; + _fy = s.y; + } + else if(length == 2) { + _xa = s.x; + _ya = s.y; + } + else if(length==3) { + _xb = s.x; + _yb = s.y; + } + else if(length==4) { + _xc = s.x; + _yc = s.y; + } + else if(length==5) { + _xd = s.x; + _yd = s.y; + } + else if(length==6) { + _xe = s.x; + _ye = s.y; + } + else if(length==7) { + _xf = s.x; + _yf = s.y; + } + else if(length==8) { + _xg = s.x; + _yg = s.y; + } + else if(length==9) { + _xh = s.x; + _yh = s.y; + } + else if(length==10) { + _xi = s.x; + _yi = s.y; + } + else if(length==11) { + _xj = s.x; + _yj = s.y; + } + else if(length==12) { + _xk = s.x; + _yk = s.y; + } + else if(length==13) { + _xl = s.x; + _yl = s.y; + } + else if(length == 14) { + _xm = s.x; + _ym = s.y; + } + else if(length==15) { + _xn = s.x; + _yn = s.y; + } +} +*/ Vector2D Snake::get_pos(int length) { if(length == 1) { @@ -1272,176 +1350,125 @@ } } -Vector2D Snake::get_pos_before15(int length) //this sends data of following sprites. -{ - if(length == 1) { - Vector2D snakepos_b15 = {-20,-20}; //Obtains the snake position. - return snakepos_b15; - } - else if(length == 2) { - Vector2D snakepos_b15 = {-20,-20}; //Obtains the snake position. - return snakepos_b15; - } - else if(length == 3) { - Vector2D snakepos_b15 = {-20,-20}; //Obtains the snake position. - return snakepos_b15; - } - else if(length == 4) { - Vector2D snakepos_b15 = {-20,-20}; //Obtains the snake position. - return snakepos_b15; - } - else if(length == 5) { - Vector2D snakepos_b15 = {-20,-20}; //Obtains the snake position. - return snakepos_b15; - } - else if(length == 6) { - Vector2D snakepos_b15 = {-20,-20}; //Obtains the snake position. - return snakepos_b15; - } - else if(length == 7) { - Vector2D snakepos_b15 = {-20,-20}; //Obtains the snake position. - return snakepos_b15; - } - else if(length == 8) { - Vector2D snakepos_b15 = {-20,-20}; //Obtains the snake position. - return snakepos_b15; - } - else if(length == 9) { - Vector2D snakepos_b15 = {-20,-20}; //Obtains the snake position. - return snakepos_b15; - } - else if(length == 10) { - Vector2D snakepos_b15 = {-20,-20}; //Obtains the snake position. - return snakepos_b15; - } - else if(length == 11) { - Vector2D snakepos_b15 = {-20,-20}; //Obtains the snake position. - return snakepos_b15; - } - else if(length == 12) { - Vector2D snakepos_b15 = {-20,-20}; //Obtains the snake position. - return snakepos_b15; - } - else if(length == 13) { - Vector2D snakepos_b15 = {-20,-20}; //Obtains the snake position. - return snakepos_b15; - } - else if(length == 14) { - Vector2D snakepos_b15 = {-20,-20}; //Obtains the snake position. - return snakepos_b15; - } - else if(length == 15) { - Vector2D snakepos_b15 = {-20,-20}; //Obtains the snake position. - return snakepos_b15; - } - //printf("snakepos from player = %f %f \n", snakepos.x, snakepos.y); - else { - Vector2D snakepos_b15 = {-20,-20}; //Obtains the snake position. - return snakepos_b15; - } -} + -void Snake::update(Direction d,float mag,int length, int speed) +void Snake::update(Direction d,float mag,int length, int speed, int* b0_to_b14) { _speed = speed; //Speed changes depending on how much you push the joystick.(As Of Now) + b0 = b0_to_b14[0]; + b1 = b0_to_b14[1]; + b2 = b0_to_b14[2]; + b3 = b0_to_b14[3]; + b4 = b0_to_b14[4]; + b5 = b0_to_b14[5]; + b6 = b0_to_b14[6]; + b7 = b0_to_b14[7]; + b8 = b0_to_b14[8]; + b9 = b0_to_b14[9]; + b10 = b0_to_b14[10]; + b11 = b0_to_b14[11]; + b12 = b0_to_b14[12]; + b13 = b0_to_b14[13]; + b14 = b0_to_b14[14]; if(length > 1) { - if (_x > _xa) { + if ((_x > _xa)&&(b1 == 1)&&(b0 == 1)) { _x-=_speed; } - if (_x < _xa) { + if ((_x < _xa)&&(b1 == 1)&&(b0 == 1)) { _x+=_speed; } if(length > 2) { - if (_xa > _xb) { + if ((_xa > _xb)&&(b2 == 1)&&(b1 == 1)) { _xa-=_speed; } - if (_xa < _xb) { + if ((_xa < _xb)&&(b2 == 1)&&(b1 == 1)) { _xa+=_speed; } if(length > 3) { - if (_xb > _xc) { + if ((_xb > _xc)&&(b3 == 1)&&(b2 == 1)) { _xb-=_speed; } - if (_xb < _xc) { + if ((_xb < _xc)&&(b3 == 1)&&(b2 == 1)) { _xb+=_speed; } if(length > 4) { - if (_xc > _xd) { + if ((_xc > _xd)&&(b4 == 1)&&(b3 == 1)) { _xc-=_speed; } - if (_xc < _xd) { + if ((_xc < _xd)&&(b4 == 1)&&(b3 == 1)) { _xc+=_speed; } if(length > 5) { - if (_xd > _xe) { + if ((_xd > _xe)&&(b5 == 1)&&(b4 == 1)) { _xd-=_speed; } - if (_xd < _xe) { + if ((_xd < _xe)&&(b5 == 1)&&(b4 == 1)) { _xd+=_speed; } if(length > 6) { - if (_xe > _xf) { + if ((_xe > _xf)&&(b6 == 1)&&(b5 == 1)) { _xe-=_speed; } - if (_xe < _xf) { + if ((_xe < _xf)&&(b6 == 1)&&(b5 == 1)) { _xe+=_speed; } if(length > 7) { - if (_xf > _xg) { + if ((_xf > _xg)&&(b7 == 1)&&(b6 == 1)) { _xf-=_speed; } - if (_xf < _xg) { + if ((_xf < _xg)&&(b7 == 1)&&(b6 == 1)) { _xf+=_speed; } if(length > 8) { - if (_xg > _xh) { + if ((_xg > _xh)&&(b8 == 1)&&(b7 == 1)) { _xg-=_speed; } - if (_xg < _xh) { + if ((_xg < _xh)&&(b8 == 1)&&(b7 == 1)) { _xg+=_speed; } if(length > 9) { - if (_xh > _xi) { + if ((_xh > _xi)&&(b9 == 1)&&(b8 == 1)) { _xh-=_speed; } - if (_xh < _xi) { + if ((_xh < _xi)&&(b9 == 1)&&(b8 == 1)) { _xh+=_speed; } if(length > 10) { - if (_xi > _xj) { + if ((_xi > _xj)&&(b10 == 1)&&(b9 == 1)) { _xi-=_speed; } - if (_xi < _xj) { + if ((_xi < _xj)&&(b10 == 1)&&(b9 == 1)) { _xi+=_speed; } if(length > 11) { - if (_xj > _xk) { + if ((_xj > _xk)&&(b11 == 1)&&(b10 == 1)) { _xj-=_speed; } - if (_xj < _xk) { + if ((_xj < _xk)&&(b11 == 1)&&(b10 == 1)) { _xj+=_speed; } if(length > 12) { - if (_xk > _xl) { + if ((_xk > _xl)&&(b12 == 1)&&(b11 == 1)) { _xk-=_speed; } - if (_xk < _xl) { + if ((_xk < _xl)&&(b12 == 1)&&(b11 == 1)) { _xk+=_speed; } if(length > 13) { - if (_xl > _xm) { + if ((_xl > _xm)&&(b13 == 1)&&(b12 == 1)) { _xl-=_speed; } - if (_xl < _xm) { + if ((_xl < _xm)&&(b13 == 1)&&(b12 == 1)) { _xl+=_speed; } if(length > 14) { - if (_xm > _xn) { + if ((_xm > _xn)&&(b14 == 1)&&(b13 == 1)) { _xm-=_speed; } - if (_xm < _xn) { - _xm+=_speed; + if ((_xm < _xn)&&(b14 == 1)&&(b13 == 1)) { + _xm+=_speed; + } } } @@ -1457,9 +1484,7 @@ } } } - - // Diagonal speeds are /2 to prevent player from going double the speed. - if(length == 15) { + if((length == 15)&&(b14 == 1)) { if (d == E) { _xn+= _speed; } @@ -1467,7 +1492,7 @@ _xn-= _speed; } } - if(length == 14) { + if((length == 14)&&(b13 == 1)) { if (d == E) { _xm+= _speed; } @@ -1475,7 +1500,7 @@ _xm-= _speed; } } - if(length == 13) { + if((length == 13)&&(b12 == 1)) { if (d == E) { _xl+= _speed; } @@ -1483,7 +1508,7 @@ _xl-= _speed; } } - if(length == 12) { + if((length == 12)&&(b11 == 1)) { if (d == E) { _xk+= _speed; } @@ -1491,7 +1516,7 @@ _xk-= _speed; } } - if(length == 11) { + if((length == 11)&&(b10 == 1)) { if (d == E) { _xj+= _speed; } @@ -1499,7 +1524,7 @@ _xj-= _speed; } } - if(length == 10) { + if((length == 10)&&(b9 == 1)) { if (d == E) { _xi+= _speed; } @@ -1507,7 +1532,7 @@ _xi-= _speed; } } - if(length == 9) { + if((length == 9)&&(b8 == 1)) { if (d == E) { _xh+= _speed; } @@ -1515,7 +1540,7 @@ _xh-= _speed; } } - if(length == 8) { + if((length == 8)&&(b7 == 1)) { if (d == E) { _xg+= _speed; } @@ -1523,7 +1548,7 @@ _xg-= _speed; } } - if(length == 7) { + if((length == 7)&&(b6 == 1)) { if (d == E) { _xf+= _speed; } @@ -1531,7 +1556,7 @@ _xf-= _speed; } } - if(length == 6) { + if((length == 6)&&(b5 == 1)) { if (d == E) { _xe+= _speed; } @@ -1539,7 +1564,7 @@ _xe-= _speed; } } - if(length == 5) { + if((length == 5)&&(b4 == 1)) { if (d == E) { _xd+= _speed; } @@ -1547,7 +1572,7 @@ _xd-= _speed; } } - if(length == 4) { + if((length == 4)&&(b3 == 1)) { if (d == E) { _xc+= _speed; } @@ -1555,7 +1580,7 @@ _xc-= _speed; } } - if(length == 3) { + if((length == 3)&&(b2 == 1)) { if (d == E) { _xb+= _speed; } @@ -1563,7 +1588,7 @@ _xb-= _speed; } } - if(length == 2) { + if((length == 2)&&(b1 == 1)) { if (d == E) { _xa+= _speed; } @@ -1571,7 +1596,7 @@ _xa-= _speed; } } - if(length == 1) { + if((length == 1)&&(b0 == 1)) { if (d == E) { _x+= _speed; } @@ -1609,7 +1634,7 @@ else if(length < 10) { _xi = _xh; } - else if(length < 11) { + else if(length < 11) { _xj = _xi; } else if(length < 12) {