1
General / PacMan Movement Not Working Properly
« on: December 18, 2011, 05:03:57 pm »Code: [Select]
//include files
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
//player movement function
void movePlayer(sf::RenderWindow &win, sf::Sprite &sprite)
{
float ET= win.GetFrameTime();
if(win.GetInput().IsKeyDown(sf::Key::W) || win.GetInput().IsKeyDown(sf::Key::Up))
{
sprite.Move(0,-100*ET);
}
else if(win.GetInput().IsKeyDown(sf::Key::S) || win.GetInput().IsKeyDown(sf::Key::Down))
{
sprite.Move(0,100*ET);
}
if(win.GetInput().IsKeyDown(sf::Key::D) || win.GetInput().IsKeyDown(sf::Key::Right))
{
sprite.Move(100*ET,0);
}
if(win.GetInput().IsKeyDown(sf::Key::A) || win.GetInput().IsKeyDown(sf::Key::Left))
{
sprite.Move(-100*ET,0);
}
}
//player image loader and displayer
void RenderPlayer(sf::RenderWindow &win, sf::Sprite &sprite)
{
movePlayer(win,sprite);
win.Draw(sprite);
}
//entry point, the great main function!
int main()
{
//render window
sf::RenderWindow win(sf::VideoMode(500,500,32),"Pac");
//player sprite
sf::Sprite sprite;
sf::Image image;
if(!image.LoadFromFile("pac.png"))
{
}
sprite.SetImage(image);
sprite.SetPosition(200,400);
//Event Var
sf::Event Event;
//game loop
while(win.IsOpened())
{
//event loop
while(win.GetEvent(Event))
{
// window closing procedures
if(Event.Type==sf::Event::Closed)
{
win.Close();
}
}
//end of event loop
//
//beginning of window clearing and drawing
win.Clear();
//Render player sprite
RenderPlayer(win,sprite);
//Display the screen
win.Display();
}
//return value
return EXIT_SUCCESS;
}
Works great now! Thanks mate :wink: