Pong for Gamepad2

Dependencies:   mbed

Edit.

Committer:
eencae
Date:
Fri Jan 31 12:32:38 2020 +0000
Revision:
0:7423345f87c5
Pong ported to Gamepad2;

Who changed what in which revision?

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