Example Pong game for mbed.

Dependencies:   mbed

main.cpp

Committer:
valavanisalex
Date:
2017-03-05
Revision:
7:75ba0b323011
Parent:
1:25a839625a1e
Child:
8:d8e9de797e90

File content as of revision 7:75ba0b323011:

///////// pre-processor directives ////////
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "PongEngine.h"

#define PADDLE_WIDTH 2
#define PADDLE_HEIGHT 10
#define BALL_RADIUS 2
#define BALL_SPEED 3

/////////////// structs /////////////////
struct UserInput {
    Direction d;
    float mag;
};

/////////////// Hardware objects ///////////////
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad pad;

///////////// prototypes ///////////////
void init();
void update_game(UserInput input);
void render(PongEngine &pong);
int get_random_number_from_pad();

///////////// functions ////////////////
int main()
{
    int fps = 8;

    // Initialise hardware
    init();

    int seed = get_random_number_from_pad();

    // Now create the game engine
    PongEngine pong(PADDLE_WIDTH,
                    PADDLE_HEIGHT,
                    BALL_RADIUS,
                    BALL_SPEED,
                    seed);

    render(pong);  // draw initial frame
    wait(1.0f/fps);

    while (1) {
        pong.read_input(pad);
        pong.update(pad);
        render(pong);
        wait(1.0f/fps);
    }
}

/**
 * @brief create some random number from ADC pins to use in srand
 * @returns a random integer
 */
int get_random_number_from_pad()
{
    float rand1 = pad.read_pot();
    Vector2D rand2 = pad.get_coord();
    int seed = int(1.0e6f*(rand1 + (rand2.x/rand2.y)));

    return seed;
    }

void init()
{
    lcd.init();
    pad.init();
}

void render(PongEngine &pong)
{
    lcd.clear();  // clear screen, re-draw and refresh
    pong.draw(lcd);
    lcd.refresh();
}