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.
Revision 1:25a839625a1e, committed 2017-03-01
- Comitter:
- eencae
- Date:
- Wed Mar 01 13:55:16 2017 +0000
- Parent:
- 0:e1442f3aa3c7
- Child:
- 2:7635d5c9b468
- Commit message:
- Beta version of Pong.
Changed in this revision
--- a/Ball.lib Wed Feb 08 19:51:44 2017 +0000 +++ b/Ball.lib Wed Mar 01 13:55:16 2017 +0000 @@ -1,1 +1,1 @@ -Ball#a695d2b64167 +https://developer.mbed.org/users/eencae/code/Ball/#201ef6494656
--- a/Paddle.lib Wed Feb 08 19:51:44 2017 +0000 +++ b/Paddle.lib Wed Mar 01 13:55:16 2017 +0000 @@ -1,1 +1,1 @@ -Paddle#fe2b9c70f7b8 +https://developer.mbed.org/users/eencae/code/Paddle/#5285b567f4c9
--- a/PongEngine.lib Wed Feb 08 19:51:44 2017 +0000 +++ b/PongEngine.lib Wed Mar 01 13:55:16 2017 +0000 @@ -1,1 +1,1 @@ -PongEngine#1800b63ad78c +https://developer.mbed.org/users/eencae/code/PongEngine/#137597c36f10
--- a/main.cpp Wed Feb 08 19:51:44 2017 +0000
+++ b/main.cpp Wed Mar 01 13:55:16 2017 +0000
@@ -2,16 +2,13 @@
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
-#include "Paddle.h"
-#include "Ball.h"
#include "PongEngine.h"
#define PADDLE_WIDTH 2
#define PADDLE_HEIGHT 10
-#define P1_X 1
-#define P2_X WIDTH - PADDLE_WIDTH - P1_X
#define BALL_RADIUS 2
#define BALL_SPEED 3
+
/////////////// structs /////////////////
struct UserInput {
Direction d;
@@ -20,25 +17,24 @@
/////////////// objects ///////////////
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad pad;
-Paddle p1;
-Paddle p2;
-Ball ball;
PongEngine pong;
///////////// prototypes ///////////////
void init();
-UserInput read_input();
void update_game(UserInput input);
void render();
///////////// functions ////////////////
int main()
{
- init();
+ int fps = 8;
- int fps = 3;
+ init();
+
+ render(); // draw initial frame
+ wait(1.0f/fps);
while (1) {
- UserInput input = read_input();
- update_game(input);
+ pong.read_input(pad);
+ pong.update(pad);
render();
wait(1.0f/fps);
}
@@ -49,35 +45,18 @@
lcd.init();
pad.init();
- p1.init(P1_X,PADDLE_HEIGHT,PADDLE_WIDTH);
- p2.init(P2_X,PADDLE_HEIGHT,PADDLE_WIDTH);
- ball.init(BALL_RADIUS,BALL_SPEED,pad);
- pong.init(); // does nothing yet
-}
+ float rand1 = pad.read_pot();
+ Vector2D rand2 = pad.get_coord();
+ int seed = int(1.0e6f*(rand1 + (rand2.x/rand2.y)));
+ // create some random number from ADC pins to use in srand
+
+ pong.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_RADIUS,BALL_SPEED,seed);
-UserInput read_input()
-{
- Direction d = pad.get_direction();
- float mag = pad.get_mag();
- UserInput input = {d,mag};
- return input;
-}
-
-void update_game(UserInput input)
-{
- // important to update paddles and ball before checking collisions so can
- // correct for it before updating the display
- p1.update(input.d,input.mag);
- p2.update(input.d,input.mag);
- ball.update();
- pong.check_collisions(ball,p1,p2);
}
void render()
{
lcd.clear(); // clear screen, re-draw and refresh
- p1.draw(lcd);
- p2.draw(lcd);
- ball.draw(lcd);
+ pong.draw(lcd);
lcd.refresh();
}
\ No newline at end of file