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

Author Topic: PacMan Movement Not Working Properly  (Read 3274 times)

0 Members and 1 Guest are viewing this topic.

Ballistic

  • Newbie
  • *
  • Posts: 2
    • View Profile
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.

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!  :D

Anata

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
PacMan Movement Not Working Properly
« Reply #1 on: December 18, 2011, 01:31:43 pm »
Code: [Select]
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);
   
}


Dont put the creation of your sprite and the SetPosition in the while(win.IsOpened()) .
Put only the the movePlayer and win.Draw in the while(win.IsOpened()) .

position is always set to 200, 400, and the sprite dont move ^^


Excuse for my english ^^

Ballistic

  • Newbie
  • *
  • Posts: 2
    • View Profile
PacMan Movement Not Working Properly
« Reply #2 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:

justcolorado

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: PacMan Movement Not Working Properly
« Reply #3 on: January 02, 2012, 08:04:49 pm »
Quote from: "Ballistic"


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.
 :D


"4 ghosts methodically running around a maze"

I am working on a pac-man derived game myself.  While there is nothing wrong with having the ghosts methodically running around the maze.  One of the best features of the original Pac-Man was the AI of the 4 ghosts.  And it is not that difficult to replicate.  The article below explains the how the ghosts AI worked in full detail.

Here is the link:

http://home.comcast.net/~jpittman2/pacman/pacmandossier.html