why does my animation jiggle slightly in pygame? - python

So I am in the process of coding a simple pong game but right now the ball sometimes has a small weird jiggle. It doesn't mess up gameplay but the jiggle is certainly visible. I monitored the speed of the ball and it seems to have a constant integer speed. So why does the ball jitter slightly sometimes even though the speed of the ball remains the same?

A code sample would be ideal but some of the problems you may be facing include:
Pixel jitter because the ball needs to move at a speed that does not sync up with the frame rate of the game (so in 1 frame the ball move 3 pixels but in another it moves 2).
Integer rounding. The position of the ball may be rounding to unexpected locations, so go over your code and check calculations of the balls movement.
It may be helpful to look at this: https://stackoverflow.com/questions/14538991/smoother-motion-using-pygame.
However these are just speculation because I'm missing the source code.
Hope this helps :)

Related

How to make something rotate based on where another object is

I am making a scrolling background type of game, kind of like Mario. I have a character that walks on the ground and can go left or right, and I want to introduce a flying eyeball that is suspended in the sky that follows the character's movements. I have an eyeball png (the eye of chuthulhu from Terraria). All I want it to do is rotate based on where the character is, and seem to stare at it (the next step is to have it shoot lasers at the character). How would I do this? Thanks in advance.
You could use the PyGame function pygame.transform.rotate() or pygame.transform.rotozoom() to pre-create rotated versions of your eyeball.
It sounds like the eyeball will pass over the top of the player when the player changes walking directions (since it's always following). So simply comparing the difference in sideways-position between the player and the eyeball should be enough to determine which of the pre-rotated eyeball images to choose.
If the eyeball's X co-ordinate is a long way from the player, then a slight angle is needed. As the player gets closer to having the eyeball above them, the angle should change to the point where the eye is looking straight down. This corresponds to the difference between the eyeball-X and player-X being small (tending towards zero).
Maybe even a simple mapping table between X-difference and sprite-image would be enough.

Pygame Draw Circle Causing Lag

I am creating a infinity scrolling 2d space battle game in pygame. I have just implemented a parallax star background, but my framerate is now 30fps. Do you how to get more performance, i've tried creating blitting instead of draw.circle, and hwsurface, but cant increase fps.
Download zip
I would suggest that you do not create the star background on the fly the way it sounds like you are doing. Instead create a surface that contains it at the start and then scroll through that. Wrap it around on itself so that as you go past the edge it continues from the beginning. You will want to make sure that the two edges blend and appear seamless.
Then you can just blit the part of the background onto the screen and you will not find it has much impact on your performance vs generating it on the fly all the time.
In fact if you are doing it ahead of time you can do something fancier than just circles. You can do stars with different sizes and brightness (color). Maybe even a glare effect.
Edited after comment from OP:
I thought from what you described you were scrolling it not rotating. If you are doing rotations, I assume that you are using pygame.transform.rotate() for the rotations. One thing I have done in the past if I need rotated images is again to rotate them before hand and save them. That can work well if you are just rotating (even saving 360 rotated images is not that bad memory wise), but if you are scrolling and rotating then that would not work

How to guarantee Kivy root widget properties will be referenced only after initialization?

I recently followed the Kivy pong game tutorial and decided to go a little further and have the game ball start at the same speed each round, but slowly increase in speed as the round progresses. I added code (to PongGame.p1_start() and PongGame.p2_start()) that set the initial speed of the ball each round based on the width of the root widget.
The problem is that the starting speed of the ball for the first round is slower than the initial speed of the ball for all subsequent rounds. For some reason, the speed of the ball the first round is set before the root widget is fully initialized, so the default widget width of 100 is used instead of the widget's true width once it's been initialized.
Here's the function where the ball speed is set:
def p1_start(self, *args):
self.ball.direction_x = -1.0
self.ball.direction_y = 0.0
self.ball.speed = 0.05*self.width
I could work out a crude workaround, but it wouldn't be pretty. Is there a clean way to get around this issue?

How can I change the speed that my turtle rotates?

I'm using turtle to draw a massive project but the turning takes years to do.
I have tried the command turtle.speed("fastest") but that only changes distance speed not rotational speed.
By looking at the documentation for turtle, it looks like turtle.speed() controls both speed and rotation:
Speeds from 1 to 10 enforce increasingly faster animation of line drawing and turtle turning.
Additionally,
speed = 0 means that no animation takes place. forward/back makes turtle jump and likewise left/right make the turtle turn instantly.
Note that turtle.speed("fastest") sets speed = 0. Maybe try changing speed to 10 with just turtle.speed(10) or turtle.speed("fast").
Your current solution sounds like it should work, if you add some example code we might be able to better assist you in finding a solution!
you can use turtle.speed(speed=None)
where speed is an integer in the range 0.. 10 or a speedstring.
if the input is a number greater than 10 or smaller than 0.5, speed is set to 0.Speedstrings are mapped to speed values as follows:
"fastest":0
"fast":10
"normal":6
"slow":3
"slowest":1
speed from 1 to 10 enforce increasingly faster animation of line drawing and turtle turning.speed=0 means that no animation takes place.forward/back makes turtle jump and likewise left/right make the turtle turn instantly.
e.g:
turtle.speed()
turtle.speed(9)
turtle.speed('normal')

Best OpenCV algorithm for detecting fast moving ball?

I am new to OpenCV. I am working on a project that involves tracking and detecting a spinning roulette ball. Here is the video I want to use: https://www.youtube.com/watch?v=IzZNaVQ3FnA&list=LL_a67IPXKsmu48W4swCQpMQ&index=7&t=0s
I want to get the ball time for 1 revolution. But the ball is quite fast and hard to detect. I am not sure how to overcome this.
What would be the best algorithm for doing this?
By subtracting successive images, you will isolate the ball as a (slightly curved) line segment. Both its length and its angular position are cues for the speed.
Anyway, these parameters are a little tricky to extract for a side view, as the ellipse has to be "unprojected" to a top view, to see the original circle. You need to know the relative position of the wheel and the viewer, which you most probably don't know.
An approximate solution is obtained by stretching the ellipse in the direction of the small axis.

Categories

Resources