问题描述
我正在读一本有关Python的书,Python速成课程,它在项目中使用了此代码。
import pygame
class Ship:
def __init__(self,screen):
self.screen = screen
# load ship and get its rect
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
# start each new ship at the bottom center of the screen
self.rect.centerx = self.screen.centerx
self.rect.bottom = self.screen.bottom
def blitme(self):
# draw the ship at its current location
self.screen.blit(self.image,self.rect)
AttributeError: 'pygame.Surface' object has no attribute 'centerx'
解决方法
self.screen
是与屏幕关联的pygame.Surface
对象。您必须获得一个pygame.Rect
对象,其表面尺寸为get_rect()
:
screen_rect = self.screen.get_rect()
self.rect.centerx = screen_rect.centerx
self.rect.bottom = screen_rect.bottom