我已经使用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)
目前没有回答
相关问题 更多 >
编程相关推荐