![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Owen Cavender 201159294
Diff: main.cpp
- Revision:
- 14:7fb3c93343b6
- Parent:
- 12:60c856354406
- Child:
- 16:9500059ad5d8
--- a/main.cpp Fri May 29 16:27:29 2020 +0000 +++ b/main.cpp Sat May 30 02:31:43 2020 +0000 @@ -11,7 +11,8 @@ The game loop will be entered after the game is initialised. The first step is to process the user input (if any). The next step is to update the game apple depending on the input i.e. -moving a character or checking for collisions. The final step is to update the display and render +moving a character or checking for collisions. The final step is to update the display and + this on the LCD. There is usual//// */ @@ -22,6 +23,7 @@ #include "snake.h" #include "GameEngine.h" +//write spec for game @@ -29,19 +31,13 @@ Gamepad pad; Snake snake; N5110 lcd; -Ticker ticker; GameEngine engine; + //functions - void welcome(); void render(); void init(); -void update_game_state(); -void gameover_true(); -void ticker_isr(); - -int countdown; - +void update_snake(); //Serial pc(USBTX, USBRX); @@ -49,33 +45,30 @@ int main() { - int fps = 6; + //int fps = 6; init(); // frames per second welcome(); + // initialise and then display welcome screen... - // and wait for one frame period - render(); - //ROB atm the code works to here - //need to draw an initial snake - while (1) { - update_game_state(); + update_snake(); snake.check_gameover(lcd); - snake.gameover_true(lcd); + snake.gameover_true(lcd, pad); + snake.apple_collected(lcd, pad); snake.get_Apple_position(lcd); + engine.get_LEDs(pad, snake); - + engine.print_countdown(lcd, snake); snake.render(lcd); - engine.print_countdown(lcd, snake); - // snake.print_display_time(lcd); + // snake.print_display_time(lcd); - wait(1.0f/6); + wait(0.01); } } @@ -84,54 +77,61 @@ void init() { - snake.init(); //need to initialise snake class + lcd.init(); pad.init(); - lcd.init(); + snake.init(); //need to initialise snake class + + } -void update_game_state() +void update_snake() { - snake.get_direction(pad); - snake.render_clear_tail(lcd); + snake.get_direction(pad); //sets the direction based on buttons pressed on the Gamepad + snake.render_clear_tail(lcd); //clears the trailing pixel of the snake before updating position to avoid the snake growing with each move - the trailing pixel would be locked and fixed as 1 in its initial position without this // =needs to be cleared before _x3, _y3 is updated - snake.move_snake(); - engine.get_LEDs(pad, snake); + snake.move_snake(); //alters the coordinate of each snake bit in accordance with the Gamepad instruction + engine.get_LEDs(pad, snake); //Turns on on LEDS based on where the snake is in the box } void welcome() { - lcd.printString(" SNAKE ",0,1); - // lcd.printString(" Press Start ",0,4); + while ( pad.start_pressed() == false) { + lcd.printString(" SNAKE ",0,1); + lcd.printString("COLLECT APPLES",0,3); + lcd.printString(" Press Start ",0,5); lcd.setContrast( pad.read_pot1()); - pad.leds_on(); - wait(0.1); - pad.leds_off(); - wait(0.1); - } - lcd.refresh(); -} -void render() -{ - lcd.clear(); - lcd.drawRect(0, 0, 84, 42, FILL_TRANSPARENT); - lcd.printString(" GO! ", 0,25); - wait(1.0); + pad.leds_off(); + pad.led(1,1); + wait(0.2); + pad.led(1,0); //turning on the LEDS in a sequence which reflects the snakes movement + pad.led(2,1); + wait (0.2); + pad.led(2,0); + pad.led(3,1); + wait(0.2); + pad.led(3,0); + wait(0.1); + pad.led(6,1); + wait (0.2); + pad.led(6,0); + pad.led(5,1); + wait (0.2); + pad.led(5,0); + pad.led(4,1); + wait(0.2); + + lcd.refresh(); + } } -//MAKE TIMER IN ONE CLASS SNAKE -//SPAWN APPLE -//MOVE THE SNAKE --- -// - - //has an apple been collected? if so increase length by 1 increase score by 1 and makes coin collecting noise and spawn new apple //has snake touched itself or wall - if so end game - present score - make xxx sound fail --- apple needs to be different to the snakes body and different to wall