Craig Evans
/
ELEC2645_Pong2021
Pong game for ELEC1620 board.
Diff: lib/PongEngine.cpp
- Revision:
- 3:5746c6833d73
- Parent:
- 2:482d74ef09c8
diff -r 482d74ef09c8 -r 5746c6833d73 lib/PongEngine.cpp --- 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