Pong game for ELEC1620 board.

Committer:
eencae
Date:
Fri Mar 05 16:58:05 2021 +0000
Revision:
1:d63a63f0d397
Child:
2:482d74ef09c8
Version ported to ELEC1620 development board.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
eencae 1:d63a63f0d397 1 #include "PongEngine.h"
eencae 1:d63a63f0d397 2
eencae 1:d63a63f0d397 3 PongEngine::PongEngine(){}
eencae 1:d63a63f0d397 4
eencae 1:d63a63f0d397 5 void PongEngine::init(int paddle_position, int paddle_height, int paddle_width, int ball_size, int speed){
eencae 1:d63a63f0d397 6 printf("Pong Engine: Init\n");
eencae 1:d63a63f0d397 7 _ball.init(ball_size,speed);
eencae 1:d63a63f0d397 8 _paddle.init(paddle_position, paddle_height, paddle_width);
eencae 1:d63a63f0d397 9 }
eencae 1:d63a63f0d397 10
eencae 1:d63a63f0d397 11 void PongEngine::update(UserInput input) {
eencae 1:d63a63f0d397 12 printf("Pong Engine: Update\n");
eencae 1:d63a63f0d397 13 _ball.update();
eencae 1:d63a63f0d397 14 _paddle.update(input);
eencae 1:d63a63f0d397 15 // important to update paddles and ball before checking collisions so can
eencae 1:d63a63f0d397 16 // correct for it before updating the display
eencae 1:d63a63f0d397 17 check_wall_collision();
eencae 1:d63a63f0d397 18 }
eencae 1:d63a63f0d397 19
eencae 1:d63a63f0d397 20 void PongEngine::draw(N5110 &lcd) {
eencae 1:d63a63f0d397 21 printf("Pong Engine: Draw\n");
eencae 1:d63a63f0d397 22 // draw the elements in the LCD buffer
eencae 1:d63a63f0d397 23 // pitch
eencae 1:d63a63f0d397 24 lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
eencae 1:d63a63f0d397 25 lcd.drawLine(WIDTH/2,0,WIDTH/2,HEIGHT-1,2);
eencae 1:d63a63f0d397 26 _ball.draw(lcd);
eencae 1:d63a63f0d397 27 _paddle.draw(lcd);
eencae 1:d63a63f0d397 28 }
eencae 1:d63a63f0d397 29
eencae 1:d63a63f0d397 30 void PongEngine::check_wall_collision()
eencae 1:d63a63f0d397 31 {
eencae 1:d63a63f0d397 32 // read current ball attributes
eencae 1:d63a63f0d397 33 Position2D ball_pos = _ball.get_pos();
eencae 1:d63a63f0d397 34 Position2D ball_velocity = _ball.get_velocity();
eencae 1:d63a63f0d397 35 int size = _ball.get_size();
eencae 1:d63a63f0d397 36
eencae 1:d63a63f0d397 37 // check if hit top wall
eencae 1:d63a63f0d397 38 if (ball_pos.y <= 1) { // 1 due to 1 pixel boundary
eencae 1:d63a63f0d397 39 ball_pos.y = 1; // bounce off ceiling without going off screen
eencae 1:d63a63f0d397 40 ball_velocity.y = -ball_velocity.y; // flip velocity
eencae 1:d63a63f0d397 41 }
eencae 1:d63a63f0d397 42 // check if hit bottom wall
eencae 1:d63a63f0d397 43 else if (ball_pos.y + size >= (HEIGHT-1) ) {
eencae 1:d63a63f0d397 44 // hit bottom
eencae 1:d63a63f0d397 45 ball_pos.y = (HEIGHT-1) - size; // stops ball going off screen
eencae 1:d63a63f0d397 46 ball_velocity.y = -ball_velocity.y; // flip velcoity
eencae 1:d63a63f0d397 47 }
eencae 1:d63a63f0d397 48
eencae 1:d63a63f0d397 49 // update ball parameters
eencae 1:d63a63f0d397 50 _ball.set_velocity(ball_velocity);
eencae 1:d63a63f0d397 51 _ball.set_pos(ball_pos);
eencae 1:d63a63f0d397 52 }