Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: What is wrong with this code?  (Read 1848 times)

0 Members and 2 Guests are viewing this topic.

mark973

  • Newbie
  • *
  • Posts: 3
    • View Profile
What is wrong with this code?
« on: January 15, 2013, 04:34:52 am »
Hi, Im new here on sfml forums and right now im trying to make a simple pong game in sfml 1.6.
When I get to making the shape of the second paddle, when I compile, it doesn't show up right. Like I put the exact parameters but it shows up in a different place.
Here's the code I have so far:

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow Window (sf::VideoMode(640, 480, 32), "Pong");
    sf::Shape paddle1 = sf::Shape::Rectangle(0, 100, 10, 10, sf::Color(255, 0, 0));
    sf::Shape paddle2 = sf::Shape::Rectangle(0, 0, 10, 10, sf::Color(255,0,0)); // I put the parameters 0, 0 in here but the rectangle shows up in a different spot on the screen
    sf::Shape ball = sf::Shape::Circle(320, 240, 10, sf::Color(255,0,0));

    while(Window.IsOpened())
    {
        sf::Event Event;
        while(Window.GetEvent(Event))
        {
            if(Event.Type == sf::Event::Closed)
                Window.Close();
        }

        if(Window.GetInput().IsKeyDown(sf::Key::Down))
            paddle1.Move(0.0, 2.0);
        if(Window.GetInput().IsKeyDown(sf::Key::Up))
            paddle1.Move(0.0, -2.0);

        Window.Clear(sf::Color(255,255,255));
        Window.Draw(paddle1);
        Window.Draw(paddle2);
        Window.Draw(ball);
        Window.Display();
        sf::Sleep(0.06);
    }
}
« Last Edit: January 15, 2013, 04:49:24 am by mark973 »

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: What is wrong with this code?
« Reply #1 on: January 15, 2013, 04:50:59 am »
1.6 Shape::Rectangle Documentation

Note what the third and fourth parameter are.


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
What is wrong with this code?
« Reply #2 on: January 15, 2013, 05:24:14 am »
Also keep in mind that SFML 1.6 is 2.5 years old, has many bugs and lacks quite a few features. So in mostly all cases it's better to use SFML 2. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

mark973

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: What is wrong with this code?
« Reply #3 on: January 15, 2013, 09:05:48 pm »
I still don't know what I'm doing wrong. Is my code wrong or something. Am I just not putting in the parameters in right?

mark973

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: What is wrong with this code?
« Reply #4 on: January 15, 2013, 09:14:49 pm »
Wait never mind, I think I figured it out. I was doing it the way I would have in SDL. I didn't realize that SFML has a different system of making rectangles lol.