FINAL VERSION

Dependencies:   mbed

Committer:
jamesheavey
Date:
Wed Apr 17 21:19:16 2019 +0000
Revision:
1:e18615d46071
Parent:
0:7d4d2023ed9c
Child:
3:5719d0fe94ed
Added the brick class

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jamesheavey 0:7d4d2023ed9c 1 ///////// pre-processor directives ////////
jamesheavey 0:7d4d2023ed9c 2 #include "mbed.h"
jamesheavey 0:7d4d2023ed9c 3 #include "Gamepad.h"
jamesheavey 0:7d4d2023ed9c 4 #include "N5110.h"
jamesheavey 0:7d4d2023ed9c 5 #include "PongEngine.h"
jamesheavey 0:7d4d2023ed9c 6
jamesheavey 0:7d4d2023ed9c 7 #ifdef WITH_TESTING
jamesheavey 0:7d4d2023ed9c 8 # include "tests.h"
jamesheavey 0:7d4d2023ed9c 9 #endif
jamesheavey 0:7d4d2023ed9c 10
jamesheavey 1:e18615d46071 11 #define PADDLE_WIDTH 12
jamesheavey 0:7d4d2023ed9c 12 #define PADDLE_HEIGHT 2
jamesheavey 0:7d4d2023ed9c 13 #define BALL_SIZE 2
jamesheavey 0:7d4d2023ed9c 14 #define BALL_SPEED 3
jamesheavey 0:7d4d2023ed9c 15
jamesheavey 0:7d4d2023ed9c 16 /////////////// structs /////////////////
jamesheavey 0:7d4d2023ed9c 17 struct UserInput {
jamesheavey 0:7d4d2023ed9c 18 Direction d;
jamesheavey 0:7d4d2023ed9c 19 float mag;
jamesheavey 0:7d4d2023ed9c 20 };
jamesheavey 0:7d4d2023ed9c 21 /////////////// objects ///////////////
jamesheavey 0:7d4d2023ed9c 22 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
jamesheavey 0:7d4d2023ed9c 23 Gamepad pad;
jamesheavey 0:7d4d2023ed9c 24 PongEngine pong;
jamesheavey 0:7d4d2023ed9c 25
jamesheavey 0:7d4d2023ed9c 26 ///////////// prototypes ///////////////
jamesheavey 0:7d4d2023ed9c 27 void init();
jamesheavey 0:7d4d2023ed9c 28 void update_game(UserInput input);
jamesheavey 0:7d4d2023ed9c 29 void render();
jamesheavey 0:7d4d2023ed9c 30 void welcome();
jamesheavey 0:7d4d2023ed9c 31
jamesheavey 0:7d4d2023ed9c 32 ///////////// functions ////////////////
jamesheavey 0:7d4d2023ed9c 33 int main()
jamesheavey 0:7d4d2023ed9c 34 {
jamesheavey 0:7d4d2023ed9c 35 #ifdef WITH_TESTING
jamesheavey 0:7d4d2023ed9c 36 int number_of_failures = run_all_tests();
jamesheavey 0:7d4d2023ed9c 37
jamesheavey 0:7d4d2023ed9c 38 if(number_of_failures > 0) return number_of_failures;
jamesheavey 0:7d4d2023ed9c 39 #endif
jamesheavey 0:7d4d2023ed9c 40
jamesheavey 1:e18615d46071 41 int fps = 10; // frames per second
jamesheavey 0:7d4d2023ed9c 42
jamesheavey 0:7d4d2023ed9c 43 init(); // initialise and then display welcome screen...
jamesheavey 0:7d4d2023ed9c 44 welcome(); // waiting for the user to start
jamesheavey 0:7d4d2023ed9c 45
jamesheavey 0:7d4d2023ed9c 46 render(); // first draw the initial frame
jamesheavey 0:7d4d2023ed9c 47 wait(1.0f/fps); // and wait for one frame period
jamesheavey 0:7d4d2023ed9c 48
jamesheavey 0:7d4d2023ed9c 49
jamesheavey 0:7d4d2023ed9c 50 // game loop - read input, update the game state and render the display
jamesheavey 0:7d4d2023ed9c 51 while (1) {
jamesheavey 0:7d4d2023ed9c 52 pong.read_input(pad);
jamesheavey 0:7d4d2023ed9c 53 pong.update(pad);
jamesheavey 0:7d4d2023ed9c 54 render();
jamesheavey 0:7d4d2023ed9c 55 wait(1.0f/fps);
jamesheavey 0:7d4d2023ed9c 56 }
jamesheavey 0:7d4d2023ed9c 57 }
jamesheavey 0:7d4d2023ed9c 58
jamesheavey 0:7d4d2023ed9c 59 // initialies all classes and libraries
jamesheavey 0:7d4d2023ed9c 60 void init()
jamesheavey 0:7d4d2023ed9c 61 {
jamesheavey 0:7d4d2023ed9c 62 // need to initialise LCD and Gamepad
jamesheavey 0:7d4d2023ed9c 63 lcd.init();
jamesheavey 0:7d4d2023ed9c 64 pad.init();
jamesheavey 0:7d4d2023ed9c 65
jamesheavey 0:7d4d2023ed9c 66 // initialise the game with correct ball and paddle sizes
jamesheavey 0:7d4d2023ed9c 67 pong.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_SIZE,BALL_SPEED);
jamesheavey 0:7d4d2023ed9c 68
jamesheavey 0:7d4d2023ed9c 69 }
jamesheavey 0:7d4d2023ed9c 70
jamesheavey 0:7d4d2023ed9c 71 // this function draws each frame on the LCD
jamesheavey 0:7d4d2023ed9c 72 void render()
jamesheavey 0:7d4d2023ed9c 73 {
jamesheavey 0:7d4d2023ed9c 74 // clear screen, re-draw and refresh
jamesheavey 0:7d4d2023ed9c 75 lcd.clear();
jamesheavey 0:7d4d2023ed9c 76 pong.draw(lcd);
jamesheavey 0:7d4d2023ed9c 77 lcd.refresh();
jamesheavey 0:7d4d2023ed9c 78 }
jamesheavey 0:7d4d2023ed9c 79
jamesheavey 0:7d4d2023ed9c 80 // simple splash screen displayed on start-up
jamesheavey 0:7d4d2023ed9c 81 void welcome() {
jamesheavey 0:7d4d2023ed9c 82
jamesheavey 0:7d4d2023ed9c 83 lcd.printString(" BREAKOUT ",0,1);
jamesheavey 0:7d4d2023ed9c 84 lcd.printString(" Press Start ",0,4);
jamesheavey 0:7d4d2023ed9c 85 lcd.refresh();
jamesheavey 0:7d4d2023ed9c 86
jamesheavey 0:7d4d2023ed9c 87 // wait flashing LEDs until start button is pressed
jamesheavey 0:7d4d2023ed9c 88 while ( pad.check_event(Gamepad::START_PRESSED) == false) {
jamesheavey 0:7d4d2023ed9c 89 pad.leds_on();
jamesheavey 0:7d4d2023ed9c 90 wait(0.1);
jamesheavey 0:7d4d2023ed9c 91 pad.leds_off();
jamesheavey 0:7d4d2023ed9c 92 wait(0.1);
jamesheavey 0:7d4d2023ed9c 93 }
jamesheavey 0:7d4d2023ed9c 94
jamesheavey 0:7d4d2023ed9c 95 }