Pong game for ELEC1620 board.

main.cpp

Committer:
eencae
Date:
2021-03-05
Revision:
1:d63a63f0d397
Parent:
0:be41a15e7a86
Child:
2:482d74ef09c8

File content as of revision 1:d63a63f0d397:


///////////// 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;
///////////// prototypes ///////////////
void init();
void render();
void welcome();
////////////////////////////////////////

int main() {
    init();      // initialise devices and objects
    welcome();   // waiting for the user to start 
    render();    // first draw the initial frame 
    thread_sleep_for(500);  // and wait for one frame period (100 ms - 10 fps
    
    while (1) {
        // read the joystick input and store in a struct
        UserInput input = {joystick.get_direction(),joystick.get_mag()};
        pong.update(input);     // update the game engine based on input
        render();               // draw frame on screen
        thread_sleep_for(500);  // 100 ms - 10 f.p.s
    }
}

void init() {
    seven_seg.write(0x00);  // turn of 7-seg display
    lcd.init();
    lcd.setContrast(0.5);
    joystick.init();
    pong.init(2,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);   
    }
}