#include "pch.h"
#include "Ball.h"
Ball::Ball(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 Ball::draw(RenderTarget& target, RenderStates state) const //wlasciwosci obiektu
{
target.draw(this->shape, state);
}
void Ball::update()
{
shape.move(this->velocity); //przekazujemy wektor predkosci
if (this->left() < 0) //sprawdzanie krawedzi i zmiana wektora
{
velocity.x = ballVelocity;
}
else if (this->right() > 800)
{
velocity.x = -ballVelocity;
}
if (this->top() < 0)
{
velocity.y = ballVelocity;
}
else if (this->bottom() > 600)
{
velocity.y = -ballVelocity;
}
}
float Ball::left() //zwracamy krawedz bierzemy pozycje kulki i wyznaczamy
{
return this->shape.getPosition().x - shape.getRadius();
}
float Ball::right()
{
return this->shape.getPosition().x + shape.getRadius();
}
float Ball::top()
{
return this->shape.getPosition().y - shape.getRadius();
}
float Ball::bottom()
{
return this->shape.getPosition().y + shape.getRadius();
}
#pragma once
#include <SFML/Graphics.hpp>
using namespace sf;
class Ball : public sf::Drawable
{
public:
Ball(float t_X, float t_Y);
Ball() = delete ;
~Ball() = default;
void update();
float left(); //krawedzie ksztaltu
float right();
float top();
float bottom();
private:
CircleShape shape;
const float ballRadius{ 10.0f };
const float ballVelocity{ 10.0f };
Vector2f velocity{ ballVelocity, ballVelocity }; //wektor predkosci w obydwu plaszczyznach
void draw(RenderTarget& target, RenderStates state) const override;
};
// Hello_world.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "pch.h"
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include "Ball.h"
using namespace sf;
using namespace std;
int main()
{
Ball ball(300, 300);
RenderWindow window{ VideoMode{800,600}, "Hello!" };
window.setFramerateLimit(60);
Event event;
while (true)
{
window.clear(Color::Red);
window.pollEvent(event);
if (event.type == Event::Closed)
{
window.close();
break;
}
ball.update();
window.draw(ball);
window.display();
}
return 0;
}
Witam, zacząłem zabawę w SFML'u i mam problem. Nie wiem jak utworzyć tablicę w klasie. Wydaje mi się to bardzo łatwe ale po wielu próbach poddałem się.