0
我正在爲一個流氓喜歡的MVP工作,而且我遇到了一個不會發生的錯誤,只要我在其他任何東西上執行它。AttributeError:'pygame.Surface'對象沒有任何屬性'display'
import pygame,sys,os
from pygame.locals import *
pygame.init
MOVERATE = 10
WINDOWWIDTH = 500
WINDOWHEIGHT = 500
def terminate():
pygame.quit()
sys.exit()
playerRect = pygame.image.load('Test_Block.png')
playerImage = playerRect.get_rect()
WHITE = (255,255,255,0)
WindowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.update()
WindowSurface.fill(WHITE)
mainClock = pygame.time.Clock()
while True:
moveLeft = moveRight = moveUp = moveDown = False
playerRect.topleft = (WINDOWHEIGHT/2),(WINDOWWIDTH/2)
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.type == ord('d'):
moveRight = False
if event.key == ord('w'):
moveUp = False
if event.key == ord('s'):
moveDown = False
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)
WindowSurface.blit(playerImage,PlayerRect)
pygame.display.update()
mainClock.tick(30)
當我運行它,我得到這個錯誤:
Traceback (most recent call last):
File "/Users/peterbrown/Desktop/data/Rogue/Rogue.py", line 32, in <module>
playerRect.topleft = (WINDOWHEIGHT/2),(WINDOWWIDTH/2)
AttributeError: 'pygame.Surface' object has no attribute 'topleft'
可以給我這樣的人解釋什麼是錯,以及如何解決它?
拍哦,謝謝!沒有注意 –
@JohnDoe接受答案,因爲它幫助你。 (點擊複選標記) – pydude