Craig Evans
/
ELEC2645_Pong2021
Pong game for ELEC1620 board.
Revision 3:5746c6833d73, committed 2021-03-11
- Comitter:
- eencae
- Date:
- Thu Mar 11 14:54:25 2021 +0000
- Parent:
- 2:482d74ef09c8
- Commit message:
- Added on LEDs and 7-seg for lives, bouncing off paddle and walls.
Changed in this revision
--- a/lib/Ball.cpp Wed Mar 10 16:37:52 2021 +0000 +++ b/lib/Ball.cpp Thu Mar 11 14:54:25 2021 +0000 @@ -3,7 +3,7 @@ Ball::Ball() {} void Ball::init(int size, int speed) { - printf("Ball: Init\n"); + //printf("Ball: Init\n"); _size = size; _x = WIDTH/2 - _size/2; _y = HEIGHT/2 - _size/2; @@ -13,12 +13,12 @@ } void Ball::draw(N5110 &lcd) { - printf("Ball: Draw\n"); + //printf("Ball: Draw\n"); lcd.drawRect(_x,_y,_size,_size,FILL_BLACK); } void Ball::update(){ - printf("Ball: Update\n"); + //printf("Ball: Update\n"); _x += _velocity.x; _y += _velocity.y; }
--- a/lib/Paddle.cpp Wed Mar 10 16:37:52 2021 +0000 +++ b/lib/Paddle.cpp Thu Mar 11 14:54:25 2021 +0000 @@ -20,9 +20,7 @@ void Paddle::update(UserInput input) { printf("Paddle: Update\n"); - _speed = int(input.mag*_height); // speed of movement changes depending on - // how far the joystick is pushed (0 to 1) and the height of the paddle - + _speed = 2; // update y value depending on direction of movement // North is decrement as origin is at the top-left so decreasing moves up if (input.d == N) { _y-=_speed; }
--- a/lib/PongEngine.cpp Wed Mar 10 16:37:52 2021 +0000 +++ b/lib/PongEngine.cpp Thu Mar 11 14:54:25 2021 +0000 @@ -1,35 +1,39 @@ #include "PongEngine.h" -PongEngine::PongEngine(){} +PongEngine::PongEngine(){ _lives = 4; } void PongEngine::init(int paddle_position, int paddle_height, int paddle_width, int ball_size, int speed){ - printf("Pong Engine: Init\n"); + //printf("Pong Engine: Init\n"); _ball.init(ball_size,speed); _paddle.init(paddle_position, paddle_height, paddle_width); } -void PongEngine::update(UserInput input) { - printf("Pong Engine: Update\n"); +int PongEngine::update(UserInput input) { + //printf("Pong Engine: Update\n"); + check_goal(); // checking for a goal is a priority _ball.update(); _paddle.update(input); // important to update paddles and ball before checking collisions so can // correct for it before updating the display check_wall_collision(); check_paddle_collision(); + + return _lives; } void PongEngine::draw(N5110 &lcd) { - printf("Pong Engine: Draw\n"); + //printf("Pong Engine: Draw\n"); // draw the elements in the LCD buffer // pitch - lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT); - lcd.drawLine(WIDTH/2,0,WIDTH/2,HEIGHT-1,2); + lcd.drawLine(0,0,WIDTH-1,0,1); // top + lcd.drawLine(WIDTH-1,0,WIDTH-1,HEIGHT-1,1); // back wall + lcd.drawLine(0,HEIGHT-1,WIDTH-1,HEIGHT-1,1); // bottom _ball.draw(lcd); _paddle.draw(lcd); } void PongEngine::check_wall_collision() { - printf("Pong Engine: Check Wall Collision\n"); + //printf("Pong Engine: Check Wall Collision\n"); // read current ball attributes Position2D ball_pos = _ball.get_pos(); Position2D ball_velocity = _ball.get_velocity(); @@ -55,7 +59,7 @@ } void PongEngine::check_paddle_collision() { - printf("Pong Engine: Check Paddle Collision\n"); + //printf("Pong Engine: Check Paddle Collision\n"); // read current ball and paddle attributes Position2D ball_pos = _ball.get_pos(); Position2D ball_velocity = _ball.get_velocity(); @@ -76,4 +80,17 @@ // write new attributes _ball.set_velocity(ball_velocity); _ball.set_pos(ball_pos); +} + +void PongEngine::check_goal() { + //printf("Pong Engine: Check Goal\n"); + Position2D ball_pos = _ball.get_pos(); + int size = _ball.get_size(); + int speed = abs(_ball.get_velocity().x); // speed is magnitude of velocity + // check if ball position has gone off the left + if (ball_pos.x + size < 0) { + // reset the ball + _ball.init(size,speed); + _lives--; // lose a life + } } \ No newline at end of file
--- a/lib/PongEngine.h Wed Mar 10 16:37:52 2021 +0000 +++ b/lib/PongEngine.h Thu Mar 11 14:54:25 2021 +0000 @@ -11,13 +11,15 @@ public: PongEngine(); // pass in the lcd object from the main file void init(int paddle_position,int paddle_height,int paddle_width,int ball_size,int speed); - void update(UserInput input); + int update(UserInput input); void draw(N5110 &lcd); private: void check_wall_collision(); void check_paddle_collision(); + void check_goal(); Ball _ball; Paddle _paddle; + int _lives; }; #endif \ No newline at end of file
--- a/main.cpp Wed Mar 10 16:37:52 2021 +0000 +++ b/main.cpp Thu Mar 11 14:54:25 2021 +0000 @@ -19,26 +19,34 @@ 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 = 1000/10; // 10 fps - thread_sleep_for(fps); // and wait for one frame period - millseconds + int fps = 10; + thread_sleep_for(1000/fps); // and wait for one frame period - millseconds - while (1) { + 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()}; - pong.update(input); // update the game engine based on input - render(); // draw frame on screen - thread_sleep_for(fps); // and wait for one frame period - ms - } + 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() { @@ -46,7 +54,7 @@ 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 + 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 @@ -61,10 +69,44 @@ lcd.refresh(); // wait flashing LEDs until button A is pressed - while ( buttonA.read() == 0) { + while (buttonA.read() == 0) { leds = 0b1111; thread_sleep_for(100); leds = 0b0000; thread_sleep_for(100); } -} \ No newline at end of file +} + +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); + } +}