【发布时间】:2019-03-16 06:06:01
【问题描述】:
下面有两个程序试图在黄色屏幕上画一个球。一个是我的表面对象在 while 循环之外填充,另一个是在 while 循环内部填充。
当我在 while 循环之外有 .fill() 时,当我尝试移动它时,我的图像会被重绘。因此,当图像下降时,它会在屏幕下方仅 10 像素处绘制一个重复的图像。
当我在 while 循环中有 .fill() 时,我的单个图像会更新并重新绘制到新位置。
这是为什么?
非尾随代码:
import pygame
import sys
length = 1200
width = 800
screen = pygame.display.set_mode((length, width))
screen_rect = screen.get_rect()
ball = pygame.image.load('ball.bmp')
ball_rect = ball.get_rect()
ball_rect.y = ball_rect.width
ball_rect.x = screen_rect.centerx
while True:
screen.fill((255, 255, 103))
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
ball_rect.y += 15
screen.blit(ball, ball_rect)
pygame.display.flip()
尾随图像代码:
import pygame
import sys
length = 1200
width = 800
screen = pygame.display.set_mode((length, width))
screen_rect = screen.get_rect()
screen.fill((255, 255, 103))
ball = pygame.image.load('ball.bmp')
ball_rect = ball.get_rect()
ball_rect.y = ball_rect.width
ball_rect.x = screen_rect.centerx
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
ball_rect.y += 15
screen.blit(ball, ball_rect)
pygame.display.flip()
【问题讨论】:
-
欢迎来到 Stack Overflow。请使用tour 并阅读有关How to Ask 的信息。请提供minimal reproducible example,以便我们能够提供帮助。代码作为文本而不是图像更好,因此更容易为其他人运行。
-
请编辑您的问题以包含程序的文本。