I want to make a pong game purely to practice since i am a beginner in programming, but nothing is moving on screen. Can someone help me fix this?
#include "game.h"
#include <SFML/Graphics.hpp>
bool game::isClosing()
{
while (this->Game.isOpen())
{
while (this->Game.pollEvent(this->event))
{
if (this->event.type == sf::Event::Closed)
{
return true;
}
}
}
return false;
}
void game::resetGame()
{
Ball.circle.setPosition(this->Game.getSize().x / 2.0f, this->Game.getSize().y / 2.0f);
player1.rectangle.setPosition(player1.rectangle.getPosition().x, 450);
player2.rectangle.setPosition(player2.rectangle.getPosition());
}
void game::render()
{
this->Game.clear();
this->Game.draw(Ball.circle);
this->Game.draw(player1.rectangle);
this->Game.draw(player2.rectangle);
this->Game.display();
}
void game::run()
{
while (this->Game.isOpen())
{
player1.movement();
player2.movement();
Ball.movement();
Ball.bounce(player1);
Ball.bounce(player2);
this->render();
if (this->isClosing())
{
this->Game.close();
}
}
}
#include "player.h"
#include <SFML/Graphics.hpp>
void player::movement()
{
switch (this->side)
{
case 'l':
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
this->rectangle.move(0, -this->speed);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
this->rectangle.move(0, this->speed);
}
break;
case 'r':
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
this->rectangle.move(0, -this->speed);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
this->rectangle.move(0, this->speed);
}
break;
default:
break;
}
}
#include "ball.h"
#include <SFML/Graphics.hpp>
void ball::movement()
{
this->circle.move(this->xSpeed, this->ySpeed);
}
void ball::bounce(const player& player)
{
if (this->circle.getGlobalBounds().intersects(player.rectangle.getGlobalBounds()))
{
if (this->ySpeed < 0)
{
if (this->circle.getPosition().y < player.rectangle.getPosition().y)
{
this->xSpeed *= -1;
}
else if (this->circle.getPosition().y > player.rectangle.getPosition().y)
{
this->xSpeed *= -1;
this->ySpeed *= -1;
}
}
else if (this->ySpeed > 0)
{
if (this->circle.getPosition().y < player.rectangle.getPosition().y)
{
this->xSpeed *= -1;
this->ySpeed *= -1;
}
else if (this->circle.getPosition().y > player.rectangle.getPosition().y)
{
this->xSpeed *= -1;
}
}
}
}