Trying to figure out where the error is

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Knobs
    New Member
    • May 2014
    • 4

    Trying to figure out where the error is

    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;
    }
    Last edited by Rabbit; May 4 '14, 05:17 AM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    What errors are you speaking of? Debugging compiler and run-time errors is a skill each person needs to develop. Therefore, I usually don't just jump in and fix your code but I will help you if you steer me a little.

    Comment

    • Knobs
      New Member
      • May 2014
      • 4

      #3
      I'm using code blocks 13.12
      When i try compile it i'm getting this error: expected unqualified-id before '{' token. on lines 22,50 and 59

      Comment

      • techboy
        New Member
        • Apr 2014
        • 28

        #4
        On line 21 their is no need of semicolon same goes with line 50 and 59.

        Comment

        • Knobs
          New Member
          • May 2014
          • 4

          #5
          OK. You're saying that but when I try to compile it, i'm getting more errors now when I remove the semicolon

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Write this program:

            Code:
            int main()
            {
            
            }
            Compile it and verify there are no errors.

            Then change it to:
            Code:
            #include <SFML/Window.hpp>
            #include <SFML/Graphics.hpp>
             
             using namespace std;
             using namespace sf;
            
            int main()
            {
            
            }
            Now compile it again and verify there are no errors.

            Add your struct declarations. Recompile and verify there are no errors.

            Repeat this over and over each time adding code and each time compiling an verifying there are no errors.

            Writing 10,000 lines of code and then trying to compile won't work. Been there. Done that.

            Comment

            • Knobs
              New Member
              • May 2014
              • 4

              #7
              OK Thanks I'll try that.

              Comment

              Working...