Cześć, przejdźmy do rzeczy mam kod:
main.cpp
#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include "Player.h"
int main()
{
sf::RenderWindow window( sf::VideoMode( 800, 600 ), "My window" );
Player p1( 400, 300 );
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( p1 );
window.display();
}
return 0;
}
Player.h
#pragma once
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
using namespace sf;
using namespace std;
class Player
{
public:
Player( float t_x, float t_y );
Player() = delete;
~Player() = default;
void update();
void draw( RenderTarget& target, RenderStates state ) const override;
float left();
float right();
float top();
float bottom();
private:
CircleShape shape;
const float ballRadius { 10.0f };
const float playerVelocity { 2.f };
Vector2f velocity { playerVelocity, playerVelocity };
};
Player.cpp
#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include "Player.h"
using namespace sf;
using namespace std;
Player::Player( float t_x, float t_y )
{
shape.setPosition( t_x, t_y );
shape.setRadius( this->ballRadius );
shape.setFillColor( Color::White );
shape.setOrigin( this->ballRadius, this->ballRadius );
}
void Player::draw( RenderTarget & target, RenderStates state ) const
{
target.draw( this->shape, state );
}
void Player::update()
{
shape.move( this->velocity );
if( this->left() < 0 )
{
velocity.x = playerVelocity;
}
else if( this->right() > 800 )
{
velocity.x = -playerVelocity;
}
if( this->top() < 0 )
{
velocity.y = playerVelocity;
}
else if( this->bottom() > 600 )
{
velocity.y = -playerVelocity;
}
}
float Player::left()
{
return this->shape.getPosition().x - shape.getRadius();
}
float Player::right()
{
return this->shape.getPosition().x + shape.getRadius();
}
float Player::top()
{
return this->shape.getPosition().y - shape.getRadius();
}
float Player::bottom()
{
return this->shape.getPosition().y + shape.getRadius();
}
I otrzymuję takie komunikaty:
https://imgur.com/a/Oy9vt
Nie jestem w stanie sam sobie z tym poradzić, proszę o pomoc i za takową dziękuję z góry. :-) Miłej nocy.