Hello gentlemen
I'm faced with an annoying issue which drives so mad, that came here to ask an advice.
First of all, English is not my native language, so apologize if I messed up
Here is my code of a simple Pong game:
[spoiler]
#include <SFML/Graphics.hpp>
using namespace sf;
int const w = 800;
int const h = 600;
class Ball
{
public:
float dx, dy;
FloatRect rect;
Sprite sprite;
Ball(Texture &image)
{
dx, dy = -10;
sprite.setTexture(image);
rect = FloatRect(w, (rand() % 500) * 10, 10, 10);
}
void update()
{
rect.top += dy;
rect.left += dx;
sprite.setPosition(rect.left, rect.top);
}
};
class Player
{
public:
float dy;
FloatRect rect;
Sprite sprite;
bool isMoveUp;
bool isMoveDown;
Player(Texture &image)
{
sprite.setTexture(image);
rect = FloatRect(0, h / 2, 32, 128);
dy = 10;
isMoveUp = false;
isMoveDown = false;
}
void update()
{
sprite.setPosition(rect.left, rect.top);
}
void movePlayer(char direction)
{
if (direction == 'U')
{
if (rect.top>0)
rect.top -= dy;
}
else if (direction == 'D')
{
if (rect.top + rect.height < h)
rect.top += dy;
}
}
};
int main()
{
Texture playerTexture;
if (!playerTexture.loadFromFile("player.png"))
{
return 1;
}
Texture ballTexture;
if (!ballTexture.loadFromFile("ball.png"))
{
return 1;
}
Player * player = new Player(playerTexture);
Ball * ball = new Ball(ballTexture);
RenderWindow window(VideoMode(w, h), "The Pong game");
window.setFramerateLimit(60);
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
{
window.close();
{
delete[] ball, player;
break;
}
}
if (event.type == Event::KeyPressed)
{
if (event.key.code == Keyboard::Down)
{
player->isMoveDown = true;
player->isMoveUp = false;
}
if (event.key.code == Keyboard::Up)
{
player->isMoveUp = true;
player->isMoveDown = false;
}
}
if (event.type == Event::KeyReleased)
{
if (event.key.code == Keyboard::Down)
player->isMoveDown = false;
if (event.key.code == Keyboard::Up)
player->isMoveUp = false;
}
}
if (player->isMoveDown)
{
player->dy = 10;
player->movePlayer('D');
}
if (player->isMoveUp)
{
player->dy = 10;
player->movePlayer('U');
}
if (ball->rect.intersects(player->rect))
{
ball->dx = 10;
}
if (ball->rect.left >= w)
{
ball->dx = -10;
}
if (ball->rect.top >= h)
{
ball->dy = -10;
}
if (ball->rect.top <= 0)
{
ball->dy = 10;
}
window.clear(Color::Black);
ball->update();
player->update();
window.draw(player->sprite);
window.draw(ball->sprite);
window.display();
}
return 0;
}
[/spoiler]
Issues:
1. Before I created both player and ball in heap and delete[] them after window closed, program process remains running in system.
I'm not sure why do I have to have these objects in heap instead of stack.. and why the program behaves like that?
2. I'm trying to test the program in case images(textures) were not found. And you'll not believe me, the program remains running every single time I launch it.
Why return 1; hasn't killed the process?..
I'm a newbie to c++ and especially to c++ windows programming in visual studio[burn in hell VS!!! jk].
Spent much time googling, but it seems I can't even manage to perform a proper request.
PS: if it's possible, I would be very appreciate if you guys will be able to review my code and point me to weak parts.
Thank you in advance!