Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
PongEngine/PongEngine.cpp
- Committer:
- jamesheavey
- Date:
- 2019-04-18
- Revision:
- 2:b3ca50f2e85d
- Parent:
- 1:e18615d46071
- Child:
- 3:5719d0fe94ed
File content as of revision 2:b3ca50f2e85d:
#include "PongEngine.h"
PongEngine::PongEngine()
{
}
PongEngine::~PongEngine()
{
}
void PongEngine::init(int paddle_width,int paddle_height,int ball_size,int speed)
{
// initialise the game parameters
_paddle_width = paddle_width;
_paddle_height = paddle_height;
_ball_size = ball_size;
_speed = speed;
// y position on screen - WIDTH is defined in N5110.h
_p1y = HEIGHT - GAP;
// puts paddles and ball in middle
_p1.init(_p1y,_paddle_height,_paddle_width);
_ball.init(_ball_size,_speed);
_grid1.init(3,GAP+1,WIDTH_BRICK,HEIGHT_BRICK); // need to figure out how to make a list of these
_grid2.init(13,GAP+1,WIDTH_BRICK,HEIGHT_BRICK);
_grid3.init(23,GAP+1,WIDTH_BRICK,HEIGHT_BRICK);
_grid4.init(33,GAP+1,WIDTH_BRICK,HEIGHT_BRICK);
_grid5.init(43,GAP+1,WIDTH_BRICK,HEIGHT_BRICK);
_grid6.init(53,GAP+1,WIDTH_BRICK,HEIGHT_BRICK);
_grid7.init(63,GAP+1,WIDTH_BRICK,HEIGHT_BRICK);
_grid8.init(73,GAP+1,WIDTH_BRICK,HEIGHT_BRICK);
_grid9.init(83,GAP+1,WIDTH_BRICK,HEIGHT_BRICK);
}
void PongEngine::read_input(Gamepad &pad)
{
_d = pad.get_direction();
_mag = pad.get_mag();
//_pause = pad.check_event(Gamepad::START_PRESSED); // == false then system("PAUSE") or cin.get()
}
void PongEngine::draw(N5110 &lcd)
{
// draw the elements in the LCD buffer
// pitch
lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
//score
print_scores(lcd);
// paddles
_p1.draw(lcd);
// ball
_ball.draw(lcd);
_grid1.draw(lcd);
_grid2.draw(lcd);
_grid3.draw(lcd);
_grid4.draw(lcd);
_grid5.draw(lcd);
_grid6.draw(lcd);
_grid7.draw(lcd);
_grid8.draw(lcd);
_grid9.draw(lcd);
// for(int i = GAP+1; i <= WIDTH; i+= WIDTH_BRICK + 1){
// for(int j = GAP+1; j <= (HEIGHT_BRICK + 1)*3; j+= HEIGHT_BRICK + 1){
// Vector3d grid[i][j]= _grid.init(i, j,WIDTH_BRICK,HEIGHT_BRICK);
// }
// }
}
void PongEngine::update(Gamepad &pad)
{
check_goal(pad);
// important to update paddles and ball before checking collisions so can
// correct for it before updating the display
_p1.update(_d,_mag);
_ball.update();
check_wall_collision(pad);
check_paddle_collisions(pad);
}
void PongEngine::check_wall_collision(Gamepad &pad)
{
// read current ball attributes
Vector2D ball_pos = _ball.get_pos();
Vector2D ball_velocity = _ball.get_velocity();
if (ball_pos.y <= 1) { // 1 due to 1 pixel boundary
ball_pos.y = 1; // bounce off ceiling without going off screen
ball_velocity.y = -ball_velocity.y;
// audio feedback
pad.tone(750.0,0.1);
}
else if (ball_pos.x <= 1) { // 1 due to 1 pixel boundary
ball_pos.x = 1; // bounce off ceiling without going off screen
ball_velocity.x = -ball_velocity.x;
// audio feedback
pad.tone(750.0,0.1);
}
// check if hit bottom wall
else if (ball_pos.x + _ball_size >= (WIDTH-1) ) { // bottom pixel is 47
// hit bottom
ball_pos.x = (WIDTH-1) - _ball_size; // stops ball going off screen
ball_velocity.x = -ball_velocity.x;
// audio feedback
pad.tone(750.0,0.1);
}
// update ball parameters
_ball.set_velocity(ball_velocity);
_ball.set_pos(ball_pos);
}
void PongEngine::check_paddle_collisions(Gamepad &pad)
{
// read current ball attributes
Vector2D ball_pos = _ball.get_pos();
Vector2D ball_velocity = _ball.get_velocity();
// check p1 first
Vector2D p1_pos = _p1.get_pos();
// see if ball has hit the paddle by checking for overlaps
if (
(ball_pos.y >= p1_pos.x) && //left
(ball_pos.y <= p1_pos.x + _paddle_width) && //right
(ball_pos.x >= _p1y) && //bottom
(ball_pos.x <= _p1y + _paddle_height) //top
) {
// if it has, fix position and reflect x velocity
ball_pos.y = _p1y + _paddle_height;
ball_velocity.y = -ball_velocity.y;
// audio feedback
pad.tone(1000.0,0.1);
}
// write new attributes
_ball.set_velocity(ball_velocity);
_ball.set_pos(ball_pos);
}
void PongEngine::check_goal(Gamepad &pad)
{
Vector2D ball_pos = _ball.get_pos();
// P1 has scored
if (ball_pos.y > HEIGHT) {
//lose_screen(); // go to loss screen then initialise again
_ball.init(_ball_size,_speed);
pad.tone(1500.0,0.5);
pad.leds_on();
wait(0.5);
pad.leds_off();
}
}
void PongEngine::print_scores(N5110 &lcd)
{
// get scores from paddles
int p1_score = _p1.get_score();
// print to LCD i
char buffer1[14];
sprintf(buffer1,"%2d",p1_score);
lcd.printString(buffer1,WIDTH/2 - 20,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
}