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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Ballistic

Pages: [1]
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:

2
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.

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

Pages: [1]