James Heavey
/
EL17JH
FINAL VERSION
main.cpp@8:1ab6d90c4d60, 2019-04-21 (annotated)
- Committer:
- jamesheavey
- Date:
- Sun Apr 21 19:40:05 2019 +0000
- Revision:
- 8:1ab6d90c4d60
- Parent:
- 5:67c3e15ee6f8
- Child:
- 9:f6f0f39538c7
edited the check paddle collision to account for direction and angle of reflection based on where the paddle is hit. Buggy, needs refinement
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:5719d0fe94ed | 6 | #include "Bitmap.h" |
jamesheavey | 4:6bd488b5b31a | 7 | #include<iostream> |
jamesheavey | 8:1ab6d90c4d60 | 8 | #include "Sprites.h" |
jamesheavey | 0:7d4d2023ed9c | 9 | |
jamesheavey | 0:7d4d2023ed9c | 10 | #ifdef WITH_TESTING |
jamesheavey | 0:7d4d2023ed9c | 11 | # include "tests.h" |
jamesheavey | 0:7d4d2023ed9c | 12 | #endif |
jamesheavey | 0:7d4d2023ed9c | 13 | |
jamesheavey | 1:e18615d46071 | 14 | #define PADDLE_WIDTH 12 |
jamesheavey | 0:7d4d2023ed9c | 15 | #define PADDLE_HEIGHT 2 |
jamesheavey | 0:7d4d2023ed9c | 16 | #define BALL_SIZE 2 |
jamesheavey | 0:7d4d2023ed9c | 17 | #define BALL_SPEED 3 |
jamesheavey | 0:7d4d2023ed9c | 18 | |
jamesheavey | 0:7d4d2023ed9c | 19 | /////////////// structs ///////////////// |
jamesheavey | 0:7d4d2023ed9c | 20 | struct UserInput { |
jamesheavey | 0:7d4d2023ed9c | 21 | Direction d; |
jamesheavey | 0:7d4d2023ed9c | 22 | float mag; |
jamesheavey | 0:7d4d2023ed9c | 23 | }; |
jamesheavey | 0:7d4d2023ed9c | 24 | /////////////// objects /////////////// |
jamesheavey | 0:7d4d2023ed9c | 25 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
jamesheavey | 0:7d4d2023ed9c | 26 | Gamepad pad; |
jamesheavey | 0:7d4d2023ed9c | 27 | PongEngine pong; |
jamesheavey | 0:7d4d2023ed9c | 28 | |
jamesheavey | 0:7d4d2023ed9c | 29 | ///////////// prototypes /////////////// |
jamesheavey | 0:7d4d2023ed9c | 30 | void init(); |
jamesheavey | 0:7d4d2023ed9c | 31 | void update_game(UserInput input); |
jamesheavey | 0:7d4d2023ed9c | 32 | void render(); |
jamesheavey | 0:7d4d2023ed9c | 33 | void welcome(); |
jamesheavey | 0:7d4d2023ed9c | 34 | |
jamesheavey | 0:7d4d2023ed9c | 35 | ///////////// functions //////////////// |
jamesheavey | 0:7d4d2023ed9c | 36 | int main() |
jamesheavey | 0:7d4d2023ed9c | 37 | { |
jamesheavey | 0:7d4d2023ed9c | 38 | #ifdef WITH_TESTING |
jamesheavey | 0:7d4d2023ed9c | 39 | int number_of_failures = run_all_tests(); |
jamesheavey | 0:7d4d2023ed9c | 40 | |
jamesheavey | 0:7d4d2023ed9c | 41 | if(number_of_failures > 0) return number_of_failures; |
jamesheavey | 0:7d4d2023ed9c | 42 | #endif |
jamesheavey | 0:7d4d2023ed9c | 43 | |
jamesheavey | 3:5719d0fe94ed | 44 | int fps = 8; // frames per second |
jamesheavey | 4:6bd488b5b31a | 45 | bool pause = false; |
jamesheavey | 0:7d4d2023ed9c | 46 | init(); // initialise and then display welcome screen... |
jamesheavey | 0:7d4d2023ed9c | 47 | welcome(); // waiting for the user to start |
jamesheavey | 0:7d4d2023ed9c | 48 | |
jamesheavey | 3:5719d0fe94ed | 49 | Bitmap three(three_data, 48, 84); |
jamesheavey | 3:5719d0fe94ed | 50 | Bitmap two(two_data, 48, 84); |
jamesheavey | 3:5719d0fe94ed | 51 | Bitmap one(one_data, 48, 84); |
jamesheavey | 3:5719d0fe94ed | 52 | |
jamesheavey | 3:5719d0fe94ed | 53 | lcd.clear(); |
jamesheavey | 3:5719d0fe94ed | 54 | three.render(lcd, 0, 0); |
jamesheavey | 3:5719d0fe94ed | 55 | lcd.refresh(); |
jamesheavey | 3:5719d0fe94ed | 56 | wait(1); |
jamesheavey | 3:5719d0fe94ed | 57 | |
jamesheavey | 3:5719d0fe94ed | 58 | lcd.clear(); |
jamesheavey | 3:5719d0fe94ed | 59 | two.render(lcd, 0, 0); |
jamesheavey | 3:5719d0fe94ed | 60 | lcd.refresh(); |
jamesheavey | 3:5719d0fe94ed | 61 | wait(1); |
jamesheavey | 3:5719d0fe94ed | 62 | |
jamesheavey | 3:5719d0fe94ed | 63 | lcd.clear(); |
jamesheavey | 3:5719d0fe94ed | 64 | one.render(lcd, 0, 0); |
jamesheavey | 3:5719d0fe94ed | 65 | lcd.refresh(); |
jamesheavey | 3:5719d0fe94ed | 66 | wait(1); |
jamesheavey | 3:5719d0fe94ed | 67 | |
jamesheavey | 0:7d4d2023ed9c | 68 | render(); // first draw the initial frame |
jamesheavey | 0:7d4d2023ed9c | 69 | wait(1.0f/fps); // and wait for one frame period |
jamesheavey | 0:7d4d2023ed9c | 70 | |
jamesheavey | 0:7d4d2023ed9c | 71 | |
jamesheavey | 0:7d4d2023ed9c | 72 | // game loop - read input, update the game state and render the display |
jamesheavey | 0:7d4d2023ed9c | 73 | while (1) { |
jamesheavey | 0:7d4d2023ed9c | 74 | pong.read_input(pad); |
jamesheavey | 0:7d4d2023ed9c | 75 | pong.update(pad); |
jamesheavey | 0:7d4d2023ed9c | 76 | render(); |
jamesheavey | 4:6bd488b5b31a | 77 | if (pad.check_event(Gamepad::BACK_PRESSED) == true) { |
jamesheavey | 4:6bd488b5b31a | 78 | pause = !pause; |
jamesheavey | 4:6bd488b5b31a | 79 | } |
jamesheavey | 5:67c3e15ee6f8 | 80 | while (pause == true) { |
jamesheavey | 4:6bd488b5b31a | 81 | lcd.clear(); |
jamesheavey | 5:67c3e15ee6f8 | 82 | lcd.printString(" PAUSED ",0,2); |
jamesheavey | 4:6bd488b5b31a | 83 | lcd.refresh(); |
jamesheavey | 5:67c3e15ee6f8 | 84 | if (pad.check_event(Gamepad::BACK_PRESSED) == true) { |
jamesheavey | 5:67c3e15ee6f8 | 85 | pause = !pause; |
jamesheavey | 5:67c3e15ee6f8 | 86 | } |
jamesheavey | 5:67c3e15ee6f8 | 87 | wait(1.0f/fps); |
jamesheavey | 4:6bd488b5b31a | 88 | } |
jamesheavey | 0:7d4d2023ed9c | 89 | wait(1.0f/fps); |
jamesheavey | 0:7d4d2023ed9c | 90 | } |
jamesheavey | 0:7d4d2023ed9c | 91 | } |
jamesheavey | 0:7d4d2023ed9c | 92 | |
jamesheavey | 0:7d4d2023ed9c | 93 | // initialies all classes and libraries |
jamesheavey | 0:7d4d2023ed9c | 94 | void init() |
jamesheavey | 0:7d4d2023ed9c | 95 | { |
jamesheavey | 0:7d4d2023ed9c | 96 | // need to initialise LCD and Gamepad |
jamesheavey | 0:7d4d2023ed9c | 97 | lcd.init(); |
jamesheavey | 0:7d4d2023ed9c | 98 | pad.init(); |
jamesheavey | 0:7d4d2023ed9c | 99 | |
jamesheavey | 0:7d4d2023ed9c | 100 | // initialise the game with correct ball and paddle sizes |
jamesheavey | 0:7d4d2023ed9c | 101 | pong.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_SIZE,BALL_SPEED); |
jamesheavey | 0:7d4d2023ed9c | 102 | |
jamesheavey | 0:7d4d2023ed9c | 103 | } |
jamesheavey | 0:7d4d2023ed9c | 104 | |
jamesheavey | 0:7d4d2023ed9c | 105 | // this function draws each frame on the LCD |
jamesheavey | 0:7d4d2023ed9c | 106 | void render() |
jamesheavey | 0:7d4d2023ed9c | 107 | { |
jamesheavey | 0:7d4d2023ed9c | 108 | // clear screen, re-draw and refresh |
jamesheavey | 0:7d4d2023ed9c | 109 | lcd.clear(); |
jamesheavey | 0:7d4d2023ed9c | 110 | pong.draw(lcd); |
jamesheavey | 0:7d4d2023ed9c | 111 | lcd.refresh(); |
jamesheavey | 0:7d4d2023ed9c | 112 | } |
jamesheavey | 0:7d4d2023ed9c | 113 | |
jamesheavey | 0:7d4d2023ed9c | 114 | // simple splash screen displayed on start-up |
jamesheavey | 0:7d4d2023ed9c | 115 | void welcome() { |
jamesheavey | 3:5719d0fe94ed | 116 | Bitmap breakwhite(breakwhite_data, 48, 84); |
jamesheavey | 3:5719d0fe94ed | 117 | Bitmap breakblack(breakblack_data, 48, 84); |
jamesheavey | 0:7d4d2023ed9c | 118 | |
jamesheavey | 0:7d4d2023ed9c | 119 | // wait flashing LEDs until start button is pressed |
jamesheavey | 0:7d4d2023ed9c | 120 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { |
jamesheavey | 3:5719d0fe94ed | 121 | lcd.clear(); |
jamesheavey | 3:5719d0fe94ed | 122 | |
jamesheavey | 3:5719d0fe94ed | 123 | breakwhite.render(lcd, 0, 0); |
jamesheavey | 3:5719d0fe94ed | 124 | lcd.printString(" PRESS START ",2,5); |
jamesheavey | 3:5719d0fe94ed | 125 | lcd.refresh(); |
jamesheavey | 0:7d4d2023ed9c | 126 | pad.leds_on(); |
jamesheavey | 3:5719d0fe94ed | 127 | wait(0.5); |
jamesheavey | 3:5719d0fe94ed | 128 | |
jamesheavey | 3:5719d0fe94ed | 129 | lcd.clear(); |
jamesheavey | 3:5719d0fe94ed | 130 | |
jamesheavey | 3:5719d0fe94ed | 131 | breakblack.render(lcd, 0, 0); |
jamesheavey | 3:5719d0fe94ed | 132 | lcd.printString(" PRESS START ",2,5); |
jamesheavey | 3:5719d0fe94ed | 133 | lcd.refresh(); |
jamesheavey | 0:7d4d2023ed9c | 134 | pad.leds_off(); |
jamesheavey | 3:5719d0fe94ed | 135 | wait(0.5); |
jamesheavey | 3:5719d0fe94ed | 136 | |
jamesheavey | 0:7d4d2023ed9c | 137 | } |
jamesheavey | 0:7d4d2023ed9c | 138 | |
jamesheavey | 0:7d4d2023ed9c | 139 | } |