2017-04-22 177 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 

我创建了复位方法不管在哪里,我所说的那样,无论是在一个if语句在game_logic方法在我的pygame的启动,或傍类game_logic内。它确实工作,但如果我将其设置为键绑定? 我是白痴吗?

+0

哪个变量代表球的位置?如果它是'self.point',则不会在函数'resetpong'中修改它。 –

+0

Rip,该代码是错误的。是的,我应该有self.point =(200,200)belove def resetpong(self) – puckerjugs

+0

但即便如此,拥有代码仍然不会重置球。 – puckerjugs

回答

0

函数resetpong更改self.point的值。该功能被playerpointcomppoint调用。 playerpointcomppoint的调用发生在功能game_logic中。这条线在game_logic结束:因此

self.point = (x,y) 

则会覆盖的self.point新值。类似的问题影响变量self.dx,该变量在game_logic中被设置,然后通过呼叫playerpointcomppoint而被破坏。

更改功能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包含这些数字,并且将相同的信息保存在两个不同的地方违反了一个基本原则。