Riad_ Bajraktarevic
/
Snake_Game
11_01_2020
Snake.cpp
- Committer:
- riad
- Date:
- 2020-01-11
- Revision:
- 0:0e46824d32a0
File content as of revision 0:0e46824d32a0:
#include "mbed.h" #include "C12832_lcd.h" C12832_LCD lcd; Ticker move; //-- Variable -- enum richting { omhoog, omlaag, links, rechts, null}; enum gamemode { start,stop,run,pauze}; richting huidigeRichting; richting vorigeRichting; gamemode game; // -- Classes -- struct block { int x; int y; }; //--------------------- struct block snakeHead; struct block fruit; int aantalFruits=0; //block snakeBody[5]; block* snakeBody=new block[aantalFruits+5];//=new block[aantalFruits+5]; // -- I/O setting -- InterruptIn up(p12); InterruptIn down(p15); InterruptIn left (p13); InterruptIn right(p16); InterruptIn center(p14); // -- Functies -- void setFruit() { fruit.x = rand() % 126 +1; fruit.y = rand() % 30 +1; } void richting_omhoog() { if ( huidigeRichting!=omlaag) huidigeRichting=omhoog; } void richting_omlaag() { if ( huidigeRichting!=omhoog) huidigeRichting=omlaag; } void richting_links() { if ( huidigeRichting!=rechts) huidigeRichting=links; } void richting_rechts() { if ( huidigeRichting!=links) huidigeRichting=rechts; } void center_knop() { if (game==pauze) { huidigeRichting=vorigeRichting; game=run; } else if (game==run) { vorigeRichting=huidigeRichting; huidigeRichting=null; game=pauze; } else if (game==stop) { for (int i=0;i<=4;i++) { snakeBody[i].x=(snakeHead.x)-(i+1); snakeBody[i].y=15; } huidigeRichting=null; game=run; } } void moveSnake() { if (game==run) { if (huidigeRichting!=null) { for (int j=(aantalFruits+4);j>=1;j--) { snakeBody[j]=snakeBody[j-1]; } snakeBody[0]=snakeHead; } switch (huidigeRichting) { case omhoog: snakeHead.y+=1; break; case omlaag: snakeHead.y-=1; break; case links: snakeHead.x-=1; break; case rechts: snakeHead.x+=1; break; } if ((snakeHead.y == 0 ||snakeHead.y == 31 ||snakeHead.x == 127 ||snakeHead.x == 0) ) { lcd.cls(); lcd.locate(25,5); lcd.printf("Game Over!!"); lcd.locate(25,15); lcd.printf("U score is %d",aantalFruits); huidigeRichting=null; snakeHead.x=15; snakeHead.y=15; game=stop; aantalFruits=0; } else if ((snakeHead.y == fruit.y )&& (snakeHead.x == fruit.x) ) { aantalFruits+=1; /* for (int j=0;j<=(aantalFruits+4);j++) { snakeBody[j+1]=snakeBody[j+1]; } snakeBody[0]=snakeHead; switch(huidigeRichting) { case omhoog: snakeHead.y+=1; break; case omlaag: snakeHead.y-=1; break; case links: snakeHead.x+=1; break; case rechts: snakeHead.x-=1; break; } */ setFruit(); } else { lcd.cls(); lcd.pixel(snakeHead.x,snakeHead.y,1); lcd.copy_to_lcd(); for(int k=0;k<=(aantalFruits+4);k++) { lcd.pixel(snakeBody[k].x,snakeBody[k].y,1); } lcd.copy_to_lcd(); lcd.pixel(fruit.x,fruit.y,1); lcd.copy_to_lcd(); lcd.rect(0,0, 127, 31, 1); lcd.copy_to_lcd(); } for (int a=0;a<=(aantalFruits+4);a++) { if ( snakeBody[a].x==snakeHead.x && snakeBody[a].y==snakeHead.y) { lcd.cls(); lcd.locate(25,5); lcd.printf("Game Over!!"); lcd.locate(25,15); lcd.printf("U score is %d",aantalFruits); huidigeRichting=null; snakeHead.x=15; snakeHead.y=15; game=stop; aantalFruits=0; } } } else if (game==pauze) { lcd.cls(); lcd.locate(30,15); lcd.printf("Pauze"); } } //template <class T> //-------------------------------- int main() { game=run; lcd.rect(0, 0, 127, 31, 1); lcd.copy_to_lcd(); huidigeRichting=null; snakeHead.x=15; snakeHead.y=15; for (int i=0;i<=4;i++) { snakeBody[i].x=(snakeHead.x)-(i+1); snakeBody[i].y=15; } setFruit(); move.attach(&moveSnake, 0.1); up.rise(&richting_omhoog); down.rise(&richting_omlaag); left.rise(&richting_links); right.rise(&richting_rechts); center.rise(¢er_knop); while (1) { } }