Pong for Gamepad2

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 ///////// pre-processor directives ////////
00002 #include "mbed.h"
00003 #include "Gamepad.h"
00004 #include "N5110.h"
00005 #include "PongEngine.h"
00006 
00007 #ifdef WITH_TESTING
00008 # include "tests.h"
00009 #endif
00010 
00011 #define PADDLE_WIDTH 2
00012 #define PADDLE_HEIGHT 8
00013 #define BALL_SIZE 2
00014 #define BALL_SPEED 3
00015 
00016 /////////////// structs /////////////////
00017 struct UserInput {
00018     Direction d;
00019     float mag;
00020 };
00021 /////////////// objects ///////////////
00022 N5110 lcd;
00023 Gamepad pad;
00024 PongEngine pong;
00025 
00026 ///////////// prototypes ///////////////
00027 void init();
00028 void update_game(UserInput input);
00029 void render();
00030 void welcome();
00031 
00032 ///////////// functions ////////////////
00033 int main()
00034 {
00035 #ifdef WITH_TESTING
00036     int number_of_failures = run_all_tests();
00037 
00038     if(number_of_failures > 0) return number_of_failures;
00039 #endif
00040 
00041     int fps = 6;  // frames per second
00042 
00043     init();     // initialise and then display welcome screen...
00044     welcome();  // waiting for the user to start
00045     
00046     render();  // first draw the initial frame 
00047     wait(1.0f/fps);  // and wait for one frame period
00048 
00049 
00050     // game loop - read input, update the game state and render the display
00051     while (1) {
00052         pong.read_input(pad);
00053         pong.update(pad);
00054         render();
00055         wait(1.0f/fps);
00056     }
00057 }
00058 
00059 // initialies all classes and libraries
00060 void init()
00061 {
00062     // need to initialise LCD and Gamepad 
00063     lcd.init();
00064     pad.init();
00065      
00066     // initialise the game with correct ball and paddle sizes
00067     pong.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_SIZE,BALL_SPEED);
00068 
00069 }
00070 
00071 // this function draws each frame on the LCD
00072 void render()
00073 {
00074     // clear screen, re-draw and refresh
00075     lcd.clear();  
00076     pong.draw(lcd);
00077     lcd.refresh();
00078 }
00079 
00080 // simple splash screen displayed on start-up
00081 void welcome() {
00082     
00083     lcd.printString("     Pong!    ",0,1);  
00084     lcd.printString("  Press Start ",0,4);
00085     lcd.refresh();
00086      
00087     // wait flashing LEDs until start button is pressed 
00088     while ( pad.start_pressed() == false) {
00089         lcd.setContrast( pad.read_pot1());
00090         pad.leds_on();
00091         wait(0.1);
00092         pad.leds_off();
00093         wait(0.1);
00094     }
00095  
00096 }