私は「Python プログラミング フォー ジ アブソリュート ビギナー」という本を読んでいて、自分のゲームを作って自分のスキルを試すことにしました。ゲームは基本的に「飛んでいるスパイクに当たらない」というもので、私はそれで問題に遭遇しました。このコードで実行すると:
class Player(games.Sprite):
"""The player that must dodge the spikes."""
def update(self):
"""Move to the mouse."""
self.x = games.mouse.x
self.y = games.mouse.y
self.check_collide()
def check_collide(self):
"""Check for a collision with the spikes."""
for spike in self.overlapping_sprites:
spike.handle_collide()
def main():
pig_image = games.load_image("Mr_Pig.png")
the_pig = Player(image = pig_image,
x = games.mouse.x,
y = games.mouse.y)
games.screen.add(the_pig)
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
main()
問題ありません。しかし、次のコードのように「 init 」を使用したい場合:
class Player(games.Sprite):
"""The player that must dodge the spikes."""
def update(self):
"""Move to the mouse."""
self.x = games.mouse.x
self.y = games.mouse.y
self.check_collide()
def check_collide(self):
"""Check for a collision with the spikes."""
for spike in self.overlapping_sprites:
spike.handle_collide()
def __init__(self):
"""A test!"""
print("Test.")
def main():
pig_image = games.load_image("Mr_Pig.png")
the_pig = Player(image = pig_image,
x = games.mouse.x,
y = games.mouse.y)
games.screen.add(the_pig)
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
main()
ゲームを実行すると、次のエラーが表示されます。
File "run.py", line 85, in main
y = games.mouse.y)
TypeError: __init__() got an unexpected keyword argument 'y'.