Pygame流畅移动

问题描述 投票:0回答:1

如何使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()
pygame smoothing
1个回答
0
投票

如何使pygame整流器平稳移动?

如果矩形必须每帧移动25像素,则在它们之间的位置绘制矩形没有任何意义。显示屏每帧更新一次,因此在它们之间的位置绘制矩形完全没有意义。可能您每秒必须减少帧数。在这种情况下,您必须提高帧率,并可以减少移动。请注意,人眼每秒只能处理一定数量的图像。诀窍是您生成足够的帧,使运动看起来对人眼来说很平滑。

pygame.Rect只能存储整数值。如果要以很高的帧率和浮点精度进行操作,则必须将对象的位置存储在单独的浮点属性中。将舍入位置同步到矩形属性。注意,您不能在窗口的“一半”像素上绘制(至少在pygame中)。

例如:

pygame.Rect
© www.soinside.com 2019 - 2024. All rights reserved.