hello i am making an pong game but when i can't get the position of the ball. i have tried to use ball.GetPosition() but that didn't work. so could someone tell me how to do that.
this is my code:
sf::Shape ball=sf::Shape::Circle(395, 330, 10, sf::Color(200,200,200));
ball.Move(-49.1934955f * ElapsedTime, -98.386991f * ElapsedTime);
std::cout<<ball.GetPosition()<<std::endl;
full code under this if someone wants to test.
#include "stdafx.h"
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
// Create main window
sf::RenderWindow App(sf::VideoMode(800, 600), "SFML Shapes"/*, sf::Style::Fullscreen*/);
//define shapes
sf::Shape panel1=sf::Shape::Rectangle(20, 300, 30, 360, sf::Color::White);
sf::Shape panel2=sf::Shape::Rectangle(770, 300, 780, 360, sf::Color::White);
sf::Shape ball=sf::Shape::Circle(395, 330, 10, sf::Color(200,200,200));
bool game=false;
int direction=0;
// Start game loop
while (App.IsOpened())
{
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
if (Event.Type == sf::Event::KeyPressed && Event.Key.Code==sf::Key::Space)
game=true;
}
// Clear screen
App.Clear();
// Draw shapes
App.Draw(panel1);
App.Draw(panel2);
App.Draw(ball);
//elapsed time
float ElapsedTime = App.GetFrameTime();
//move panels
if (App.GetInput().IsKeyDown(sf::Key::Up)) panel2.Move(0, -110 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::Down)) panel2.Move(0, 110 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::U)) panel1.Move(0, -110 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::J)) panel1.Move(0, 110 * ElapsedTime);
if (game) {
if (direction==0) {
ball.Move(-49.1934955f * ElapsedTime, -98.386991f * ElapsedTime);
//sqrt(110²/5) <-- *2
std::cout<<ball.GetPosition()<<std::endl;
}
}
// Finally, display the rendered frame on screen
App.Display();
}
return EXIT_SUCCESS;
}