AttributeError:'Player'对象在python中没有属性'Player\u xpos'

2024-07-08 10:54:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经使用pygame编程了游戏“Pong”,并且在没有使用面向对象编程的情况下取得了很好的效果,但是现在我想使用OOP重写代码,然后问题就开始了

当尝试绘制与玩家对应的条(矩形)时,标题中提到的错误出现在我的眼前

p1 = Player (10, p1y)
  File "PongPOO.py", line 32, in __init__
    sprite = pygame.draw.rect (window, white, (self.player_xpos, self.player_ypos, PLAYER_WIDTH, PLAYER_HEIGTH)) #pygame rect
AttributeError: 'Player' object has no attribute 'player_xpos'

我会复制代码以便你能帮助我

PLAYER_WIDTH = 10
PLAYER_HEIGTH = 100
PLAYER_POSY_INIT = (resY/2)-(PLAYER_HEIGTH/2)
p1y=PLAYER_POSY_INIT
p2y=PLAYER_POSY_INIT

class Player():

    def __init__(self, player_xpos, player_ypos):
        sprite = pygame.draw.rect(window, white, (self.player_xpos, self.player_ypos, PLAYER_WIDTH, PLAYER_HEIGTH)) #pygame rect
        speed = 2

正在创建Player对象:

    p1 = Player(10, p1y)
    p2 = Player(resx-20, p2y)

Tags: 代码rectselfinitwidthpygameplayerp1

热门问题