2017-04-22 1 views
0

古典的なアーケードゲーム「Pong」をコードしようとすると、コンピュータのスコアの後に元の位置に「ボール」をリセットしようとしていました。pygameのオブジェクト位置をリセット

class Pong: 

    def __init__(self, width, height, x,y, color, screenw, screenh): 
      self.width = width 
      self.height = height 

      self.x = x 
      self.y = y 
      self.point = (self.x,self.y) 

      self.color = color 
      self.speed = random.randint(3,5) 

      self.screenw = screenw 
      self.screenh = screenh 

      self.dx = random.choice([-2,-1,1,2]) 
      self.dy = random.choice([1,-1]) 

      self.compscore = 0 
      self.playerscore = 0 

      self.score = False 


    def game_logic(self): 
      x,y = self.point 
      x += self.speed*self.dx 
      y += self.speed*self.dy 

      if x + self.width >= self.screenw: 
        self.dx = -1 
        self.color = GREEN 
        self.playerpoint() 
        print(str(self.playerscore)+" : "+str(self.compscore)) 
      if x <= 100: 
        self.dx = 1 
        self.color = WHITE 
        self.comppoint() 
        print(str(self.playerscore)+" : "+str(self.compscore)) 
      if y + self.height >= self.screenh: 
        self.dy = -1 
        self.color = ORANGE 
      if y <= 0: 
        self.dy = 1 
        self.color = SALMON 

      self.point = (x,y) 
      return 

    def resetpong(self): 
     self.point = (200,200) 
     self.dx = random.choice([-2,-1,1,2]) 
     self.dy = random.choice([1,-1]) 
     return self.point 

    def comppoint(self): 
      self.compscore += 1 
      print("The computer has scored a point.") 
      self.resetpong() 
      return self.compscore 

    def playerpoint(self): 
      self.playerscore += 1 
      print("Nice! You've scored a point.") 
      self.resetpong() 
      return self.playerscore 

私は、リセット方法と私はpygameのスターターでgame_logic方法で、またはポンクラスのgame_logic内かどうかをif文で、それを入れているどんなにを作成しました。私はそれをキーバインディングに設定すれば動作しますか? 私はばかですか?

+0

ボールの位置を表す変数はどれですか? 'self.point'の場合は、' resetpong'関数でそれを変更しないでください。 –

+0

リップ、そのコードは間違っています。ええ、私はself.point =(200,200)belove def resetpong(self)を持っているはずです – puckerjugs

+0

それでも、コードを持っていてもまだボールはリセットされません。 – puckerjugs

答えて

0

resetpongは、self.pointの値を変更します。この関数はplayerpointまたはcomppointによって呼び出されます。 playerpointまたはcomppointの呼び出しは、関数game_logicで発生します。 game_logicの終わりにこのライン:

self.point = (x,y) 

ゆえself.pointの新しい値を切り詰め。同様の問題は、に影響し、game_logicに設定されてから、playerpointまたはcomppointにコールすることによって騒ぎます。

変更機能game_logicこれらの両方を修正するために、次のように:

def game_logic(self): 
     x,y = self.point 
     x += self.speed*self.dx 
     y += self.speed*self.dy 
     self.point = x, y # parenthesis not needed here 

     if x + self.width >= self.screenw: 
       self.color = GREEN 
       self.playerpoint() 
       print(str(self.playerscore)+" : "+str(self.compscore)) 
     elif x <= 100: # elif here: only test this if previous if is false 
       self.color = WHITE 
       self.comppoint() 
       print(str(self.playerscore)+" : "+str(self.compscore)) 
     if y + self.height >= self.screenh: 
       self.dy = -1 
       self.color = ORANGE 
     elif y <= 0: # elif here: only test this if previous if is false 
       self.dy = 1 
       self.color = SALMON 

     # return not needed here 

私はまた、彼らは決して使用されないので、ポンコンストラクタから変数self.xself.yを削除することをおすすめします。変数self.pointにはそれらの数値が含まれています。同じ情報を2つの異なる場所に保存することは、基本原則に違反しています。

関連する問題