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

Author Topic: Key Pressed  (Read 2793 times)

0 Members and 2 Guests are viewing this topic.

wvtrammell

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Key Pressed
« on: October 28, 2010, 07:10:40 pm »
Hi
Can some one tell me why pressing the up key and moving the cursor has the same effect in the following code:



Code: [Select]
#include <SFML/Graphics.hpp>


int main()
{
    srand((unsigned int)time(NULL));

int x, y;

// Create main window
    sf::RenderWindow App(sf::VideoMode(1200, 800), "SFML Graphics");

// Change color of background
App.Clear(sf::Color(128,128,128));
App.Display();



// 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::Up));
   {
       x = rand() % 1000 + 50;
       y = rand() % 600 + 50;

        // Draw a shape
       App.Draw(sf::Shape::Circle(x, y, 50, sf::Color::Red));

        // Finally, display the on screen
       App.Display();

    }

}

    }

    return EXIT_SUCCESS;
}


Thanks
Warren

wvtrammell

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Key Pressed
« Reply #1 on: October 28, 2010, 10:14:56 pm »
Let me ask this question:
How does circles get drawn and displayed if the only signals received are from the mouse button:
Is this an error in SFML Lib or resulting from clobbered software on my machine?
Thanks
Warren

Lupinius

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
Key Pressed
« Reply #2 on: October 28, 2010, 11:03:55 pm »
There is a semicolon after your if statement. So nothing happens if up is pressed and the drawing happens always.

Thiziri

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Key Pressed
« Reply #3 on: October 28, 2010, 11:27:57 pm »
I took a little time to rewrite your code (very confuse), and saw the extra semicolon too. I really suggest you to read some tutorials on C + + and those of the SFML to improve your current programming level.

Code: [Select]
#include <SFML/Graphics.hpp>
#include <list>

// Set the program's constants
const float Width = 1200;
const float Height = 800;
const sf::Color BackgroundColor(120, 120, 120);

// Create our shape list
std::list<sf::Shape> myCircles;

// A function to generate a random circle
void RandomizeCircle(std::list<sf::Shape>& shapeList, float width, float height)
{
// Get some random values to our future circle
float s = sf::Randomizer::Random(10,       50); // size
float x = sf::Randomizer::Random( s,  width-s); // pos x
float y = sf::Randomizer::Random( s, height-s); // pos y
float r = sf::Randomizer::Random( 0,      255); // color R
float v = sf::Randomizer::Random( 0,      255); // color V
float b = sf::Randomizer::Random( 0,      255); // color B

// Create and add the new circle to the list
shapeList.push_back(sf::Shape::Circle(x, y, s, sf::Color(r, v, b)));
}

int main()
{

    // Create main window
    sf::RenderWindow App(sf::VideoMode(Width, Height), "SFML Graphics");

    // Save the planet, don't waste CPU resources for nothing
    App.SetFramerateLimit(60);

   // 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();

            // Push up key : generate a circle
            if(Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Up)
            RandomizeCircle(myCircles, Width, Height);
        }

        // Clear the screen
        App.Clear(BackgroundColor);

        // Draw the shape list
        for( std::list<sf::Shape>::const_iterator it = myCircles.begin(); it != myCircles.end(); ++it )
        App.Draw(*it);

        // Finally, display the on screen
        App.Display();

    }


    return EXIT_SUCCESS;
}

 

anything