pygameモジュールを使用してPythonでゲームを作成していて、ある特定の変数をあるクラスから別のクラスに継承したいのですが、その方法がわかりません。super()を試しましたが、うまくいきませんでしたが、初心者であるということは、間違ったことを意味している可能性があるので、うまくいく可能性としてそれを軽視しないでください。
以下のコードにも、継承したい変数をリストしました。
以下は私の関連するコードです:
FROMを継承したいクラス:
class player():
def __init__(self, x, y, width, height, walkCount):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 15
self.lastDirection = "right" or "left" or "up" or "down" #<--- variable I want to inherit
self.walkCount = walkCount
def drawCharacter(self, window):
dest = (self.x, self.y)
if self.walkCount + 1 >= 30:
self.walkCount = 0
if self.lastDirection == "left":
window.blit(protagL[0], dest)
elif self.lastDirection == "right":
window.blit(protagR[0], dest)
elif self.lastDirection == "up":
window.blit(protagU[0], dest)
elif self.lastDirection == "down":
window.blit(protagD[0], dest)
継承を受け取りたいクラス:
class projectile(player):
def __init__(self, x, y, direction, travelCount):
self.x = x
self.y = y
self.direction = direction
self.vel = 20 * direction
self.travelCount = travelCount
def drawBullet(self, window):
dest = (self.x, self.y)
if self.travelCount + 1 >= 15:
self.walkCount = 0
if lastDirection == "right" or "left": # <--- I Want to call lastDirection variable here
if self.travelCount < 15:
window.blit(bulletsP[self.travelCount//3], dest) #dest)
self.travelCount += 1
self.x += self.vel
どんな助けでも大歓迎です、ありがとう。
基本クラスのコンストラクターを呼び出す必要があります。参照super()
:
class projectile(player):
def __init__(self, x, y, direction, travelCount):
super().__init__(x, y, 10, 10, travelCount)
# [...]
あなたの例では幅と高さの引数が不明確であることに注意してください。そのため、私は例として(10、10)を使用しました。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加