Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Joystick N5110 SDFileSystem beep fsmMenu mbed
Fork of SnakeProjectRev1 by
Diff: main.cpp
- Revision:
- 20:f634b1060981
- Parent:
- 19:8907a82ebe09
- Child:
- 21:e03461ea23e9
--- a/main.cpp Wed May 04 19:20:40 2016 +0000 +++ b/main.cpp Thu May 05 13:31:19 2016 +0000 @@ -44,7 +44,7 @@ greenLed = 1; redLed = 0; lcd.init(); - //snakeIntro(); + snakeIntro(); gamePlaying = false; button.rise(&buttonISR); gameTicker.attach(&timer_isr,0.1); @@ -58,6 +58,115 @@ } +void mainGame() { + + if(gameType == classicMode) { + hardWall(); + i = 41; + j = 23; + } + else if(gameType == infiniteMode) { + wrapAround(); + i = 40; + j = 22; + } + else { + specialMap(); + i = 13; + j = 5; + } + generateFood(); + initSnakeTail(); + snakeTailLength = 3; + score = 0; + fruitValue = 10; + pauseCount = 0; // init everything + + while(gamePlaying == true) { + + lcd.setBrightness(1-pot); // turn pot right for brightness + + if (buttonFlag ==1) { + buttonFlag = 0; + if (pauseCount < 3) { + gamePaused(); + } + buttonFlag = 0; + } + buttonFlag = 0; + + if(game_timer_flag) { + game_timer_flag = 0; + gameLogic(); + } + if (printFlag) { // if flag set, clear flag and print joystick values to serial port + printFlag = 0; + moveSnake(); + } + sleep(); // put the MCU to sleep until an interrupt wakes it up + } +} + +void mainMenu() { + + while(1) { + + lcd.printString("Classic",0,1); + lcd.printString("Infinite",0,3); + lcd.printString("Hard Map",0,5); + + if (rb_flag == 1) { + rb_flag = 0; + gamePlaying = true; + mainGame(); + } + + // check if flag i.e. interrupt has occured + if (printFlag ==1) { + printFlag = 0; // if it has, clear the flag + + // swap direction when button has been pressed + // (could just use ! but want this to be explicit to aid understanding) + if (joystick.direction == CENTRE) { + serial.printf(" CENTRE\n"); + menuDirection = menuSTOP; + } + else if (joystick.direction == UP) { + serial.printf(" UP\n"); + menuDirection = menuUP; + buzzer.beep(2000,0.2); + } + else if (joystick.direction == DOWN) { + serial.printf(" DOWN\n"); + menuDirection = menuDOWN; + buzzer.beep(2000,0.2); + } + } + + + menuFSM(); + + + wait(0.2); // small delay + if (state ==0) { + lcd.clear(); + lcd.printString("*",70,1); + gameType = classicMode; + } + else if (state ==1) { + lcd.clear(); + lcd.printString("*",70,3); + gameType = infiniteMode; + } + else if (state ==2) { + lcd.clear(); + lcd.printString("*",70,5); + gameType = hardMode; + } + + } + +} void generateFood() { @@ -200,6 +309,8 @@ j == 0 || j+1 == 0 || j == 47 || j+1 == 47) { + i = 41; + j = 23; gameOver(); } @@ -271,26 +382,31 @@ gamePlaying = true; mainGame(); } + rb_flag = 0; if (lb_flag == 1) { lb_flag = 0; gamePlaying = false; mainMenu(); - } + } sleep(); } - } void specialMap() { lcd.drawRect(0,0,83,47,0); + lcd.drawRect(15,9,1,31,1); + lcd.drawRect(33,9,1,31,1); + lcd.drawRect(49,9,1,31,1); lcd.refresh(); if (i == 0 || i+1 == 0 || i == 83 || i+1 == 83 || // if any of the 4 pixels within the snake head touch the border j == 0 || j+1 == 0 || j == 47 || j+1 == 47) { + i = 13; + j = 5; gameOver(); } @@ -346,6 +462,8 @@ prev2_j = 5; } + snakeTailX[0] = i; + snakeTailY[0] = j; snakeTailX[1] = prev_i; snakeTailY[1] = prev_j; snakeTailX[2] = prev2_i; @@ -392,7 +510,6 @@ wait_ms(100); pauseCount++; - //gamePlaying = false; lcd.clear(); char buffer[14]; int length = sprintf(buffer,"%i left",3-pauseCount); // number of pauses left @@ -412,114 +529,6 @@ } -void mainGame() { - - if(gameType == classicMode) { - hardWall(); - i = 41; - j = 23; - } - else if(gameType == infiniteMode) { - wrapAround(); - i = 40; - j = 22; - } - else { - specialMap(); - i = 13; - j = 5; - } - generateFood(); - initSnakeTail(); - snakeTailLength = 3; - score = 0; - fruitValue = 10; - pauseCount = 0; // init everything - - while(gamePlaying == true) { - - lcd.setBrightness(1-pot); // turn pot right for brightness - - if (buttonFlag ==1) { - buttonFlag = 0; - if (pauseCount < 3) { - gamePaused(); - } - } - - if(game_timer_flag) { - game_timer_flag = 0; - gameLogic(); - } - if (printFlag) { // if flag set, clear flag and print joystick values to serial port - printFlag = 0; - moveSnake(); - } - sleep(); // put the MCU to sleep until an interrupt wakes it up - } -} - -void mainMenu() { - - while(1) { - - lcd.printString("Classic",0,1); - lcd.printString("Infinite",0,3); - lcd.printString("Hard Map",0,5); - - if (rb_flag == 1) { - rb_flag = 0; - gamePlaying = true; - mainGame(); - } - - // check if flag i.e. interrupt has occured - if (printFlag ==1) { - printFlag = 0; // if it has, clear the flag - - // swap direction when button has been pressed - // (could just use ! but want this to be explicit to aid understanding) - if (joystick.direction == CENTRE) { - serial.printf(" CENTRE\n"); - menuDirection = menuSTOP; - } - else if (joystick.direction == UP) { - serial.printf(" UP\n"); - menuDirection = menuUP; - buzzer.beep(2000,0.2); - } - else if (joystick.direction == DOWN) { - serial.printf(" DOWN\n"); - menuDirection = menuDOWN; - buzzer.beep(2000,0.2); - } - } - - - menuFSM(); - - - wait(0.2); // small delay - if (state ==0) { - lcd.clear(); - lcd.printString("*",70,1); - gameType = classicMode; - } - else if (state ==1) { - lcd.clear(); - lcd.printString("*",70,3); - gameType = infiniteMode; - } - else if (state ==2) { - lcd.clear(); - lcd.printString("*",70,5); - gameType = hardMode; - } - - } - -} - void timer_isr() { game_timer_flag = 1;