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: mbed
main.cpp@1:c4e8e32f91ac, 2019-05-09 (annotated)
- Committer:
- fusihui
- Date:
- Thu May 09 14:03:04 2019 +0000
- Revision:
- 1:c4e8e32f91ac
- Parent:
- 0:19e5a063d7ea
pinball
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fusihui | 0:19e5a063d7ea | 1 | ///////// pre-processor directives //////// |
fusihui | 0:19e5a063d7ea | 2 | #include "mbed.h" |
fusihui | 0:19e5a063d7ea | 3 | #include "Gamepad.h" |
fusihui | 0:19e5a063d7ea | 4 | #include "N5110.h" |
fusihui | 0:19e5a063d7ea | 5 | #include "Engine.h" |
fusihui | 0:19e5a063d7ea | 6 | |
fusihui | 0:19e5a063d7ea | 7 | |
fusihui | 0:19e5a063d7ea | 8 | #define BALL_SIZE 4 |
fusihui | 0:19e5a063d7ea | 9 | #define BALL_SPEED 3 |
fusihui | 0:19e5a063d7ea | 10 | #define PADDLE_HEIGHT 4 |
fusihui | 0:19e5a063d7ea | 11 | #define PADDLE_WIDTH 12 |
fusihui | 0:19e5a063d7ea | 12 | #define BLOCK_SIZE 8 |
fusihui | 0:19e5a063d7ea | 13 | |
fusihui | 1:c4e8e32f91ac | 14 | /////////////// structs ///////////////// |
fusihui | 0:19e5a063d7ea | 15 | struct UserInput |
fusihui | 0:19e5a063d7ea | 16 | { |
fusihui | 0:19e5a063d7ea | 17 | Direction d; |
fusihui | 0:19e5a063d7ea | 18 | }; |
fusihui | 0:19e5a063d7ea | 19 | |
fusihui | 1:c4e8e32f91ac | 20 | /////////////// objects /////////////// |
fusihui | 0:19e5a063d7ea | 21 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
fusihui | 0:19e5a063d7ea | 22 | Gamepad pad; |
fusihui | 0:19e5a063d7ea | 23 | Engine engine; |
fusihui | 0:19e5a063d7ea | 24 | |
fusihui | 0:19e5a063d7ea | 25 | |
fusihui | 1:c4e8e32f91ac | 26 | ///////////// prototypes /////////////// |
fusihui | 0:19e5a063d7ea | 27 | void init(); |
fusihui | 0:19e5a063d7ea | 28 | void display(); |
fusihui | 0:19e5a063d7ea | 29 | void start(); |
fusihui | 0:19e5a063d7ea | 30 | |
fusihui | 0:19e5a063d7ea | 31 | |
fusihui | 1:c4e8e32f91ac | 32 | ///////////// functions //////////////// |
fusihui | 0:19e5a063d7ea | 33 | int main() |
fusihui | 0:19e5a063d7ea | 34 | { |
fusihui | 0:19e5a063d7ea | 35 | |
fusihui | 0:19e5a063d7ea | 36 | |
fusihui | 0:19e5a063d7ea | 37 | int fps = 6; // frames per second |
fusihui | 0:19e5a063d7ea | 38 | |
fusihui | 0:19e5a063d7ea | 39 | init(); // initialise |
fusihui | 0:19e5a063d7ea | 40 | start(); // display the welcome screen and wait for starting |
fusihui | 0:19e5a063d7ea | 41 | |
fusihui | 0:19e5a063d7ea | 42 | |
fusihui | 0:19e5a063d7ea | 43 | display(); // draw the initial frame |
fusihui | 0:19e5a063d7ea | 44 | wait(1.0f/fps); |
fusihui | 0:19e5a063d7ea | 45 | |
fusihui | 0:19e5a063d7ea | 46 | // game loop |
fusihui | 0:19e5a063d7ea | 47 | while (1) { |
fusihui | 0:19e5a063d7ea | 48 | engine.read_input(pad); |
fusihui | 0:19e5a063d7ea | 49 | engine.update(lcd,pad); |
fusihui | 0:19e5a063d7ea | 50 | display(); |
fusihui | 0:19e5a063d7ea | 51 | wait(1.0f/fps); |
fusihui | 0:19e5a063d7ea | 52 | } |
fusihui | 0:19e5a063d7ea | 53 | } |
fusihui | 0:19e5a063d7ea | 54 | |
fusihui | 0:19e5a063d7ea | 55 | // initial all classea and libraries |
fusihui | 0:19e5a063d7ea | 56 | void init() |
fusihui | 0:19e5a063d7ea | 57 | { |
fusihui | 0:19e5a063d7ea | 58 | // initialise lcd and gamepad |
fusihui | 0:19e5a063d7ea | 59 | lcd.init(); |
fusihui | 0:19e5a063d7ea | 60 | pad.init(); |
fusihui | 0:19e5a063d7ea | 61 | |
fusihui | 0:19e5a063d7ea | 62 | // initialise the game content |
fusihui | 0:19e5a063d7ea | 63 | engine.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_SIZE,BALL_SPEED,BLOCK_SIZE); |
fusihui | 0:19e5a063d7ea | 64 | |
fusihui | 0:19e5a063d7ea | 65 | } |
fusihui | 0:19e5a063d7ea | 66 | |
fusihui | 0:19e5a063d7ea | 67 | //draw frame on the lcd |
fusihui | 0:19e5a063d7ea | 68 | void display() |
fusihui | 0:19e5a063d7ea | 69 | { |
fusihui | 0:19e5a063d7ea | 70 | // clear the screen |
fusihui | 0:19e5a063d7ea | 71 | lcd.clear(); |
fusihui | 0:19e5a063d7ea | 72 | //draw the content |
fusihui | 0:19e5a063d7ea | 73 | engine.draw(lcd); |
fusihui | 0:19e5a063d7ea | 74 | //refresh |
fusihui | 0:19e5a063d7ea | 75 | lcd.refresh(); |
fusihui | 0:19e5a063d7ea | 76 | } |
fusihui | 0:19e5a063d7ea | 77 | |
fusihui | 0:19e5a063d7ea | 78 | // start screen |
fusihui | 0:19e5a063d7ea | 79 | void start() |
fusihui | 0:19e5a063d7ea | 80 | { |
fusihui | 0:19e5a063d7ea | 81 | // print the name of game |
fusihui | 0:19e5a063d7ea | 82 | lcd.printString(" *PINBALL*",0,1); |
fusihui | 0:19e5a063d7ea | 83 | lcd.printString(" -",0,3); |
fusihui | 0:19e5a063d7ea | 84 | wait(0.3); |
fusihui | 0:19e5a063d7ea | 85 | lcd.printString(" --",0,3); |
fusihui | 0:19e5a063d7ea | 86 | wait(0.3); |
fusihui | 0:19e5a063d7ea | 87 | lcd.printString(" ---",0,3); |
fusihui | 0:19e5a063d7ea | 88 | wait(0.3); |
fusihui | 0:19e5a063d7ea | 89 | lcd.printString(" ----",0,3); |
fusihui | 0:19e5a063d7ea | 90 | wait(0.3); |
fusihui | 0:19e5a063d7ea | 91 | lcd.printString(" -----",0,3); |
fusihui | 0:19e5a063d7ea | 92 | wait(0.3); |
fusihui | 0:19e5a063d7ea | 93 | lcd.printString(" -----Sihui Fu",0,3); |
fusihui | 0:19e5a063d7ea | 94 | wait(0.3); |
fusihui | 0:19e5a063d7ea | 95 | lcd.printString("Press to Start~",0,4); |
fusihui | 0:19e5a063d7ea | 96 | lcd.refresh(); |
fusihui | 0:19e5a063d7ea | 97 | |
fusihui | 0:19e5a063d7ea | 98 | //wait for the start button to be pressed |
fusihui | 0:19e5a063d7ea | 99 | while (pad.check_event(Gamepad::START_PRESSED) == false) |
fusihui | 0:19e5a063d7ea | 100 | { |
fusihui | 0:19e5a063d7ea | 101 | pad.leds_on(); |
fusihui | 0:19e5a063d7ea | 102 | wait(0.3); |
fusihui | 0:19e5a063d7ea | 103 | pad.leds_off(); |
fusihui | 0:19e5a063d7ea | 104 | wait(0.3); |
fusihui | 0:19e5a063d7ea | 105 | } |
fusihui | 0:19e5a063d7ea | 106 | } |
fusihui | 0:19e5a063d7ea | 107 | |
fusihui | 0:19e5a063d7ea | 108 |