16
Dependencies: Ball Ball1 Gamepad N5110 Paddle1 PongEngine cab lab mbed
Fork of Pong by
main.cpp@6:af8039432891, 2017-05-04 (annotated)
- Committer:
- fy14ta
- Date:
- Thu May 04 06:44:37 2017 +0000
- Revision:
- 6:af8039432891
- Parent:
- 5:33cac7200f38
Libraries
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 |
fy14ta | 5:33cac7200f38 | 8 | #define PADDLE_HEIGHT 15 |
fy14ta | 5:33cac7200f38 | 9 | #define BALL_SIZE 4 |
fy14ta | 5:33cac7200f38 | 10 | #define BALL1_SIZE 6 |
eencae | 0:e1442f3aa3c7 | 11 | #define BALL_SPEED 3 |
fy14ta | 5:33cac7200f38 | 12 | #define PADDLE1_WIDTH 15 |
fy14ta | 5:33cac7200f38 | 13 | #define PADDLE1_HEIGHT 2 |
fy14ta | 5:33cac7200f38 | 14 | #define LAB_WIDTH 3 |
fy14ta | 5:33cac7200f38 | 15 | #define LAB_HEIGHT 35 |
fy14ta | 5:33cac7200f38 | 16 | #define CAB_WIDTH 10 |
fy14ta | 5:33cac7200f38 | 17 | #define CAB_HEIGHT 6 |
fy14ta | 5:33cac7200f38 | 18 | |
fy14ta | 5:33cac7200f38 | 19 | |
fy14ta | 5:33cac7200f38 | 20 | |
fy14ta | 5:33cac7200f38 | 21 | |
eencae | 1:25a839625a1e | 22 | |
eencae | 0:e1442f3aa3c7 | 23 | /////////////// structs ///////////////// |
eencae | 0:e1442f3aa3c7 | 24 | struct UserInput { |
eencae | 0:e1442f3aa3c7 | 25 | Direction d; |
eencae | 0:e1442f3aa3c7 | 26 | float mag; |
eencae | 0:e1442f3aa3c7 | 27 | }; |
eencae | 0:e1442f3aa3c7 | 28 | /////////////// objects /////////////// |
eencae | 0:e1442f3aa3c7 | 29 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
eencae | 0:e1442f3aa3c7 | 30 | Gamepad pad; |
eencae | 0:e1442f3aa3c7 | 31 | PongEngine pong; |
eencae | 0:e1442f3aa3c7 | 32 | ///////////// prototypes /////////////// |
eencae | 0:e1442f3aa3c7 | 33 | void init(); |
eencae | 0:e1442f3aa3c7 | 34 | void update_game(UserInput input); |
eencae | 0:e1442f3aa3c7 | 35 | void render(); |
eencae | 3:910d7e87f367 | 36 | void welcome(); |
eencae | 0:e1442f3aa3c7 | 37 | ///////////// functions //////////////// |
eencae | 0:e1442f3aa3c7 | 38 | int main() |
eencae | 0:e1442f3aa3c7 | 39 | { |
eencae | 3:910d7e87f367 | 40 | int fps = 8; // frames per second |
eencae | 0:e1442f3aa3c7 | 41 | |
eencae | 1:25a839625a1e | 42 | init(); |
eencae | 3:910d7e87f367 | 43 | welcome(); |
eencae | 1:25a839625a1e | 44 | |
eencae | 1:25a839625a1e | 45 | render(); // draw initial frame |
eencae | 3:910d7e87f367 | 46 | wait(1.0f/fps); |
eencae | 0:e1442f3aa3c7 | 47 | |
eencae | 4:d349e5d847cf | 48 | // game loop - read input, update the game state and render the display |
eencae | 0:e1442f3aa3c7 | 49 | while (1) { |
eencae | 1:25a839625a1e | 50 | pong.read_input(pad); |
eencae | 1:25a839625a1e | 51 | pong.update(pad); |
eencae | 0:e1442f3aa3c7 | 52 | render(); |
eencae | 0:e1442f3aa3c7 | 53 | wait(1.0f/fps); |
eencae | 0:e1442f3aa3c7 | 54 | } |
eencae | 0:e1442f3aa3c7 | 55 | } |
eencae | 0:e1442f3aa3c7 | 56 | |
eencae | 0:e1442f3aa3c7 | 57 | void init() |
eencae | 0:e1442f3aa3c7 | 58 | { |
eencae | 3:910d7e87f367 | 59 | // need to initialise LCD and Gamepad |
eencae | 0:e1442f3aa3c7 | 60 | lcd.init(); |
eencae | 0:e1442f3aa3c7 | 61 | pad.init(); |
eencae | 3:910d7e87f367 | 62 | |
eencae | 3:910d7e87f367 | 63 | // initialise the game |
fy14ta | 5:33cac7200f38 | 64 | pong.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_SIZE, BALL1_SIZE, BALL_SPEED, PADDLE1_WIDTH, PADDLE1_HEIGHT, LAB_WIDTH, LAB_HEIGHT, CAB_WIDTH, CAB_HEIGHT); |
eencae | 0:e1442f3aa3c7 | 65 | |
eencae | 0:e1442f3aa3c7 | 66 | } |
eencae | 0:e1442f3aa3c7 | 67 | |
eencae | 0:e1442f3aa3c7 | 68 | void render() |
eencae | 0:e1442f3aa3c7 | 69 | { |
eencae | 3:910d7e87f367 | 70 | // clear screen, re-draw and refresh |
eencae | 3:910d7e87f367 | 71 | lcd.clear(); |
eencae | 1:25a839625a1e | 72 | pong.draw(lcd); |
eencae | 0:e1442f3aa3c7 | 73 | lcd.refresh(); |
eencae | 3:910d7e87f367 | 74 | } |
eencae | 3:910d7e87f367 | 75 | |
eencae | 3:910d7e87f367 | 76 | void welcome() { |
eencae | 3:910d7e87f367 | 77 | |
fy14ta | 5:33cac7200f38 | 78 | lcd.printString("Weclome",25,1); |
fy14ta | 5:33cac7200f38 | 79 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 80 | lcd.refresh(); |
fy14ta | 5:33cac7200f38 | 81 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 82 | lcd.printString("To the", 25,2); |
fy14ta | 5:33cac7200f38 | 83 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 84 | lcd.refresh(); |
fy14ta | 5:33cac7200f38 | 85 | lcd.printString("Labirint!",24,3); |
fy14ta | 5:33cac7200f38 | 86 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 87 | lcd.refresh(); |
fy14ta | 5:33cac7200f38 | 88 | lcd.clear(); |
fy14ta | 5:33cac7200f38 | 89 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 90 | |
fy14ta | 5:33cac7200f38 | 91 | lcd.printString("Rules:",5,1); |
fy14ta | 5:33cac7200f38 | 92 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 93 | lcd.refresh(); |
fy14ta | 5:33cac7200f38 | 94 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 95 | lcd.printString("Press L", 5,2); |
fy14ta | 5:33cac7200f38 | 96 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 97 | lcd.refresh(); |
fy14ta | 5:33cac7200f38 | 98 | lcd.printString("To get bigger",5,3); |
fy14ta | 5:33cac7200f38 | 99 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 100 | lcd.refresh(); |
fy14ta | 5:33cac7200f38 | 101 | lcd.clear(); |
fy14ta | 5:33cac7200f38 | 102 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 103 | |
fy14ta | 5:33cac7200f38 | 104 | lcd.printString("Or",5,1); |
fy14ta | 5:33cac7200f38 | 105 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 106 | lcd.refresh(); |
fy14ta | 5:33cac7200f38 | 107 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 108 | lcd.printString("Press R", 5,2); |
fy14ta | 5:33cac7200f38 | 109 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 110 | lcd.refresh(); |
fy14ta | 5:33cac7200f38 | 111 | lcd.printString("To get",5,3); |
fy14ta | 5:33cac7200f38 | 112 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 113 | lcd.refresh(); |
fy14ta | 5:33cac7200f38 | 114 | lcd.printString("Smaller",5,4); |
fy14ta | 5:33cac7200f38 | 115 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 116 | lcd.refresh(); |
fy14ta | 5:33cac7200f38 | 117 | lcd.clear(); |
fy14ta | 5:33cac7200f38 | 118 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 119 | |
fy14ta | 5:33cac7200f38 | 120 | lcd.printString("Small can",5,1); |
fy14ta | 5:33cac7200f38 | 121 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 122 | lcd.refresh(); |
fy14ta | 5:33cac7200f38 | 123 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 124 | lcd.printString("go only", 5,2); |
fy14ta | 5:33cac7200f38 | 125 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 126 | lcd.refresh(); |
fy14ta | 5:33cac7200f38 | 127 | lcd.printString("horizontally",5,3); |
fy14ta | 5:33cac7200f38 | 128 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 129 | lcd.refresh(); |
fy14ta | 5:33cac7200f38 | 130 | lcd.clear(); |
fy14ta | 5:33cac7200f38 | 131 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 132 | |
fy14ta | 5:33cac7200f38 | 133 | lcd.printString("Big can",5,1); |
fy14ta | 5:33cac7200f38 | 134 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 135 | lcd.refresh(); |
fy14ta | 5:33cac7200f38 | 136 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 137 | lcd.printString("go only", 5,2); |
fy14ta | 5:33cac7200f38 | 138 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 139 | lcd.refresh(); |
fy14ta | 5:33cac7200f38 | 140 | lcd.printString("vertically",5,3); |
fy14ta | 5:33cac7200f38 | 141 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 142 | lcd.refresh(); |
fy14ta | 5:33cac7200f38 | 143 | lcd.clear(); |
fy14ta | 5:33cac7200f38 | 144 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 145 | |
fy14ta | 5:33cac7200f38 | 146 | lcd.printString("You have",5,1); |
fy14ta | 5:33cac7200f38 | 147 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 148 | lcd.refresh(); |
fy14ta | 5:33cac7200f38 | 149 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 150 | lcd.printString("2 paddles", 5,2); |
fy14ta | 5:33cac7200f38 | 151 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 152 | lcd.refresh(); |
fy14ta | 5:33cac7200f38 | 153 | lcd.printString("to deliver",5,3); |
fy14ta | 5:33cac7200f38 | 154 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 155 | lcd.refresh(); |
fy14ta | 5:33cac7200f38 | 156 | lcd.clear(); |
fy14ta | 5:33cac7200f38 | 157 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 158 | |
fy14ta | 5:33cac7200f38 | 159 | lcd.printString("Ball to the",5,1); |
fy14ta | 5:33cac7200f38 | 160 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 161 | lcd.refresh(); |
fy14ta | 5:33cac7200f38 | 162 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 163 | lcd.printString("Home", 5,2); |
fy14ta | 5:33cac7200f38 | 164 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 165 | lcd.refresh(); |
fy14ta | 5:33cac7200f38 | 166 | lcd.printString("Enjoy!",5,3); |
fy14ta | 5:33cac7200f38 | 167 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 168 | lcd.refresh(); |
fy14ta | 5:33cac7200f38 | 169 | lcd.clear(); |
fy14ta | 5:33cac7200f38 | 170 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 171 | |
fy14ta | 5:33cac7200f38 | 172 | lcd.printString("Press Start",15,2); |
fy14ta | 5:33cac7200f38 | 173 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 174 | lcd.printString("To Begin",25,4); |
fy14ta | 5:33cac7200f38 | 175 | wait(1.0); |
fy14ta | 5:33cac7200f38 | 176 | |
fy14ta | 5:33cac7200f38 | 177 | |
eencae | 3:910d7e87f367 | 178 | lcd.refresh(); |
eencae | 3:910d7e87f367 | 179 | |
eencae | 3:910d7e87f367 | 180 | // wait flashing LEDs until start button is pressed |
eencae | 3:910d7e87f367 | 181 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { |
eencae | 3:910d7e87f367 | 182 | pad.leds_on(); |
eencae | 3:910d7e87f367 | 183 | wait(0.1); |
eencae | 3:910d7e87f367 | 184 | pad.leds_off(); |
eencae | 3:910d7e87f367 | 185 | wait(0.1); |
eencae | 3:910d7e87f367 | 186 | } |
eencae | 0:e1442f3aa3c7 | 187 | } |