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

Author Topic: Help with game and sprite movement  (Read 4412 times)

0 Members and 1 Guest are viewing this topic.

Kiblinix

  • Newbie
  • *
  • Posts: 15
    • View Profile
Help with game and sprite movement
« on: May 17, 2011, 09:31:45 pm »
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:
Code: [Select]
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:
Code: [Select]
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);
}
}

Code: [Select]
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.  :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Help with game and sprite movement
« Reply #1 on: May 17, 2011, 10:28:12 pm »
Code: [Select]
if (sf::Event::KeyPressed)This does not check whether a key is currently pressed. KeyPressed is a integral constant (to be precise, an enumerator) and is converted to bool, where every value except 0 corresponds true.

The correct way to check key events looks as follows:
Code: [Select]
sf::Event event;
while (app.PollEvent(event)) // GetEvent() in SFML 1
{
    if (event.Type == sf::Event::Closed)
        return 0;

    else if (event.Type == sf::Event::KeyPressed)
    {
        switch (event.Key.Code)
        {
            case sf::Key::X:  DoSomething();       break;
            case sf::Key::Y:  DoSomethingElse();   break;
        }
    }
}
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Kiblinix

  • Newbie
  • *
  • Posts: 15
    • View Profile
Help with game and sprite movement
« Reply #2 on: May 18, 2011, 07:24:03 pm »
Thanks for your reply, it was very helpful :) I now have movement how I want it apart from the character not staying in the grid.

Does anybody have any tips on this?
I imagine it would be done using the sprite position and could I then compare it with each tile position? Or only be able to move the character when he is aligned with the grid?

Dimple

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Help with game and sprite movement
« Reply #3 on: May 18, 2011, 07:46:39 pm »
I usually use dynamic 2D-arrays for the tilemaps. If I want to store some extra information, I just add an extra dimension. Here's an example of a map and how it could look like in memory:
Code: [Select]

The map:

####
#00#
####

How it's stored:

1 1 1 1
1 0 0 1
1 1 1 1


I'm not sure how you want the character to move so I can't give you tip on that one. Do you want it to hop from one tile to another, or do you want it to move "freely" from the center of the tile to the center of the next tile?

Kiblinix

  • Newbie
  • *
  • Posts: 15
    • View Profile
Help with game and sprite movement
« Reply #4 on: May 18, 2011, 07:59:25 pm »
I'd like it to move freely from the center of the tile to the center of the next tile, and thanks for the info, I'll look into that.

hagel

  • Newbie
  • *
  • Posts: 23
    • View Profile
Help with game and sprite movement
« Reply #5 on: May 19, 2011, 03:37:28 pm »
What you want to do is move it until
 player.x % (TILE_SIZE + (GRID_SIZE/2)) == 0
I think. Well, I know you gotta use % to check if your dude went as far as it should've.

I don't know if it's just an image that looks like a grid, if so. It's just
 player.x % TILE_SIZE == 0

Dimple

  • Newbie
  • *
  • Posts: 30
    • View Profile
Help with game and sprite movement
« Reply #6 on: May 21, 2011, 11:16:45 am »
You could do like Hagel suggested but there is a problem: what if the character can move faster than one pixel per frame? It might go over the grid.

I actually made an example of that kind of movement for a programming language called Coolbasic just a while ago. What I did was that I had two variables for storing coordinates for the waypoint where the character is heading and when the player wants to move, the waypoint is set accordingly. For example, if the player wanted to move right, the waypoint's coordinates would be (x+TileWidth, y) and then the character would start moving towards the waypoint. When the distance to the waypoint (Pythagora's theorem) is below certain limit, the character's coordinates are set to be the waypoint's coordinates. I also had a variable that stored the direction where the player is headed so that the right sprite would be drawn.

 

anything