Last active
March 21, 2019 18:39
-
-
Save Sam-Belliveau/be637c9d85900c7e66ce71ff99750e53 to your computer and use it in GitHub Desktop.
Pong With an Unbeatable Robot. The bot is ment to feel as much like a human as possible. It is also supposed to be as hard as possible. (THAT DOESNT CHEAT)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SFML/Graphics.hpp> | |
#include <cstdlib> | |
#include <cmath> | |
const static float height = 720; | |
const static float width = 1280; | |
const static float pHeight = 12; | |
const static float pWidth = 100; | |
const static float fric = 0.75; | |
const static float pSpeed = 4; | |
const static float ballSize = 12; // output Size | |
const static float ballSpeed = 5.5; | |
const static float PY = height - pHeight; | |
const static float BY = pHeight; | |
const static float ReactionTime = 0.75; | |
static float PX = width/2; | |
static float PV = 0; | |
static float BX = width/2; | |
static float BV = 0; | |
static float ballLand = width/2; | |
static sf::Vector2f ballP(width/2, height/2); | |
static sf::Vector2f ballV(ballSpeed/2, ballSpeed); | |
static sf::VertexArray output(sf::Quads, 12); | |
// Create the main window | |
static sf::RenderWindow app(sf::VideoMode((int)width, (int)height), "Pong!"); | |
static void reset() | |
{ | |
ballP.y = height/2; | |
ballP.x = width/2; | |
ballV.y = ballSpeed; | |
ballV.x = (std::rand()%256)/16.0 - 8.0; | |
ballLand = width/2; | |
PX = width/2; | |
BX = width/2; | |
PV = 0; | |
BV = 0; | |
} | |
static void calBallPos() | |
{ | |
sf::Vector2f tempVel = ballV; | |
ballLand = ballP.x; | |
for(float tempY = ballP.y; BY + pHeight < tempY; tempY += tempVel.y) | |
{ | |
ballLand += tempVel.x; | |
if(ballLand < ballSize) tempVel.x = 0 - tempVel.x; | |
else if (ballLand > width - ballSize) tempVel.x = 0 - tempVel.x; | |
} | |
} | |
static void update() | |
{ | |
// Friction | |
PV *= fric; BV *= fric; | |
// Movement | |
// You | |
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) PV -= pSpeed; | |
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) PV += pSpeed; | |
// output | |
if(BX - ballP.y/ReactionTime - pWidth/8 > ballLand) BV -= pSpeed; | |
else if(BX + ballP.y/ReactionTime + pWidth/8 < ballLand) BV += pSpeed; | |
// Movement | |
PX += PV; BX += BV; | |
ballP.x += ballV.x; | |
ballP.y += ballV.y; | |
// Edge Detection | |
if(PX < pWidth) { PX = pWidth; PV = 0 - PV; } | |
else if (PX + pWidth > width) | |
{ PX = width - pWidth; PV = 0 - PV; } | |
if(BX < pWidth) { BX = pWidth; BV = 0 - BV; } | |
else if (BX + pWidth > width) { BX = width - pWidth; BV = 0 - BV; } | |
if(ballP.x < ballSize) ballV.x = 0 - ballV.x; | |
else if (ballP.x > width - ballSize) ballV.x = 0 - ballV.x; | |
// Goals | |
if(ballP.y < 0) { reset(); ballV.y = 0 - ballV.y; calBallPos(); } | |
if (ballP.y > height) reset(); | |
// Players Paddle | |
if(ballP.x > PX - pWidth && ballP.x < PX + pWidth) | |
{ | |
if(ballP.y + ballSize > PY - pHeight) | |
{ | |
ballP.y = PY - pHeight - ballSize; | |
ballV.y = 0 - ballV.y * (1.0 + std::abs(PV)/512.0); | |
ballV.x += PV; | |
ballV.x *= 5.0/8.0; | |
calBallPos(); | |
} | |
} | |
if(ballP.x > BX - pWidth && ballP.x < BX + pWidth) | |
{ | |
if(ballP.y - ballSize < BY + pHeight) | |
{ | |
ballP.y = BY + pHeight + ballSize; | |
ballV.y = 0 - ballV.y * (1.0 + std::abs(BV)/1024.0); | |
ballV.x += BV; | |
ballV.x *= 5.0/8.0; | |
} | |
} | |
// Drawing | |
output[0].position.x = ballP.x - ballSize; | |
output[0].position.y = ballP.y - ballSize; | |
output[1].position.x = ballP.x + ballSize; | |
output[1].position.y = ballP.y - ballSize; | |
output[2].position.x = ballP.x + ballSize; | |
output[2].position.y = ballP.y + ballSize; | |
output[3].position.x = ballP.x - ballSize; | |
output[3].position.y = ballP.y + ballSize; | |
output[4].position.x = PX - pWidth; | |
output[5].position.x = PX + pWidth; | |
output[6].position.x = PX + pWidth; | |
output[7].position.x = PX - pWidth; | |
output[8].position.x = BX - pWidth; | |
output[9].position.x = BX + pWidth; | |
output[10].position.x = BX + pWidth; | |
output[11].position.x = BX - pWidth; | |
app.clear(sf::Color(48,48,48)); | |
app.draw(output); | |
} | |
int main() | |
{ | |
output[4].position.y = PY - pHeight; | |
output[5].position.y = PY - pHeight; | |
output[6].position.y = PY + pHeight; | |
output[7].position.y = PY + pHeight; | |
output[8].position.y = BY - pHeight; | |
output[9].position.y = BY - pHeight; | |
output[10].position.y = BY + pHeight; | |
output[11].position.y = BY + pHeight; | |
output[12].position.y = BY + pHeight; | |
output[13].position.y = BY + pHeight; | |
output[14].position.y = BY + pHeight + 2*ballSize; | |
output[15].position.y = BY + pHeight + 2*ballSize; | |
output[0].color = sf::Color(255,255,255); | |
output[1].color = sf::Color(255,255,255); | |
output[2].color = sf::Color(255,255,255); | |
output[3].color = sf::Color(255,255,255); | |
output[4].color = sf::Color(255,255,255); | |
output[5].color = sf::Color(255,255,255); | |
output[6].color = sf::Color(255,255,255); | |
output[7].color = sf::Color(255,255,255); | |
output[8].color = sf::Color(255,255,255); | |
output[9].color = sf::Color(255,255,255); | |
output[10].color = sf::Color(255,255,255); | |
output[11].color = sf::Color(255,255,255); | |
app.setFramerateLimit(60); | |
// Start the game loop | |
while (app.isOpen()) | |
{ | |
// Process events | |
sf::Event event; | |
while (app.pollEvent(event)) | |
{ | |
// Close window : exit | |
if (event.type == sf::Event::Closed) | |
app.close(); | |
} | |
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) | |
{ | |
reset(); | |
app.clear(); | |
update(); | |
app.display(); | |
while(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {} | |
while(!sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) | |
{ | |
sf::Event event; | |
while (app.pollEvent(event)) | |
{ | |
// Close window : exit | |
if (event.type == sf::Event::Closed) | |
app.close(); | |
} | |
} | |
while(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {} | |
} | |
// Clear screen | |
update(); | |
// Update the window | |
app.display(); | |
} | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The bot predicts where the ball will go
It is really cool