How to implement smooth tile based movement [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
The player in my game is centered in the screen, and I 'scroll' the background to move around.
I get a list of keys pressed with pygame.key.get_pressed(), if the player's move cooldown is over, I call a move function. This function scrolls the map in the direction of movement for one tile.
However the movement is not smooth. It 'jumps' to the next tile.
How do I implement a movement system so that the player moves in small steps (speed * dt) but still only moves the distance of one tile every time the move function is called? The game needs to be rendered in between each small step I think.
Full source here: https://github.com/HCollings/rpg-game/blob/alternate_movement/game.py

Animations and movement are all about what you perceive rather than what's actually happening. In a grid system, you can abstract the visual movement of the player away from the actual mechanics by taking the drawing part of the code and updating the draw position independently of the gameplay position.
The problem you are experiencing with jumping is commonly handled with some kind of interpolation technique. lerp() is a common function that provides linear interpolation, but in my below example I will use a different technique.
Here is a resource about interpolation techniques: http://paulbourke.net/miscellaneous/interpolation/
You could implement your movement as destination, direction, and "unspent" increments of movement.
If you wanted to move to a new square, you would set your direction and add a standard number of increments (representing one square) to your "unspent increment counter". Then you draw your graphic at the new location offset by the number of increments you still have. If you want to move more than one square at a time, then you would increase the number of unspent increments.
Movement would only take place so long as you have unspent increments (think movement points). This technique also lends itself to visual collision detection; if the remaining number of increments is some fraction of the total increments (~25%?) between the number of squares you are moving, then the graphic is "practically" in the square according to what the player sees.

Related

Creating a vector in order to measure distance from a sprite to another vector

I'm currently just starting to create a game with Pyglet featuring a race car, which will drive around a track, of which I intend to later add Tensorflow.
Is there any way which I can draw a vector from a certain position on the car (i.e the back, front or right hand side)? I would then ideally be able to measure the distance from the side of the track in that direction through a vector I will set on the side, not sure if this is possible or not.

Make a Yin-Yang circle [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I've been trying to attempt a Yin-Yang circle on Python, but so far I've only been able to do a black semi-circle. In here I've attached what it looks like for me and what it's supposed to look like. What else should I add to my code?
*to make it easier for me to understand, use fill function when coloring.
My Code:
import turtle
turtle.begin_fill()
turtle.circle(50,180)
turtle.end_fill()
turtle.hideturtle()
What it looks like for me:
results
How it's supposed to be:
desired results
You are pretty close already, if you think of the yin-yang symbol mathematically, it's just a bunch of arcs, two large ones on the outside, and two on the inside, half the length.
If the outside arcs have a length of 50, then the internal arcs need to be 25. Turtles move counter-clockwise by default, so to get a clockwise curve, use a negative radius. Like so:
turtle.begin_fill()
turtle.circle(50, 180)
turtle.circle(25, 180)
turtle.circle(-25, 180)
turtle.end_fill()
turtle.circle(-50, 180)
After that, the turtle will back where it started, and the positions for the circles should be roughly a quarter and three quarters of 50 directly below the turtle, where you just need to make two full circles.
As much as I like #CameronFerguson's single curve approach (+1), it might be easier for a beginner to think of this as simply five circles. The first, largest circle is filled on one side, as you have already achieved. The next two circles are half the radius of the original, centered on the vertical axis, and opposite colored:
Choose a size for the smallest two circles, above they are 1/8th of the original radius, use the same center point as the previous circles and reverse the color again. Now, switching to the final colors, and raising the pen appropriately to avoid unwanted lines, we get:
With some additional thought, the two sets of inner circles can done in a loop, cutting the number of calls to turtle.circle() in half. But if you're comfortable with #CameronFerguson's curve approach, go with that!

Pygame Top-Down Scrolling [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
For a project I want to do for my class' Pygame final, I want to make The Legend of Zelda: A Link To The Past. However right when I started I realized that the scrolling would be quite an issue.
How do you implement a scrolling technique that follows the player until the edge of a map or image but still allows the player to move closer to the edge?
A reference because I feel as if I am not correctly wording myself:
https://www.youtube.com/watch?v=kWyT9d8CiKY
My personal idea was to use a switch that switches the background image moving to Link's image moving.
A major component of any branch of engineering is breaking down big problems into smaller ones. So let's break down your problem.
How would you design a scrolling technique that follows the player
until the edge of a map or image but still allows the player to move
closer to the edge?
Okay, so there are three problems here. Moving the player sprite, moving the background sprite, and working out when to do each. Moving the player sprite is pretty straight forward - give it an (x,y) coordinate on the screen and move that according to the input controls (keyboard/mouse/etc).
Now let's consider the background sprite. For simplicity we'll assume that your whole background can be loaded as one big sprite. We want to render a portion of that background onto the screen - so we need to maintain the position of the background relative to the screen with it's own coordinates.
You can think about this two ways - either the screen stays stationary and the background moves behind it, or the background stays and the screen moves. Given that you'll eventually be tracking lots of other items (baddies, treasure, etc) and their position on the map, I would suggest thinking about everything moving relative to the background (even though this may seem less intuitive at first). Let's call this the world coordinate. To render things to the screen we'll need to work out their screen coordinate.
Okay, so we now have two coordinates - the positions of the screen and the player. For consistency, let's make the player position use world coordinates too.
So how do we render this to the screen? Start by listing out the rules:
the background should always fill the screen (i.e. don't scroll so far
that you can see outside of the background sprite)
the player should be centred on screen, except when that would violate #1
So the position of the screen is dependent on the player, but with some limits depending on where it is on the map. Let's consider the x coordinate (note this is untested):
# start by centring the screen on the player
screen_x = player_x - screen_width/2
# limit the screen to within the bounds of the background
if screen_x < 0:
screen_x = 0
if screen_x > (background_width - screen_width):
screen_x = (background_width - screen_width)
You can now calculate the render position of the player (position on screen) by subtracting screen_x from player_x. The background render position is calculated the same way (but should result in a negative coordinate).

"Unblock me" puzzle representation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
i am trying to implement a program that solves the "Unblock me" puzzle. For those who dont know it, "Unblock me" is a sliding block puzzle where you have a board with 1x1, 1x2, 2x1 and 2x2 boxes and the goal is to move the red box out of the board through an opening.
My question is how can i represent a board state? I searched throught the internet and found a suggestion to represent the state like this.
For example this board: Board State
Representation: Grid representation
I can understand how this representation works. The problem comes when i want to move one of the boxes. How can i check if a box is 1x1, 1x2, 2x1 or 2x2 in order to check if the wanted move is possible or not??
Any ideas on how i could do that, or any other "easier" representations?
I would do:
Width of grid
Height of grid
List of boxes, and for each box:
Dimensions of the box (doesn't have to be a square or rectangle)
Width of the box
Height of the box
Each of the cells that are occupied within the bounds of the box
Position of top-left cell, so coordinates (x, y)
Name of the box (e.g., "A", "B", "C")
If this is an unblock puzzle where you need to slide a box outside the grid, you'll also need:
Coordinates of the grid cell that has the exit
The position of the exit for that cell (e.g., "north", "east", etc). This is necessary when the exit is located in a corner cell.
This should allow you to perform any computations as necessary. Given the top-left cell of where a box is located, the dimensions of the box and knowing what cells are occupied within the box, you can compute whether a collision is taking place or not.
The above representation also supports arbitrary shapes, not just rectangles and squares.
You could always have an array of the same dimensions as the "grid", the exit would have a special value of -1, empty squares have 0 and each block would have the number of the block.
You could then have a Check function that takes a block number and a direction and for each current cell that has the block number as the value check that the adjacent cell either has the same block number or is <1.
The move function would shift the values in the required direction setting zero, with a little care on the order things are done you can just start with the first set the cell in the required direction to the required number and the current square to 0. Minimal ifs and a couple of loops.

Pygame Tile Based Movement Speed

Thanks for taking the time to read this.
Right now I'm making a really basic tile based game. The map is a large amount of 16x16 tiles, and the character image is 16x16 as well. My character has its own class that is an extension of the sprite class, and the x and y position is saved in terms of the tile position.
To note I am fairly inexperienced with pygame.
My question is, I am planning to have character movement restricted to one tile at a time, and I'm not sure how to make it so that, even if the player hits the directional key dozens of time quickly, (WASD or arrow keys) it will only move from tile to tile at a certain speed. How could I implement this generally with pygame? (Similar to game movement of like Pokemon or NexusTk). One movement would result in a player being in a tile. They couldn't stop halfway between tiles for example.
Thanks for your time! Ryan
You store your characters location as a grid coordinate. So if he's at (2,0) he is rendered at (32,0). The game then animates him moving between tiles, but, he's either on one or the other. While in the move state, you render an (x,y) offset between 0 to tilewidth.
It sounds like you want one move per keypress, if time elapsed / animation has completed. So:
On keypress, toggle to: animating state
Set destination tile coordinate
draw offset, between 0 and tilewidth, depending on time elapsed. offset = (elapsed_ms / 1000.) * tile_w would scale between 0 to 16 if time is less<= 1 second.
Once time elapsed is >= animation length (I chose 1000. above), switch to stationary state.
If keypress happens while in animation state, ignore it.
Pygame example: using numpy for map array.

Categories

Resources