Balint Bogdan 2645 project, 200966741
Snake.cpp
- Committer:
- Nefos
- Date:
- 2017-05-05
- Revision:
- 1:93a4cb86f100
- Parent:
- 0:2eb4d7e86e2f
- Child:
- 2:5e04e34a3b66
File content as of revision 1:93a4cb86f100:
#include "Snake.h" //Snake snakePart; snakePart snek; Snake::Snake() { } Snake::~Snake() { //delete things create aboce in construcxtor } void Snake::init(int x, int y, int lenght){ //initalizing the starting variables snek._x[0]=x; snek._y[0]=y; _length = lenght; counter=0; for (int i=0;_length>i;i++) { snek._x[i]=x+i; snek._y[i]=y; snek._dir[i]=1; } snek._x[_length]=x+_length; snek._y[_length]=y; /* snek._y[0]=y; snek._y[1]=y+1; snek._y[2]=y+2; snek._y[3]=y+2; snek._y[4]=y+3; snek._y[5]=y+3; snek._y[6]=y+4;*/ _direction = 1;//1 is East, 2 is South, 3 is West, 4 is North //putting food at middle of the map before randomizing printf("xog is %d ", snek._x[_length-1]); //printf("initalized"); } void Snake::draw(N5110 &lcd){ lcd.clear(); lcd.drawRect(0,4,84,48-8,FILL_TRANSPARENT); for ( int i=0; _length>i;i++) { if (snek._x!=0) { if (snek._y!=0) { lcd.setPixel(snek._x[i],snek._y[i]); } } //draw the snake array //draw lines from breakpoint to breakpoint to head //draw food //lcd.setPixel(48,28); lcd.refresh(); //printf("drAwn"); } } void Snake::update(Gamepad &pad){ d=pad.get_direction(); startx=snek._x[0]; counter=_length-2; printf("x is %d ", snek._x[_length-1]); if (snek._dir[_length-1]==1) { snek._x[_length]++; startx=0; //snek._dir[counter]=1; //counter--; //if (counter>0) //{counter--;} } if (snek._dir[_length-1]==2) { snek._y[_length]--; //snek._dir[counter]=2; //counter--; //if (i>0) //{counter--;} } if (snek._dir[_length-1]==3) { snek._x[_length]--; //snek._dir[counter]=2; //counter--; //if (i>0) //{counter--;} } if (snek._dir[_length-1]==4) { snek._y[_length]++; //snek._dir[counter]=2; //counter--; //if (i>0) //{counter--;} } for (int i=0 ;_length<i ;i++) { //printf(" x = %d", snek._x[i]); snek._x[i]=snek._x[i+1]; snek._y[i]=snek._y[i+1]; //printf("done"); } /* //snek._x[i]=snek._x[i+1]; // snek._y[i]=snek._y[i+1]; switch (snek._dir[i]) { case 1: snek._x[i]++; case 4: snek._y[i]--; } */ //check dpad which way it is pointing //set direction accordingly, 1 is right, up is 2, 3 is left and 4 is down //set a breakpoint at head by snakeX/Y[i] //switch case to make sure direction is good //if head == food, _length++, and -direction we add 1 length // if head==wall game over //if (counter>0) // {counter--;} if (d==N) { if (snek._dir[_length]!=4) { snek._dir[_length-1] = 2; //snek._y[5]--; //return; } } if (d==E) { if (snek._dir[_length]!=3) { snek._dir[_length-1] = 1; //snek._y[5]--; //return; } } if (d==W) { if (snek._dir[_length]!=1) { snek._dir[_length-1] = 3; //snek._y[5]--; //return; } } if (d==S) { if (snek._dir[_length]!=2) { snek._dir[_length-1] = 4; //snek._y[5]--; //return; } } /*switch (d) { case N: /*case E: if (snek._dir[_length]!=3) { snek._dir[_length-1] = 1; //snek._y[5]--; //return; }*/ // } //counter=0; printf("updated "); } snakePart Snake::add_point(int x, int y){ //snek._x=x; //snek._y=y; return; //_length++; } void Snake::get_length(){ //return _length; }