46
General / better tutorial for SFML?
« on: August 29, 2011, 12:00:25 am »
one example is this code. i tried putting a simple png image to turn with WASD movements, and i wanted it to turn the direction that the user chose (but only once). my original image is suppose to be a top-down view of a guy(thus the reason for turning) but the ship game me a point to see where its heading is easier.
1)line 37, i tried putting an if() to only allow it to do it only once based on its direction
EDIT 1: i posted this code thinking this forum had number lines for codes line 37 is if(!(ship.Rotate(270.f)))...i already know that this is invalid
1)line 37, i tried putting an if() to only allow it to do it only once based on its direction
Code: [Select]
#include <SFML/Graphics.hpp>
int main()
{
// Create the main rendering window
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Pac-Man");
// Load the sprite image from a file
sf::Image Image;
if (!Image.LoadFromFile("ship.png"))
return EXIT_FAILURE;
// Create the sprite
sf::Sprite ship(Image);
// Change its properties
ship.SetColor(sf::Color(0, 255, 255, 128));
ship.SetPosition(200.f, 100.f);
ship.SetScale(0.f, 0.f);
ship.Rotate(0.f);
// Start game loop
while (window.IsOpened())
{
// Process events
sf::Event Event;
while (window.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
window.Close();
if(Event.Type == sf::Event::KeyPressed)
{
if(!(ship.Rotate(270.f)))
{
switch(Event.Key.Code)
{
case sf::Key::A:
ship.Rotate(270.f);
break;
}
}
}
if(Event.Type == sf::Event::KeyPressed)
{
switch(Event.Key.Code)
{
case sf::Key::W:
ship.Rotate(0.f);
break;
}
}
if(Event.Type == sf::Event::KeyPressed)
{
switch(Event.Key.Code)
{
case sf::Key::D:
ship.Rotate(90.f);
break;
}
}
if(Event.Type == sf::Event::KeyPressed)
{
switch(Event.Key.Code)
{
case sf::Key::S:
ship.Rotate(180.f);
break;
}
}
}
// Get elapsed time
float ElapsedTime = window.GetFrameTime();
// Move the sprite
if (window.GetInput().IsKeyDown(sf::Key::A))
{
ship.Move(-200 * ElapsedTime, 0);
}
if (window.GetInput().IsKeyDown(sf::Key::D)) ship.Move( 200 * ElapsedTime, 0);
if (window.GetInput().IsKeyDown(sf::Key::W)) ship.Move(0, -200 * ElapsedTime);
if (window.GetInput().IsKeyDown(sf::Key::S)) ship.Move(0, 200 * ElapsedTime);
// Rotate the sprite
if (window.GetInput().IsKeyDown(sf::Key::Add)) ship.Rotate(- 200 * ElapsedTime);
if (window.GetInput().IsKeyDown(sf::Key::Subtract)) ship.Rotate(+ 200 * ElapsedTime);
// Clear screen
window.Clear();
// Display sprite in our window
window.Draw(ship);
// Display window contents on screen
window.Display();
}
return EXIT_SUCCESS;
}
EDIT 1: i posted this code thinking this forum had number lines for codes line 37 is if(!(ship.Rotate(270.f)))...i already know that this is invalid