Craig Evans
/
Pong
Example Pong game for mbed.
main.cpp@8:d8e9de797e90, 2017-03-05 (annotated)
- Committer:
- valavanisalex
- Date:
- Sun Mar 05 16:47:15 2017 +0000
- Revision:
- 8:d8e9de797e90
- Parent:
- 7:75ba0b323011
- Child:
- 9:6e8964c19459
Formatting only
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eencae | 0:e1442f3aa3c7 | 1 | ///////// pre-processor directives //////// |
eencae | 0:e1442f3aa3c7 | 2 | #include "mbed.h" |
eencae | 0:e1442f3aa3c7 | 3 | #include "Gamepad.h" |
eencae | 0:e1442f3aa3c7 | 4 | #include "N5110.h" |
eencae | 0:e1442f3aa3c7 | 5 | #include "PongEngine.h" |
eencae | 0:e1442f3aa3c7 | 6 | |
eencae | 0:e1442f3aa3c7 | 7 | #define PADDLE_WIDTH 2 |
eencae | 0:e1442f3aa3c7 | 8 | #define PADDLE_HEIGHT 10 |
eencae | 0:e1442f3aa3c7 | 9 | #define BALL_RADIUS 2 |
eencae | 0:e1442f3aa3c7 | 10 | #define BALL_SPEED 3 |
eencae | 1:25a839625a1e | 11 | |
eencae | 0:e1442f3aa3c7 | 12 | /////////////// structs ///////////////// |
eencae | 0:e1442f3aa3c7 | 13 | struct UserInput { |
eencae | 0:e1442f3aa3c7 | 14 | Direction d; |
eencae | 0:e1442f3aa3c7 | 15 | float mag; |
eencae | 0:e1442f3aa3c7 | 16 | }; |
valavanisalex | 7:75ba0b323011 | 17 | |
valavanisalex | 7:75ba0b323011 | 18 | /////////////// Hardware objects /////////////// |
eencae | 0:e1442f3aa3c7 | 19 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
eencae | 0:e1442f3aa3c7 | 20 | Gamepad pad; |
valavanisalex | 7:75ba0b323011 | 21 | |
eencae | 0:e1442f3aa3c7 | 22 | ///////////// prototypes /////////////// |
eencae | 0:e1442f3aa3c7 | 23 | void init(); |
eencae | 0:e1442f3aa3c7 | 24 | void update_game(UserInput input); |
valavanisalex | 7:75ba0b323011 | 25 | void render(PongEngine &pong); |
valavanisalex | 7:75ba0b323011 | 26 | int get_random_number_from_pad(); |
valavanisalex | 7:75ba0b323011 | 27 | |
eencae | 0:e1442f3aa3c7 | 28 | ///////////// functions //////////////// |
eencae | 0:e1442f3aa3c7 | 29 | int main() |
eencae | 0:e1442f3aa3c7 | 30 | { |
eencae | 1:25a839625a1e | 31 | int fps = 8; |
eencae | 0:e1442f3aa3c7 | 32 | |
valavanisalex | 7:75ba0b323011 | 33 | // Initialise hardware |
eencae | 1:25a839625a1e | 34 | init(); |
valavanisalex | 7:75ba0b323011 | 35 | |
valavanisalex | 7:75ba0b323011 | 36 | int seed = get_random_number_from_pad(); |
valavanisalex | 7:75ba0b323011 | 37 | |
valavanisalex | 7:75ba0b323011 | 38 | // Now create the game engine |
valavanisalex | 7:75ba0b323011 | 39 | PongEngine pong(PADDLE_WIDTH, |
valavanisalex | 7:75ba0b323011 | 40 | PADDLE_HEIGHT, |
valavanisalex | 7:75ba0b323011 | 41 | BALL_RADIUS, |
valavanisalex | 7:75ba0b323011 | 42 | BALL_SPEED, |
valavanisalex | 7:75ba0b323011 | 43 | seed); |
valavanisalex | 7:75ba0b323011 | 44 | |
valavanisalex | 7:75ba0b323011 | 45 | render(pong); // draw initial frame |
eencae | 1:25a839625a1e | 46 | wait(1.0f/fps); |
eencae | 0:e1442f3aa3c7 | 47 | |
eencae | 0:e1442f3aa3c7 | 48 | while (1) { |
eencae | 1:25a839625a1e | 49 | pong.read_input(pad); |
eencae | 1:25a839625a1e | 50 | pong.update(pad); |
valavanisalex | 7:75ba0b323011 | 51 | render(pong); |
eencae | 0:e1442f3aa3c7 | 52 | wait(1.0f/fps); |
eencae | 0:e1442f3aa3c7 | 53 | } |
eencae | 0:e1442f3aa3c7 | 54 | } |
eencae | 0:e1442f3aa3c7 | 55 | |
valavanisalex | 7:75ba0b323011 | 56 | /** |
valavanisalex | 7:75ba0b323011 | 57 | * @brief create some random number from ADC pins to use in srand |
valavanisalex | 7:75ba0b323011 | 58 | * @returns a random integer |
valavanisalex | 7:75ba0b323011 | 59 | */ |
valavanisalex | 7:75ba0b323011 | 60 | int get_random_number_from_pad() |
valavanisalex | 7:75ba0b323011 | 61 | { |
valavanisalex | 7:75ba0b323011 | 62 | float rand1 = pad.read_pot(); |
valavanisalex | 7:75ba0b323011 | 63 | Vector2D rand2 = pad.get_coord(); |
valavanisalex | 7:75ba0b323011 | 64 | int seed = int(1.0e6f*(rand1 + (rand2.x/rand2.y))); |
valavanisalex | 7:75ba0b323011 | 65 | |
valavanisalex | 7:75ba0b323011 | 66 | return seed; |
valavanisalex | 8:d8e9de797e90 | 67 | } |
valavanisalex | 7:75ba0b323011 | 68 | |
eencae | 0:e1442f3aa3c7 | 69 | void init() |
eencae | 0:e1442f3aa3c7 | 70 | { |
eencae | 0:e1442f3aa3c7 | 71 | lcd.init(); |
eencae | 0:e1442f3aa3c7 | 72 | pad.init(); |
eencae | 0:e1442f3aa3c7 | 73 | } |
eencae | 0:e1442f3aa3c7 | 74 | |
valavanisalex | 7:75ba0b323011 | 75 | void render(PongEngine &pong) |
eencae | 0:e1442f3aa3c7 | 76 | { |
eencae | 0:e1442f3aa3c7 | 77 | lcd.clear(); // clear screen, re-draw and refresh |
eencae | 1:25a839625a1e | 78 | pong.draw(lcd); |
eencae | 0:e1442f3aa3c7 | 79 | lcd.refresh(); |
eencae | 0:e1442f3aa3c7 | 80 | } |