1

我正在使用 pygame 构建一个简单的游戏,这个游戏的功能是控制图像并使其向上/向下左/右移动但我遇到了这个问题,图像在向下或向一边移动时会重复<code>图片</code>

import pygame
pygame.init()
display_width = 1080
display_height = 1080

screen = pygame.display.set_mode((display_width,display_height))

Clock = pygame.time.Clock()
pygame.display.set_caption('DEMO')
black=(0,0,0)
white=(255,255,255)
red=(255,0,0)
green=(0,255,0)
blue=(0,0,255)
screen.fill(white)

face=pygame.image.load('H:\\brain.jpg')

 def brain(x,y):
     screen.blit(face,(x,y))

 x1 =(display_width*0.5)
 y1 =(display_height*0.5)
  x_change =0
   y_change =0
  gameExit = False

while not gameExit:
for event in pygame.event.get():

    if event.type == pygame.QUIT:
        gameExit = True

    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            x_change = -5
        elif  event.key == pygame.K_RIGHT:
            x_change = 5
        elif event.key == pygame.K_DOWN:
            y_change = 5
        elif event.key == pygame.K_UP:
            y_change = -5
    if event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
            x_change = 0
    if event.type == pygame.KEYUP:
        if event.key == pygame.K_DOWN or event.key == pygame.K_UP:
            y_change = 0

x1 += x_change
y1 += y_change
print(event)
brain(x1,y1)
pygame.display.update()
Clock.tick(100)
pygame.quit()
quit()
4

2 回答 2

3

您必须在 while 循环的每次迭代中对背景进行 blit:

 while True:
     for event in pygame.event.get():

         #get keyboard input


     screen.fill((255, 255, 255)) #or, you can draw a background image

     x1 += x_change
     y1 += y_change
     print(event)
     brain(x1,y1)
     pygame.display.update()
于 2017-06-26T23:17:03.630 回答
0

您需要清除旧图像,并绘制新图像。您看到的是残留物:之前图像中未被新图像的白色(背景颜色)覆盖的部分。

处理此问题的一种简单方法是将绘制的图像扩展为包含至少与运动大小一样大的白色边框。

于 2017-06-26T23:15:13.073 回答