OK so i'm writing this program for a game we're making in school. I'll send the codes and I would like someone to let me know how i can fix these errors.
Code:
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
constexpr int windowWidth{800}, windowHeight{600};
constexpr float ballRadius{10.f}, ballVelocity{8.f};
//constants for the bat;
constexpr float batWidth{60.f},batHeight{10.f},batVelocity{6.f};
struct Bat{
//CircleShape is an SFML class that defines a renderable circle
RectangleShape shape;
//2D vector that stores the Bat's velocity
Vector2f velocity;
//creating the Bat constructor
//argument mX -> starting x coordinate
//argument mY -> starting y coordinate
Bat(float mX,float mY);
{
shape.setPosition(mX,mY);
shape.setSize(batWidth,batHeight);
shape.setFillColor(Color::Red);
shape.setOrigin(batWidth/2,batHeight/2);
};
// Move update
void update(){
shape.move(velocity);
if(Keyboard::isKeyPressed(Keyboard::Key::Left) && left() > 0)
velocity.x = -batVelocity;
else if(Keyboard::isKeyPressed(Keyboard::Key::Right))
velocity.x = batVelocity;
else
velocity.x = 0;
}
//create property methods to easily get commonly used values
float x() { return shape.getPosition().x; }
float y() { return shape.getPosition().y; }
float left() { return x() - shape.getSize().x/2.f; }
float right() { return x() + shape.getSize().x/2.f; }
float top() { return y() - shape.getSize().y/2.f; }
float bottom() { return y() + shape.getSize().y/2.f; }
};
//creating the template
template<class T1, class T2> bool isIntersecting(T1& mA, T2& mB);
{
mA.right => mB.left()&& mA.left() <= mB.right() && mA.bottom()> =mB.top() <= mB.bottom();
return;
}
//creating the collision functions
void testCollision(Bat mBat, Ball mBall);
{
if(!isIntersecting(mBat, mBall)) return;
mBall.Velocity.y = -ballVelocity;
if(mBall.x()<mBat.x()) mBall.velocity.x = -ballVelocity;
else mBall.velocity.x = ballVelocity;
};
//creating a circle class/struct for the Ball
struct Ball
{
//CircleShape is an SFML class that defines a renderable circle
CircleShape shape;
//2D vector that stores the Ball's velocity
Vector2f velocity{-ballVelocity, -ballVelocity};
//creating the Ball constructor
//argument mx -> starting x coordinate
//argument my -> starting y coordinate
Ball(float mX, float mY)
{
//apply position, radius, colour and origin to the CircleShape shape
shape.setPosition(mX,mY);
shape.setRadius(ballRadius);
shape.setFillColor(Color::Red);
shape.setOrigin(ballRadius, ballRadius);
}
//Update the ball: by moving is shape by the current velocity
void update()
{
shape.move(velocity);
//we need to keep the ball inside the screen
//if it is leaving towards the left, we need to set horizontal velocity to a positive
//value
if(left() < 0) velocity.x = ballVelocity;
//otherwise, if it is leaving towards the right, we need to right we need to set
// velocity a negative value
else if(right() > windowWidth) velocity.x = -ballVelocity;
//apply the same idea to the top and bottom collisions
if(top() < 0) velocity.y = ballVelocity;
else if(bottom() > windowHeight) velocity.y = -ballVelocity;
}
//create property methods to easily get commonly used values
float x() {return shape.getPosition().x; }
float y() {return shape.getPosition().y; }
float left() {return x() - shape.getRadius(); }
float right() {return x() + shape.getRadius(); }
float top() {return y() - shape.getRadius(); }
float bottom() {return y() + shape.getRadius(); }
};
int main()
{
//create an instance of Ball positioning it at the centre of the window
Ball ball(windowWidth / 2, windowHeight / 2);
Bat bat(windowWidth / 2, windowHeight - 5);
//creation of the game window
RenderWindow window{{windowWidth, windowHeight}, "Space Pool- 1"};
window.setFramerateLimit(60);
//Game loop
while(true)
{
//clear the window from previously drawn graphics
window.clear(Color::Blue);
//if "Escape" is pressed, break out of the loop
if(Keyboard::isKeyPressed(Keyboard::Key::Escape)) break;
//every loop iteration, we need to update the ball
ball.update();
bat.update();
//render the Ball instance on the window
window.draw(ball.shape);
window.draw(bat.shape);
//Show the window contents
window.display();
}
return 0;
}
Comment