Craig Evans
/
ELEC2645_Pong2021
Pong game for ELEC1620 board.
main.cpp
- Committer:
- eencae
- Date:
- 2021-03-11
- Revision:
- 3:5746c6833d73
- Parent:
- 2:482d74ef09c8
File content as of revision 3:5746c6833d73:
///////////// includes ///////////////////// #include "mbed.h" #include "platform/mbed_thread.h" #include "Joystick.h" #include "N5110.h" #include "ShiftReg.h" #include "PongEngine.h" #include "Utils.h" ///////////// defines ///////////////////// #define PADDLE_WIDTH 2 #define PADDLE_HEIGHT 8 #define BALL_SIZE 2 #define BALL_SPEED 3 ///////////// objects /////////////////// N5110 lcd(p14,p8,p9,p10,p11,p13,p21); Joystick joystick(p20,p19); DigitalIn buttonA(p29); BusOut leds(LED4,LED3,LED2,LED1); ShiftReg seven_seg; PongEngine pong; AnalogIn pot(p17); ///////////// prototypes /////////////// void init(); void render(); void welcome(); void display_lives(int lives); void game_over(); //////////////////////////////////////// int main() { init(); // initialise devices and objects welcome(); // waiting for the user to start render(); // first draw the initial frame int fps = 10; thread_sleep_for(1000/fps); // and wait for one frame period - millseconds int lives = 4; // display lives on LEDs display_lives(lives); while (lives > 0) { // keep looping while lives remain // read the joystick input and store in a struct UserInput input = {joystick.get_direction(),joystick.get_mag()}; lives = pong.update(input); // update the game engine based on input display_lives(lives); // display lives on LEDs render(); // draw frame on screen thread_sleep_for(1000/fps); // and wait for one frame period - ms } game_over(); } void init() { seven_seg.write(0x00); // turn of 7-seg display lcd.init(); lcd.setContrast(0.5); joystick.init(); pong.init(0,8,2,2,2); // paddle x position, paddle_height,paddle_width,ball_size,speed } void render() { // clear screen, re-draw and refresh lcd.clear(); pong.draw(lcd); lcd.refresh(); } void welcome() { // splash screen lcd.printString(" Pong! ",0,1); lcd.printString(" Press A ",0,4); lcd.refresh(); // wait flashing LEDs until button A is pressed while (buttonA.read() == 0) { leds = 0b1111; thread_sleep_for(100); leds = 0b0000; thread_sleep_for(100); } } void display_lives(int lives) { if (lives == 4) { leds = 0b1111; seven_seg.write(0x66); } else if (lives == 3) { leds = 0b1110; seven_seg.write(0x4F); } else if (lives == 2) { leds = 0b1100; seven_seg.write(0x5B); } else if (lives == 1) { leds = 0b1000; seven_seg.write(0x06); } else { leds = 0b0000; seven_seg.write(0x3F); } } void game_over() { // splash screen while (1) { lcd.clear(); lcd.printString(" Game Over ",0,2); lcd.printString(" Loser! ",0,4); lcd.refresh(); leds = 0b1111; thread_sleep_for(250); lcd.clear(); lcd.refresh(); leds = 0b0000; thread_sleep_for(250); } }