Python 如何使我的对象在Pygame中移动时不留下痕迹?
好的,所以我无法想出如何从屏幕上每帧删除对象的前一个渲染,以便不留下痕迹,换句话说,我希望对象具有运动的外观,而不是留下一堆红色方块。插入图像是为了理解我留下痕迹的意思,以下是我的代码:-Python 如何使我的对象在Pygame中移动时不留下痕迹?,python,python-3.x,pygame,Python,Python 3.x,Pygame,好的,所以我无法想出如何从屏幕上每帧删除对象的前一个渲染,以便不留下痕迹,换句话说,我希望对象具有运动的外观,而不是留下一堆红色方块。插入图像是为了理解我留下痕迹的意思,以下是我的代码:- import pygame import random WIDTH = 1000 ROWS = 25 WIN = pygame.display.set_mode((WIDTH,WIDTH)) pygame.display.set_caption("Particle Physics") c
import pygame
import random
WIDTH = 1000
ROWS = 25
WIN = pygame.display.set_mode((WIDTH,WIDTH))
pygame.display.set_caption("Particle Physics")
clock = pygame.time.Clock()
RED = (255, 0, 0)
GRAVITY = 2
wid = 5
class Square:
def __init__(self,x,y,width,color,vel_x,vel_y):
self.x = x
self.y = y
self.width = width
self.color = color
self.velx = vel_x
self.vely = vel_y
def draw(self):
pygame.draw.rect(WIN, self.color, (self.x, self.y, self.width, self.width))
def move(self):
self.x += self.velx
self.y += self.vely
self.vely += GRAVITY[enter image description here][1]
def particles():
pass
def main():
run = True
a,b = 500,300
c = random.randint(-20,20)
d = random.randint(-50,0)
square = Square(a,b,wid, RED, c, d)
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
square.move()
square.draw()
pygame.display.update()
pygame.time.delay(100)
#clock.tick(60)
pygame.quit()
main()
在主功能中,在开始处添加设置的背景色,以便删除以前的帧
def main()
...
while run
....
screen.fill((255,255,255))
...
在主功能中,在开始处添加设置的背景色,以便删除以前的帧
def main()
...
while run
....
screen.fill((255,255,255))
...
非常感谢,非常感谢您的帮助在每帧开始时清除屏幕太慢了。新手教程https://www.pygame.org/docs/tut/newbieguide.html 解释如何仅更新包含上一个精灵位置的矩形。请参见脏矩形部分Animation@SiddharthAgrawal我该怎么做?对不起,我是新手,我的答案旁边有个记号。单击它,它将变为绿色。当我第一次加入时,我也不知道这一点。非常感谢你的帮助。在每帧开始时清除屏幕的速度太慢了。新手教程https://www.pygame.org/docs/tut/newbieguide.html 解释如何仅更新包含上一个精灵位置的矩形。请参见脏矩形部分Animation@SiddharthAgrawal我该怎么做?对不起,我是新手,我的答案旁边有个记号。单击它,它将变为绿色。我刚加入时也不知道这件事