Reputation: 136
Im working on a MVP for a rogue like, and I encountered an error that doesn't happen whenever i do it on anything else.
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)
When I run this, i get this error:
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'
Can someone explain to me what is wrong with this, as well as how to fix it?
Upvotes: 0
Views: 5818
Reputation: 60070
It looks like you mixed up your variable names here:
playerRect = pygame.image.load('Test_Block.png')
playerImage = playerRect.get_rect()
I think what you wanted instead is:
playerImage = pygame.image.load('Test_Block.png')
playerRect = playerRect.get_rect()
Upvotes: 1