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.
Diff: PongEngine/PongEngine.cpp
- Revision:
- 7:ef162a6232ca
- Parent:
- 6:7f79f320b827
- Child:
- 8:1ab6d90c4d60
--- a/PongEngine/PongEngine.cpp Sat Apr 20 19:25:28 2019 +0000
+++ b/PongEngine/PongEngine.cpp Sun Apr 21 12:45:11 2019 +0000
@@ -1,5 +1,7 @@
#include "PongEngine.h"
+
+
PongEngine::PongEngine()
{
@@ -25,12 +27,47 @@
_p1.init(_p1y,_paddle_height,_paddle_width);
_ball.init(_ball_size,_speed);
- _brick1.init(3,GAP+1,HEIGHT_BRICK,WIDTH_BRICK); // need to figure out how to make a list of these
- _brick2.init(16,GAP+1,HEIGHT_BRICK,WIDTH_BRICK);
- _brick3.init(29,GAP+1,HEIGHT_BRICK,WIDTH_BRICK);
- _brick4.init(42,GAP+1,HEIGHT_BRICK,WIDTH_BRICK);
- _brick5.init(55,GAP+1,HEIGHT_BRICK,WIDTH_BRICK);
- _brick6.init(68,GAP+1,HEIGHT_BRICK,WIDTH_BRICK);
+ _brick11.init(3,GAP+1,HEIGHT_BRICK,WIDTH_BRICK); // need to figure out how to make a list of these
+ _brick12.init(16,GAP+1,HEIGHT_BRICK,WIDTH_BRICK);
+ _brick13.init(29,GAP+1,HEIGHT_BRICK,WIDTH_BRICK);
+ _brick14.init(42,GAP+1,HEIGHT_BRICK,WIDTH_BRICK);
+ _brick15.init(55,GAP+1,HEIGHT_BRICK,WIDTH_BRICK);
+ _brick16.init(68,GAP+1,HEIGHT_BRICK,WIDTH_BRICK);
+
+ _brick21.init(3,GAP+HEIGHT_BRICK+2,HEIGHT_BRICK,WIDTH_BRICK); // need to figure out how to make a list of these
+ _brick22.init(16,GAP+HEIGHT_BRICK+2,HEIGHT_BRICK,WIDTH_BRICK);
+ _brick23.init(29,GAP+HEIGHT_BRICK+2,HEIGHT_BRICK,WIDTH_BRICK);
+ _brick24.init(42,GAP+HEIGHT_BRICK+2,HEIGHT_BRICK,WIDTH_BRICK);
+ _brick25.init(55,GAP+HEIGHT_BRICK+2,HEIGHT_BRICK,WIDTH_BRICK);
+ _brick26.init(68,GAP+HEIGHT_BRICK+2,HEIGHT_BRICK,WIDTH_BRICK);
+
+ _brick31.init(3,GAP+1+((HEIGHT_BRICK+1)*2),HEIGHT_BRICK,WIDTH_BRICK); // need to figure out how to make a list of these
+ _brick32.init(16,GAP+1+((HEIGHT_BRICK+1)*2),HEIGHT_BRICK,WIDTH_BRICK);
+ _brick33.init(29,GAP+1+((HEIGHT_BRICK+1)*2),HEIGHT_BRICK,WIDTH_BRICK);
+ _brick34.init(42,GAP+1+((HEIGHT_BRICK+1)*2),HEIGHT_BRICK,WIDTH_BRICK);
+ _brick35.init(55,GAP+1+((HEIGHT_BRICK+1)*2),HEIGHT_BRICK,WIDTH_BRICK);
+ _brick36.init(68,GAP+1+((HEIGHT_BRICK+1)*2),HEIGHT_BRICK,WIDTH_BRICK);
+
+ listofBricks.push_back(_brick11);
+ listofBricks.push_back(_brick12);
+ listofBricks.push_back(_brick13);
+ listofBricks.push_back(_brick14);
+ listofBricks.push_back(_brick15);
+ listofBricks.push_back(_brick16);
+
+ listofBricks.push_back(_brick21);
+ listofBricks.push_back(_brick22);
+ listofBricks.push_back(_brick23);
+ listofBricks.push_back(_brick24);
+ listofBricks.push_back(_brick25);
+ listofBricks.push_back(_brick26);
+
+ listofBricks.push_back(_brick31);
+ listofBricks.push_back(_brick32);
+ listofBricks.push_back(_brick33);
+ listofBricks.push_back(_brick34);
+ listofBricks.push_back(_brick35);
+ listofBricks.push_back(_brick36);
}
void PongEngine::read_input(Gamepad &pad)
@@ -52,20 +89,44 @@
// ball
_ball.draw(lcd);
- _brick1.draw(lcd);
- _brick2.draw(lcd);
- _brick3.draw(lcd);
- _brick4.draw(lcd);
- _brick5.draw(lcd);
- _brick6.draw(lcd);
+ _brick11.draw(lcd);
+ _brick12.draw(lcd);
+ _brick13.draw(lcd);
+ _brick14.draw(lcd);
+ _brick15.draw(lcd);
+ _brick16.draw(lcd);
+
+ _brick21.draw(lcd);
+ _brick22.draw(lcd);
+ _brick23.draw(lcd);
+ _brick24.draw(lcd);
+ _brick25.draw(lcd);
+ _brick26.draw(lcd);
+
+ _brick31.draw(lcd);
+ _brick32.draw(lcd);
+ _brick33.draw(lcd);
+ _brick34.draw(lcd);
+ _brick35.draw(lcd);
+ _brick36.draw(lcd);
+
+
+
+// for (it = listofBricks.begin(); it != listofBricks.end(); it++)
+// {
+// int x = it->_x;
+// int y = it->_y;
+
+// lcd.drawRect(_x,_y,_width,_height,FILL_TRANSPARENT);
+// }
// 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]= _brick.init(i, j,WIDTH_BRICK,HEIGHT_BRICK);
+// for(int j = GAP+1; j <= (HEIGHT_BRICK + 1)*3; j+= HEIGHT_BRICK + 1){
+// Vector2D _grid[i][j]= _brick.init(i, j,WIDTH_BRICK,HEIGHT_BRICK);
// }
// }
}
@@ -152,28 +213,28 @@
// read current ball attributes
Vector2D ball_pos = _ball.get_pos();
Vector2D ball_velocity = _ball.get_velocity();
-
+
// check p1 first
- Vector2D _brick1_pos = _brick1.get_pos();
-
+ Vector2D _brick11_pos = _brick11.get_pos();
+
// see if ball has hit the paddle by checking for overlaps
if (
- (ball_pos.x >= _brick1_pos.x) && //left
- (ball_pos.x <= _brick1_pos.x + WIDTH_BRICK) && //right
- (ball_pos.y >= _brick1_pos.y) && //bottom
- (ball_pos.y <= _brick1_pos.y + HEIGHT_BRICK) //top
+ (ball_pos.x >= _brick11_pos.x) && //left
+ (ball_pos.x <= _brick11_pos.x + WIDTH_BRICK) && //right
+ (ball_pos.y >= _brick11_pos.y) && //bottom
+ (ball_pos.y <= _brick11_pos.y + HEIGHT_BRICK) //top
) { // edit this so that if it hits the middle, reflect, else change angle depending on how far off centre (add angle to ball)
// if it has, fix position and reflect x velocity
- ball_pos.y = _brick1_pos.y;
+ ball_pos.y = _brick11_pos.y;
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);
- _brick1.hit();
+ _brick11.hit();
}
void PongEngine::check_goal(Gamepad &pad)