所以我有一小段代码,它运行正常,但是当我移动角色时,无论角色在哪里,它背后都有一张图片。想象它是用铅笔画在一张纸上,这基本上是它的表现方式。我不知道代码在哪里出错,否则运行正常。任何其他显示播放器的方式都有这个问题。帮助
import pygame,sys,os
from pygame.locals import *
pygame.init
MOVERATE = 8
WINDOWWIDTH = 1000
WINDOWHEIGHT = 1000
def terminate():
pygame.quit()
sys.exit()
playerImage = pygame.image.load('Test_Block.png')
playerRect = playerImage.get_rect()
WHITE = (255,255,255)
WindowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.update()
WindowSurface.fill(WHITE)
mainClock = pygame.time.Clock()
pygame.display.update()
while True:
playerRect.topleft = (WINDOWWIDTH /3, WINDOWHEIGHT / 3)
moveLeft = moveRight = moveUp = moveDown = False
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == ord('a'):
moveRight = False
moveLeft = True
if event.key == ord('d'):
moveLeft = False
moveRight = True
if event.key == ord('w'):
moveDown = False
moveUp = True
if event.key == ord('s'):
moveUp = False
moveDown = True
if event.type == KEYUP:
if event.type == K_ESCAPE:
terminate()
if event.key == ord('a'):
moveLeft = False
if event.key == ord('d'):
moveRight = False
if event.key == ord('w'):
moveUp = False
if event.key == ord('s'):
moveDown = False
pygame.display.update()
if moveLeft and playerRect.left > 0:
playerRect.move_ip(-1 * MOVERATE,0)
if moveRight and playerRect.right < WINDOWWIDTH:
playerRect.move_ip(MOVERATE,0)
if moveUp and playerRect.top >0:
playerRect.move_ip(0,-1 * MOVERATE)
if moveDown and playerRect.bottom < WINDOWHEIGHT:
playerRect.move_ip(0,MOVERATE)
pygame.display.update()
WindowSurface.blit(playerImage, playerRect)
pygame.display.update()
mainClock.tick(30)
pygame.display.update()
答案 0 :(得分:0)
您需要用白色填充屏幕以删除旧图像。在WindowSurface.fill(WHITE)
之前的代码中使用 WindowSurface.blit(playerImage, playerRect)
同一缩进。