2017-04-22 9 views
0

고전적인 아케이드 게임 'Pong'을 코딩하려고 시도한 결과, 컴퓨터 점수 뒤에 원래 위치로 '공'을 재설정하려고 시도했습니다. 내 파이 게임 스타터에 game_logic 방법에 여부 if 문에 넣어, 또는 퐁 클래스의 game_logic 안에 한 곳파이 게임에서 개체 위치 재설정

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 

나는 리셋 방법과 상관없이 만들었습니다. 키 바인딩으로 설정하면 작동합니까? 나는 바보인가?

+0

공의 위치를 ​​나타내는 변수는 무엇입니까? 'self.point' 인 경우,'resetpong' 함수에서 그것을 수정하지 않습니다. –

+0

RIP, 코드가 잘못되었습니다. 예, self.point = (200,200) belove def resetpong (self) – puckerjugs

+0

그래도 그렇다고해도 코드를 유지하는 것은 여전히 ​​공을 재설정하지 않습니다. – puckerjugs

답변

0

기능 resetpongself.point의 값을 변경합니다. 이 함수는 playerpoint 또는 comppoint에 의해 호출됩니다. playerpoint 또는 comppoint에 대한 호출은 game_logic 함수에서 발생합니다. game_logic의 끝이 선 :

self.point = (x,y) 

그러므로 self.point의 새 값을 내리 쳤을 때. 유사한 문제가 에 설정되고 playerpoint 또는 comppoint에 대한 호출에 의해 clobbered되는 변수 self.dx에 영향을줍니다.

변경이 두 가지를 해결하기 위해 다음과 같은 기능 game_logic : 나는 또한 그들이 사용되지 않습니다 때문에 퐁 생성자에서 변수 self.xself.y을 제거하는 것이 좋습니다

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.point는 이러한 숫자를 포함하고 있으며 동일한 정보를 두 곳의 다른 장소에 유지하는 기본 원칙을 위반합니다.