Hey, I'm currently in the progress of creating my first game using SFML.
My game is going to be like Pickin' Sticks, but have the movement of something like Pokemon / Snake.
Basically, the character will have to collect an object, like in Pickin' Sticks, and will move by when I press a key, he sets off in that direction (No holding down the button to get him to move and then releasing to stop). I also want him to be walking on the tiles on the floor only, not in between.
Here's what the game looks like so far (Ugly but what I need to visualise it at the moment):
Here is my character movement code:
float ElapsedTime = Clock.GetElapsedTime();
Clock.Reset();
float charSpeed = 50.f;
if ((sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::W))
Character.Move(0, -charSpeed * ElapsedTime);
if ((sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::A))
Character.Move(-charSpeed * ElapsedTime, 0);
if ((sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::S))
Character.Move(0, charSpeed * ElapsedTime);
if ((sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::D))
Character.Move(charSpeed * ElapsedTime, 0);
My problem at the moment is that the character sprite moves fine, until I tap a key, for example 'g', and then the sprite stops. How do I prevent this?
My next problem is, how do I keep the sprite between the tiles, so there is no overlapping on the black lines? I've thought about this for a while but haven't thought of a way to implement it.
Lastly, my code for creating all those tiles etc is quite messy:
const float imgWidth = Tile1.GetWidth(); // Tile1 is just the image version.
const float imgHeight = Tile1.GetHeight();
const float horizontalTiles = App.GetWidth() / imgWidth;
const float verticalTiles = App.GetHeight() / imgHeight;
std::vector<sf::Sprite> Tiles;
for (int i = 0; i < horizontalTiles; i++)
{
for (int j = 0; j < verticalTiles; j++)
{
sf::Sprite Tile;
Tile.SetImage(Tile1);
Tile.SetPosition(i * imgWidth, j * imgHeight);
Tiles.push_back(Tile);
}
}
for (unsigned int i = 0; i < Tiles.size(); i++)
App.Draw(Tiles[i]);
Is there a "cleaner" way of doing this? What if I also wanted to change a few of the middle tiles colour using .SetColor? I imagine to do this I would use for loops again, but I can't really think of how to do it without changing nearly all of them.
Thanks.