C++/SFML Positioning Sprite Issue - c++

I have a class named 'Player' where I handle movements, level ups etc in a game I am attempting to create. In the main loop in the main source file I have the keyboard events (Left/Right.) I want the movements to be able to know when your character is venturing past where it's allowed. To answer the problem I placed two if statements.
I am only having issues with this if statement:
else if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Right)) {
if (Player.getX() > 1279) {
Player.move(640,0);
}
Player.move(0.1,0);
}
No compiling issues. The only issue is, unlike the other if statement, this one doesn't return the sprite to the wanted position. If I lower the if statement to something like 1000, the sprite disappears from the screen.
Any help appreciated.

Player.move(640,0);
Your moving your player 640 pixel on the X-axis. This means that when the player gets to x > 1279, i.e. at the right end of your world, you move the player further to the right. So it disappears.
You might want to use setPosition instead of move here, or simply don't move the player, etc...

Related

how can i draw a sprite by left clicking?

i've tried to make a project, but i can't draw a sprite as i want. I mean that everything works when i just draw a sprite, but it stop working when i am trying to draw the sprite by clicking left mouse button. There's code i tried:
if(zdarzenie.type == Event::MouseButtonPressed && zdarzenie.mouseButton.button == Mouse::Left)
{
pocisk.setPosition(10, 10);
oknoAplikacji.draw(pocisk);
}
Btw, I am writing in Polish as if it would change something.
And yes, i have everything good besides that.
(and i am using 2.4.1 version of SFML)
I don't know what you are doing now because you didn't provide enough of your code and I actually don't understand your if statement but, it can just be :
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
sprite.setPosition(sf::Mouse::getPosition());
renderTarget.draw(sprite);
}
By the way I strongly suggest that you do not use the draw function here but organize your code to have a draw method called in a render loop, because depending on where your code is in your program, the sprite could be drawn for only one frame and then erased since it's not updated.
From what I understand in your code in Polish, you have the right code to do what you want, but the problem comes from the fact that you draw the sprite only once.
The draw method is called every frame and it will erase everything on screen and then redraw them. Doing it only once, like in your code, will only draw it a single time then delete it the very next frame.
At that point multiple solution can be used. If its a GameObject clicking can activate the object to then draw it or a simple bool could be used has a switch in your draw to make it appear.

Pygame: Character Centered Movement System (Diablo II like click-to-move)

I am currently working on a new RPG game using Pygame (my aim here is really to learn how to use object oriented programming). I started a few days ago and developed a movement system where the player clicks a location and the character sprite goes to that location and stops when it get's there by checking if the sprite 'collides' with the mouse position.
I quickly found however that this greatly limited the world size (to the app window size).
I started having a look into making a movement system where the background would move with respect to the player, hence providing the illusion of movement.
I managed to achieve this by creating a variable keeping track of my background map position. The map is much bigger than the app window. And each time I want my player to move I offset the background by the speed of the player in the opposite direction.
My next problem now is that I can't get my character to stop moving... because the character sprite never actually reaches the last position clicked by the mouse, since it is the background that is moving, not the character sprite.
I was thinking of spending some time coding in a variable that would keep track of how many displacements it would take the character sprite to reach the mouse clicked position if it was to move. Since the background moves at the character sprite's speed it would take as many displacement of the background in the x and y directions to center the clicked position on the background to the character sprite at the center of the screen.
It would be something like that:
If MOUSEBUTTON clicked:
NM = set number of moves needed to reach the clicked position based on character sprite distance to click and character sprite speed.
If NM != 0:
Move background image
Else:
pass
This would mean that when my background has moved enough for the character sprite to now be just over the area of the background that was originally clicked by the player, the movement would stop since NM == 0.
I guess that my question is: Does that sound like a good idea or will it be a nightmare to handle the movement of other sprites and collisions ? And are there better tools in Pygame to achieve this movement system ?
I could also maybe use a clock and work out how many seconds the movements would take.
I guess that ultimately the whole challenge is dealing with a fixed reference point and make everything move around it, both with respect to this fixed reference, but also to their own. e.g. If two other sprites move toward one another, and the character of the player also "moves" then the movement of the other two sprites will have to depend both on the position of the other sprite and also on the offset of the background caused by the movement of the player's character.
An interesting topic which has been frying my brain for a few nights !
Thank you for your suggestions !
You actually asking for an opinion on game design. The way I look at it, nothing is impossible so go ahead and try your coding. Also it would be wise to look around at similar projects scattered around the net. You may be able to pick up a lot of tips without re inventing the wheel. Here is a good place to start.
scrolling mini map

C++ How to animate graphics (POO)

I'm working on a game project in c++ using programming oriented object and classes but I can't figure out a way to animate the following graphics.
What I need is while the player is holding left key or the right key, the graphics should be appearing to make the character like moving and when they stop holding the key it'll turn to idle graphic.
I can't paste the whole source code here, i have many classes, and functions.. All I need is a BASIC idea of how to implement it, an example or a function anything useful. I don't need libraries because i just have two sprites to animate so it's not necessary.
As an example be Sprites the class that creates the object and Koala the one that moves it and prints it in a certain position.
Sprites idleSprite, walkingSprite;
Koala koala;
These declarations are just for avoiding other explanations.
I would appreciate your help.
PD: Don't worry about the keyboard keys, or other classes all I need is how to animate a sprite.
Koala should have two states:
a direction state: enum Direction {Left,Right};
a movement state. enum Movement { Idle, Walk };
As you have only one picture for the walking status, moving graphically the picture around will give the impression of a floating body. I'd recomment that you'd really have at least two walking positions to show that the foots are moving:
a movement step counter
a constant for the maximum number of steps.
Then the states should be updated in your game loop according to keyboard status and elapsed time. The pseudo code would be something like:
if (!arrow_key_pressed()) {
status_movement = Idle;
lasttimer = gametimer(); // keep track of last event
}
else {
status_movement = Walk;
if (left_arrow_pressed() )
status_direction = Left;
else if (right_arrow_pressed() )
status_direction = Right;
if (gametimer() - lasttimer > 2 ms ) { // if enough time,
if (status_direction==Left)
position_x -= step_increment;
else if (status_direction==Right)
position_x += step_increment;
movement_step = (movement_step+1) % maxi_steps;
lasttimer = gametimer();
}
}
All you have then to do is to restore te background of the picture at tis old position, and draw the picture at the position. For this, youd could call a function with paramters position_x, direction, movement status and status step, to return/draw the sprite at the right place.

SDL drawing a sprite once a button is pressed

So, i'm a complete stranger to SDL and i found this nice code online:
http://gamedevgeek.com/tutorials/animating-sprites-with-sdl/
I was just wondering how to make it so that when i press space a shape gets placed infront of me? For instance, im just walking around and when i press space a rectangle or another bmp is places in front of me.
Sorry for not being explicit in what i want, i just dont know how to explain it.
You need:
Another Surface to draw (blit), made from the rectangle.bmp. This would use the same method as is used for the grass (or the player if you wanted animation).
Knowledge of where "in front" is: up, down, left or right. Look at the code and see what variables change when you press one of the arrow keys. (Hint: don't use rcSprite.) In a larger game, you would want to define a new variable for the direction that the player is facing, and then use that for both the sprite animation and for placing the rectangle, as it would make the code easier to understand.
Some new code in HandleEvent, which does something if the key is SDLK_SPACE.
Then calculate a position to place the rectangle (say, the player's position with 50 added to "x" if they were facing right), and draw the rectangle in the same way as the grass.
In general, look at what code has already been written, and Google for stuff you don't know (e.g. the name of SDLK_SPACE).
Good luck

Board Game using SDL

I am building a board game in SDL and here is the problem I am currently facing.
I have a pawn on square 1 and I roll the dice. Based on the value I get on the dice the pawn moves to another square. I am bale to move the pawn after i read the SDL tutorials online. But the problem I am facing is that after moving the pawn to a new location the old pawn still stays at the old location. The tutorials I found on the internet moves a dot but also refreshes the background to cover up the old dot. But I cant do that as my game board is intricate and there are pawns from other players sitting there.
Is there a way in SDL that I could really move a pawn instead of having to create a new pawn at the new location and covering up the old pawn?
The fundamental concept of sprites: Before you insert the sprite, you save a copy of the original screen content. When you need to remove the sprite, you just paste the stored old content back in.
You will have to process all your objects in the correct order (LIFO) for this to work. Since you'll usually be double-buffered, this happens on the cold buffer, so this isn't an issue.
No, your code will need to be able to redraw that board position with the pawn missing. There isn't any way for the computer to automatically reconstruct what the board should look like without the pawn.
It sounds like your render code is mixed in with your game logic. You ought to separate rendering so that you can redraw the complete game scene with a single function call, which you can then use whenever a visible change is made to the game state.