//Main.cpp
#include <SFML/Graphics.hpp>
#include <iostream>
Why include
iostream? You don't use aything from that header.
#include "Player.h"
#include "ball.h"
int main();
No need to forward declare main - it's the first function in the file.
sf::RenderWindow window(sf::VideoMode(720, 480), "Pong");
Player player(285, 420);
Ball ball(100, 500);
Try to avoid global variables, they increase coupling and cause many other problems (search the forum for examples). Especially avoid globals of SFML types (again, search the forum for more).
int main() {
Why all the vertical whitespace?
player.setPos();
ball.setPos();
Why doesn't the constructor(s) just do this?
window.setFramerateLimit(60);
window.setVerticalSyncEnabled(true);
Use one or the other, not both, they can interact badly. See the tutorial for details.
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear(sf::Color::Black);
window.draw(player.getrPlayer());
window.draw(ball.getBall());
Youv'e just declared player and ball as globals (btw; just declare them inside main()), so why the getXXX functions?
player.update();
ball.ballCollisions(player.getrPlayer());
ball.update();
window.display();
}
return 0;
Unneeded. Falling off the end of main without returning a value is guaranteed by the standard to return EXIT_SUCCESS.
}
//ball.h
#pragma once
Classic header guards are more broadly supported than
#pragma once and does the same thing.
#include <SFML\Graphics.hpp>
Don't use backslashes in includes (or other file paths). They only work on Windows, but front-slash "
/" works on all platforms
including Windows.
class Ball {
public:
Ball(int x, int y);
sf::CircleShape getBall();
void setPos();
void ballMovement();
void update();
int xVel;
int yVel;
Why not use sf::Vector2i here? Velocity is a Vector and that's exactly what that class is designed to hold.
void ballCollisions(sf::RectangleShape rectangle);
sf::RectangleShape is a fairly small class, but since you don't modify it or otherwise
need a copy, I'd suggest passing it as a const reference instead of by value.
private :
int x;
int y;
Suggest using sf::Vector2 here.
public:
int getX();
int getY();
int getXVel();
int getYVel();
Why are these not together with the other public members?
};
//ball.cpp
#include "ball.h"
#include "Player.h"
Why is this file including Player.h?
sf::CircleShape ball(10);
int xVel = 5;
int yVel = 5;
More global variables. You
really should stop using those.
Ball::Ball(int x, int y) {
this->x = x;
this->y = x;
}
The use of
this-> here is completly superfluous.
You really should use an initializer list rather than the constructor body to initialize members.
You are not initializing xVel and yVel - leaving them uninitialized means they will get 'random' values at runtime (at least in release builds).
int Ball::getX() { return x; }
int Ball::getY() { return y; }
sf::CircleShape Ball::getBall() {
return ball;
}
void Ball::setPos() {
ball.setPosition(x, y);
}
This function will set your balls position to its initial position every time it is called.
void Ball::ballMovement() {
ball.move(xVel, yVel);
}
void Ball::update() {
ballMovement();
}
Why have two functions when all one does is just call the other?
int Ball::getXVel() { return xVel; }
int Ball::getYVel() { return yVel; }
void Ball::ballCollisions(sf::RectangleShape rectangle) {
if (ball.getGlobalBounds().intersects(rectangle.getGlobalBounds())) {
xVel *= -1;
yVel *= -1;
}
}
Think a bit about the size of rectangle in this function.
Also, the bouncing ball example Satus hints at is here:
https://github.com/SFML/SFML/wiki/Source:-Bouncing-ballAnd don't delete topics/posts please. Others may come by and find them useful.