generated from sigonasr2/CPlusPlusProjectTemplate
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
230 lines
6.0 KiB
230 lines
6.0 KiB
#define OLC_PGE_APPLICATION
|
|
#include "olcPixelGameEngine.h"
|
|
|
|
enum state{
|
|
TITLE_SCREEN,
|
|
RUNNING_GAME,
|
|
WIN_GAME,
|
|
LOSE_GAME
|
|
};
|
|
|
|
class Example : public olc::PixelGameEngine
|
|
{
|
|
float ballx = 200;
|
|
float bally = 200;
|
|
float ballXSpd = -80;
|
|
float ballYSpd = 80;
|
|
float ballRadius = 10;
|
|
|
|
float paddlex = 20;
|
|
float paddley;
|
|
float paddleWidth = 30;
|
|
float paddleHeight = 100;
|
|
|
|
float enemyPaddleX = 20;
|
|
float enemyPaddleY;
|
|
float enemyPaddleWidth = 30;
|
|
float enemyPaddleHeight = 100;
|
|
|
|
int playerScore = 0;
|
|
int enemyScore = 0;
|
|
|
|
state GameState = TITLE_SCREEN;
|
|
|
|
public:
|
|
Example()
|
|
{
|
|
sAppName = "Example";
|
|
}
|
|
|
|
public:
|
|
bool OnUserCreate() override
|
|
{
|
|
// Called once at the start, so create things here
|
|
|
|
paddley = ScreenHeight()/2;
|
|
enemyPaddleX = ScreenWidth()-20;
|
|
enemyPaddleY = ScreenHeight()/2;
|
|
|
|
srand(time(NULL));
|
|
|
|
return true;
|
|
}
|
|
|
|
bool OnUserUpdate(float fElapsedTime) override
|
|
{
|
|
// called once per frame
|
|
|
|
Clear(olc::VERY_DARK_GREY);
|
|
|
|
if (GameState == TITLE_SCREEN) {
|
|
std::string titleText = "PONG";
|
|
std::string spacebarText = "Press <SPACE> to Begin!";
|
|
|
|
DrawString(ScreenWidth() / 2 - GetTextSize(titleText).x/2*6, 80 - GetTextSize(titleText).y/2*6, titleText, olc::BLACK, 6);
|
|
DrawString(ScreenWidth() / 2 - GetTextSize(spacebarText).x/2*3, 240 - GetTextSize(spacebarText).y/2*3, spacebarText, olc::BLACK, 3);
|
|
|
|
if (GetKey(olc::SPACE).bPressed) {
|
|
GameState = RUNNING_GAME;
|
|
playerScore = 0;
|
|
enemyScore = 0;
|
|
}
|
|
} else
|
|
if (GameState == WIN_GAME) {
|
|
Clear(olc::DARK_BLUE);
|
|
std::string winText = "YOU WIN!";
|
|
std::string spacebarText = "Press <SPACE> to Play Again!";
|
|
|
|
DrawString(ScreenWidth() / 2 - GetTextSize(winText).x/2*6, 80 - GetTextSize(winText).y/2*6, winText, olc::WHITE, 6);
|
|
DrawString(ScreenWidth() / 2 - GetTextSize(spacebarText).x/2*2, 240 - GetTextSize(spacebarText).y/2*2, spacebarText, olc::WHITE, 2);
|
|
|
|
if (GetKey(olc::SPACE).bPressed) {
|
|
GameState = TITLE_SCREEN;
|
|
}
|
|
} else
|
|
if (GameState == LOSE_GAME) {
|
|
Clear(olc::DARK_RED);
|
|
std::string loseText = "YOU LOSE!";
|
|
std::string spacebarText = "Press <SPACE> to Play Again!";
|
|
|
|
DrawString(ScreenWidth() / 2 - GetTextSize(loseText).x/2*6, 80 - GetTextSize(loseText).y/2*6, loseText, olc::WHITE, 6);
|
|
DrawString(ScreenWidth() / 2 - GetTextSize(spacebarText).x/2*2, 240 - GetTextSize(spacebarText).y/2*2, spacebarText, olc::WHITE, 2);
|
|
|
|
if (GetKey(olc::SPACE).bPressed) {
|
|
GameState = TITLE_SCREEN;
|
|
}
|
|
} else
|
|
if (GameState == RUNNING_GAME) {
|
|
|
|
ballx += ballXSpd * fElapsedTime; // move the ball horizontally
|
|
bally += ballYSpd * fElapsedTime; // move the ball vertically
|
|
|
|
//Related to Ball Horizontal Collision Checking
|
|
float ballLeft = ballx - ballRadius;
|
|
float ballRight = ballx + ballRadius;
|
|
float paddleRight = paddlex + paddleWidth/2;
|
|
float enemyPaddleLeft = enemyPaddleX - enemyPaddleWidth/2;
|
|
|
|
//Related to Ball Vertical Collision Checking
|
|
float ballTop = bally - ballRadius;
|
|
float ballBottom = bally + ballRadius;
|
|
float paddleTop = paddley - paddleHeight/2;
|
|
float paddleBottom = paddley + paddleHeight/2;
|
|
float enemyPaddleTop = enemyPaddleY - enemyPaddleHeight/2;
|
|
float enemyPaddleBottom = enemyPaddleY + enemyPaddleHeight/2;
|
|
|
|
if (
|
|
ballLeft < paddleRight &&
|
|
ballTop < paddleBottom &&
|
|
ballBottom > paddleTop
|
|
) { //If the ball collides with the paddle.
|
|
ballXSpd *= -1.1;
|
|
ballYSpd *= 1.1;
|
|
ballx=paddleRight+ballRadius;
|
|
}
|
|
|
|
if (
|
|
ballRight > enemyPaddleLeft &&
|
|
ballTop < enemyPaddleBottom &&
|
|
ballBottom > enemyPaddleTop
|
|
) { //If the ball collides with the paddle.
|
|
ballXSpd *= -1.1;
|
|
ballYSpd *= 1.1;
|
|
ballx=enemyPaddleLeft-ballRadius;
|
|
}
|
|
|
|
if (ballBottom > ScreenHeight()) { //If the ball touches the bottom edge, reverse its Y direction.
|
|
ballYSpd *= -1;
|
|
bally = ScreenHeight()-ballRadius;
|
|
}
|
|
if ( //If the ball touches the top edge, reverse its Y direction.
|
|
ballTop < 0
|
|
) {
|
|
ballYSpd *= -1;
|
|
bally = ballRadius;
|
|
}
|
|
|
|
if (ballRight > ScreenWidth()) { //If the ball touches the right edge, award a point to the player.
|
|
playerScore += 1;
|
|
ballx = ScreenWidth()/2;
|
|
bally = ScreenHeight()/2;
|
|
|
|
ballXSpd = 80;
|
|
ballYSpd = 80;
|
|
|
|
int randomNumber = rand() % 2; //Get a random number that is 0 or 1.
|
|
if (randomNumber == 0) {
|
|
ballXSpd *= -1;
|
|
}
|
|
|
|
randomNumber = rand() % 2;
|
|
if (randomNumber == 0) {
|
|
ballYSpd *= -1;
|
|
}
|
|
}
|
|
if (ballLeft < 0) { //If the ball touches the left edge, award a point to the enemy.
|
|
enemyScore += 1;
|
|
ballx = ScreenWidth()/2;
|
|
bally = ScreenHeight()/2;
|
|
|
|
ballXSpd = 80;
|
|
ballYSpd = 80;
|
|
|
|
int randomNumber = rand() % 2; //Get a random number that is 0 or 1.
|
|
if (randomNumber == 0) {
|
|
ballXSpd *= -1;
|
|
}
|
|
|
|
randomNumber = rand() % 2;
|
|
if (randomNumber == 0) {
|
|
ballYSpd *= -1;
|
|
}
|
|
}
|
|
|
|
if (GetKey(olc::UP).bHeld) {
|
|
paddley -= 120 * fElapsedTime; //move the paddle up at 120 pixels / s
|
|
}
|
|
if (GetKey(olc::DOWN).bHeld) {
|
|
paddley += 120 * fElapsedTime; //move the paddle down at 120 pixels / s
|
|
}
|
|
|
|
if (bally < enemyPaddleY) {
|
|
enemyPaddleY -= 100 * fElapsedTime;
|
|
}
|
|
if (bally > enemyPaddleY) {
|
|
enemyPaddleY += 100 * fElapsedTime;
|
|
}
|
|
|
|
if (playerScore >= 5) {
|
|
GameState = WIN_GAME;
|
|
}
|
|
if (enemyScore >= 5) {
|
|
GameState = LOSE_GAME;
|
|
}
|
|
|
|
|
|
DrawCircle(ballx,bally,ballRadius,olc::YELLOW);
|
|
FillRect(paddlex - paddleWidth/2, paddley - paddleHeight/2, paddleWidth, paddleHeight, olc::GREEN); //Draw a paddle centered at the paddle position.
|
|
FillRect(enemyPaddleX - enemyPaddleWidth/2, enemyPaddleY - enemyPaddleHeight/2, enemyPaddleWidth, enemyPaddleHeight, olc::DARK_RED); //Draw a paddle centered at the paddle position.
|
|
|
|
DrawString(30,30,std::to_string(playerScore),olc::WHITE,4);
|
|
|
|
std::string enemyScoreText = std::to_string(enemyScore);
|
|
|
|
DrawString(ScreenWidth() - 30 - GetTextSize(enemyScoreText).x*4 , 30, enemyScoreText,olc::WHITE, 4);
|
|
|
|
}
|
|
|
|
return true;
|
|
}
|
|
};
|
|
|
|
|
|
int main()
|
|
{
|
|
Example demo;
|
|
if (demo.Construct(640, 480, 4, 4))
|
|
demo.Start();
|
|
|
|
return 0;
|
|
}
|
|
|