How to Continually Move an Image in Pygame
How to continuously move an image smoothly in Pygame?
Yes, I know that there are similar questions out there, and I have read through them, but they do not help me (or most likely I just don't understand them enough to use them).
FYI: I do not use classes, or anything like that.
The Problem
So what I am trying to do is make security cameras in my game. It moves/pans the image left until the image reaches the edge, waits like 1 second or something, then it moves/pans the image right until the image reaches the edge, wait 1 second or something, repeats. I have accomplished that, but the resulting animation looks jerky/laggy. I wish to make the animation smoother and less jerky/laggy.
EDIT: I also want to keep the same speed of the animation if possible
Here is my code;
if var_bounce == 0: if var_x <= -207: if var_x_timer == 10: var_x = var_x + 10 var_bounce = 1 var_x_timer = 0 else: var_x_timer = var_x_timer + 1 else: var_x = var_x - 10 if var_bounce == 1: if var_x >= 0: if var_x_timer == 10: var_x = var_x - 10 var_bounce = 0 var_x_timer = 0 else: var_x_timer = var_x_timer + 1 else: var_x = var_x + 10 Some Variables and Values Explained
-
The
var_xis the x value which I just place in the x value spot when I use theblitfunction.E.g.
screen.blit(image, (var_x, 0)) -
The
var_x_timeris the timer for when it needs to wait for the 1 second or something.The reason why I am using the framerate of my program rather than use a timer function because 1) I did not want to sleep/delay/stop the program just for this, and 2) I don't need it to be precise when waiting.
-
The
var_bounceis just a way for the program to know that it has reached the edge and needs to go in the opposite direction. -
The minimum and maximum x values are
0and-207respectively and this is so that the image still covers the entire screen. I got these values through trial and error, so no need to change those.
Other/Additional Information
-
The size of my images for the security cameras is
1600 x 718 -
The screen size is
1366 x 718
My Code in Action
(Ignore the other stuff)
(Looped)
1 answer
- answered 2021-06-12 07:01 Rabbid76
Do not "move" the image by 10, but move it by 1.
The method
tick()of apygame.time.Clockobject, delays the game in that way, that every iteration of the loop consumes the same period of time. Seepygame.time.Clock.tick():This method should be called once per frame.
That means that the loop:
var_x, var_bounce, speed_x = 0, 0, 2 # application loop clock = pygame.time.Clock() run = True while run: clock.tick(60) if var_bounce == 0: if var_x > -207: var_x = var_x - speed_x else: var_bounce = 1 elif var_bounce == 1: if var_x < 0: var_x = var_x + speed_x else: var_bounce = 0 # [...]
Minimal example:
import pygame pygame.init() window = pygame.display.set_mode((400, 300)) clock = pygame.time.Clock() background = pygame.Surface((window.get_width()+200, window.get_height())) ts, w, h, c1, c2 = 100, *background.get_size(), (64, 64, 64), (127, 64, 64) tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)] for rect, color in tiles: pygame.draw.rect(background, color, rect) var_x, var_bounce, speed_x = 0, 0, 2 def scroll(): global var_x, var_bounce if var_bounce == 0: if var_x > -200: var_x = var_x - speed_x else: var_bounce = 1 elif var_bounce == 1: if var_x < 0: var_x = var_x + speed_x else: var_bounce = 0 run = False while not run: clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: run = True scroll() window.blit(background, (var_x, 0)) pygame.display.flip() pygame.quit() exit()
Source: http://5.9.10.113/67944873/how-to-continuously-move-an-image-smoothly-in-pygame
0 Response to "How to Continually Move an Image in Pygame"
Post a Comment