Pygame 流畅的动作
Pygame smooth movement
我怎样才能让 pygame 矩形平滑移动?就像我将 x 位置更新为 2 它看起来很平滑但是如果我将它更新为更大的数字如 25 它会传送到该位置。另外,如果可能的话,这也适用于小数吗?
Visual Representation
import pygame
import math
GREEN = (20, 255, 140)
GREY = (210, 210 ,210)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
PURPLE = (255, 0, 255)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
class Dot(pygame.sprite.Sprite):
# This class represents a car. It derives from the "Sprite" class in Pygame.
def __init__(self, color, width, height):
# Call the parent class (Sprite) constructor
super().__init__()
# Pass in the color of the car, and its x and y position, width and height.
# Set the background color and set it to be transparent
self.image = pygame.Surface([width, height])
self.image.fill(WHITE)
self.image.set_colorkey(WHITE)
self.color = color
self.width = width
self.height = height
pygame.draw.rect(self.image, self.color, [0, 0, self.width, self.height])
self.rect = self.image.get_rect()
How can I get a pygame rect to move smoothly?
如果您的矩形必须每帧移动 25px,那么在中间位置绘制矩形就没有意义。显示每帧更新一次,在中间位置绘制矩形完全没有意义。
可能你每秒必须减少帧数。在那种情况下,您必须增加帧率,并且可以减少运动。请注意,人眼每秒只能处理一定数量的图像。诀窍在于您生成了足够多的帧,这样运动对于人眼来说就显得平滑了。
pygame.Rect
只能存储整数值。如果你想以非常高的帧率和浮动精度进行操作,那么你必须将对象的位置存储在单独的浮点属性中。将圆角位置同步到矩形属性。请注意,您不能在 window 的 "half" 像素上绘制(至少在 pygame 中)。
例如:
class Dot(pygame.sprite.Sprite):
# This class represents a car. It derives from the "Sprite" class in Pygame.
def __init__(self, color, x, y, width, height):
# Call the parent class (Sprite) constructor
super().__init__()
# Pass in the color of the car, and its x and y position, width and height.
# Set the background color and set it to be transparent
self.x = x
self.y = y
self.image = pygame.Surface([width, height])
self.image.fill(self.color)
self.rect = self.image.get_rect(center = (round(x), round(y)))
def update(self):
# update position of object (change `self.x`, ``self.y``)
# [...]
# synchronize position to `.rect`
self.rect.center = (round(x), round(y))
我怎样才能让 pygame 矩形平滑移动?就像我将 x 位置更新为 2 它看起来很平滑但是如果我将它更新为更大的数字如 25 它会传送到该位置。另外,如果可能的话,这也适用于小数吗?
Visual Representation
import pygame
import math
GREEN = (20, 255, 140)
GREY = (210, 210 ,210)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
PURPLE = (255, 0, 255)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
class Dot(pygame.sprite.Sprite):
# This class represents a car. It derives from the "Sprite" class in Pygame.
def __init__(self, color, width, height):
# Call the parent class (Sprite) constructor
super().__init__()
# Pass in the color of the car, and its x and y position, width and height.
# Set the background color and set it to be transparent
self.image = pygame.Surface([width, height])
self.image.fill(WHITE)
self.image.set_colorkey(WHITE)
self.color = color
self.width = width
self.height = height
pygame.draw.rect(self.image, self.color, [0, 0, self.width, self.height])
self.rect = self.image.get_rect()
How can I get a pygame rect to move smoothly?
如果您的矩形必须每帧移动 25px,那么在中间位置绘制矩形就没有意义。显示每帧更新一次,在中间位置绘制矩形完全没有意义。
可能你每秒必须减少帧数。在那种情况下,您必须增加帧率,并且可以减少运动。请注意,人眼每秒只能处理一定数量的图像。诀窍在于您生成了足够多的帧,这样运动对于人眼来说就显得平滑了。
pygame.Rect
只能存储整数值。如果你想以非常高的帧率和浮动精度进行操作,那么你必须将对象的位置存储在单独的浮点属性中。将圆角位置同步到矩形属性。请注意,您不能在 window 的 "half" 像素上绘制(至少在 pygame 中)。
例如:
class Dot(pygame.sprite.Sprite):
# This class represents a car. It derives from the "Sprite" class in Pygame.
def __init__(self, color, x, y, width, height):
# Call the parent class (Sprite) constructor
super().__init__()
# Pass in the color of the car, and its x and y position, width and height.
# Set the background color and set it to be transparent
self.x = x
self.y = y
self.image = pygame.Surface([width, height])
self.image.fill(self.color)
self.rect = self.image.get_rect(center = (round(x), round(y)))
def update(self):
# update position of object (change `self.x`, ``self.y``)
# [...]
# synchronize position to `.rect`
self.rect.center = (round(x), round(y))