
Display text on screen.
Dependencies: TextLCD mbed MaxSonar RTC-DS1307
Fork of TextLCD_HelloWorld by
Diff: main.cpp
- Revision:
- 13:95eea857ed37
- Parent:
- 12:75b8081e1304
- Child:
- 14:2bde99b3345c
--- a/main.cpp Thu May 25 12:09:54 2017 +0000 +++ b/main.cpp Thu May 25 13:19:04 2017 +0000 @@ -48,6 +48,9 @@ //all functions for pong game void Pong(); +void UpdatePlayerLocation(); +void UpdateBallLocation(); +void Draw(); MaxSonar *range1; float r1; @@ -81,7 +84,8 @@ ClearAllScreen(); - GameOption(); + Pong(); + //GameOption(); } int gameOptionLocation = 0; @@ -205,22 +209,26 @@ } } -void Pong() +//player +int pLocation = 5; +string player = "___"; + +//ball +string ball = 0; + +int ballPX = 0; +int ballPY = 0; + +int ballVX = 1; +int ballVY = 1; + +void Pong() //this is a version of pong where you can't see the middle part of the board (15*10 pixel but only 4 horrizontal pixels are displaying) { bool pong = true; - //player - int pLocation = 5; - string player = "___"; + - //ball - string ball = 0; - int ballPX = 0; - int ballPY = 0; - - int ballVX = 1; - int ballVY = 1; UpdateScreen(questionScreen, "Welcome to Pong!"); UpdateScreen(screen2, 0, "", pLocation, player); @@ -233,27 +241,62 @@ GameOption(); } - if(r2 < 20) - { - if(pLocation > 0) - { - pLocation--; - UpdateScreen(screen2, 0, "", pLocation, player); - } - } - if(r1 < 20) - { - if(pLocation < 12) - { - pLocation++; - UpdateScreen(screen2, 0, "", pLocation, player); - } - } + UpdatePlayerLocation(); + UpdateBallLocation(); - wait(0.5); + Draw(); } } +void UpdatePlayerLocation() +{ + if(r2 < 20) + { + if(pLocation > 0) + { + pLocation--; + } + } + if(r1 < 20) + { + if(pLocation < 12) + { + pLocation++; + } + } + + wait(0.5); +} + +void UpdateBallLocation() +{ + if(ballPY == 0) { ballVY = 1; } + if(ballPY == 9) { ballVY = -1; } + if(ballPX == 0) { ballVX = 1; } + if(ballPX == 15) { ballVX = -1; } + + ballPY += ballVY; + ballPX += ballVX; +} + +void Draw() +{ + //draw ball also + if(ballPY < 2 || ballPY > 7) + { + if(ballPY == 0) { UpdateScreen(screen1, ballPX, "0", 0, ""); } + if(ballPY == 1) { UpdateScreen(screen1, 0, "", ballPX, "0"); } + if(ballPY == 8) { UpdateScreen(screen2, ballPX, "0", 0, ""); } + if(ballPY == 9) { UpdateScreen(screen2, 0, "", ballPX, "0"); } + } + else //only draw player and enermy + { + ClearAllScreen(); + } +} + + + string exitOption[2] = {"Yes", "No"}; bool CheckExit(float r1, float r2) {