Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: nothing is moving on screen  (Read 3749 times)

0 Members and 1 Guest are viewing this topic.

alimao

  • Newbie
  • *
  • Posts: 5
    • View Profile
nothing is moving on screen
« on: January 17, 2022, 08:13:22 pm »
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;
                        }
                }
        }
}
« Last Edit: January 17, 2022, 08:27:45 pm by alimao »

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: nothing is moving on screen
« Reply #1 on: January 17, 2022, 08:46:59 pm »
Your game::isClosing() contains a while loop that won't let game::isClosing() return until the window is closed. If the window is open, the outer while loop will be an infinite loop and the rest of the game will never get to run.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: nothing is moving on screen
« Reply #2 on: January 17, 2022, 09:23:21 pm »
You have an infinite (until there's a Closed event) loop inside your isClosing function.

alimao

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: nothing is moving on screen
« Reply #3 on: January 17, 2022, 09:28:19 pm »
Yes, that was the issue, thanks for helping me

 

anything