1
General / PacMan Movement Not Working Properly
« on: December 18, 2011, 07:00:38 am »
Hello everyone! I'm a little new with SFML, although I have some intermediate experience with C++ Console Applications and I have written a few simple text based games before. I've decided to start making simple 2D games, and SFML looked like a very nice choice I've gotten used to rendering windows and setting up a game loop ect. And now I'm working on my first full game!
It's basically a PacMan clone. In the end product I hope to have 4 ghosts methodically running around a maze. But right now, I'm having a little trouble getting the player to move. My code will render the player at the bottom of the screen, but he just sort of jitters in place a bit when I try to move him.
I'm sorry if I'm doing something stupid. I'm just kind of new to SFML and I'm a bit intimidated, to be honest. Any help is appreciated!
It's basically a PacMan clone. In the end product I hope to have 4 ghosts methodically running around a maze. But right now, I'm having a little trouble getting the player to move. My code will render the player at the bottom of the screen, but he just sort of jitters in place a bit when I try to move him.
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::Image image;
sf::Sprite sprite;
if(!image.LoadFromFile("pac.png"))
{
}
sprite.SetImage(image);
sprite.SetPosition(200,400);
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;
//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);
//Display the screen
win.Display();
}
//return value
return EXIT_SUCCESS;
}
I'm sorry if I'm doing something stupid. I'm just kind of new to SFML and I'm a bit intimidated, to be honest. Any help is appreciated!