微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AttributeError:“ pygame.Surface”对象没有属性“ centerx”

如何解决AttributeError:“ pygame.Surface”对象没有属性“ centerx”

我正在读一本有关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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。