Sam Thys
/
Snake
The game of snake on the LCD of mbed application board, with the joystick as control
Revision 0:b4eddf41d75b, committed 2013-05-13
- Comitter:
- sammekevremde
- Date:
- Mon May 13 10:47:49 2013 +0000
- Commit message:
- The game of snake on the Application board of the mbed.
Changed in this revision
diff -r 000000000000 -r b4eddf41d75b C12832_lcd.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C12832_lcd.lib Mon May 13 10:47:49 2013 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/dreschpe/code/C12832_lcd/#c9afe58d786a
diff -r 000000000000 -r b4eddf41d75b main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon May 13 10:47:49 2013 +0000 @@ -0,0 +1,234 @@ +#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) +{ + +} +} \ No newline at end of file
diff -r 000000000000 -r b4eddf41d75b mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon May 13 10:47:49 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/7e6c9f46b3bd \ No newline at end of file