고전적인 아케이드 게임 '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
나는 리셋 방법과 상관없이 만들었습니다. 키 바인딩으로 설정하면 작동합니까? 나는 바보인가?
공의 위치를 나타내는 변수는 무엇입니까? 'self.point' 인 경우,'resetpong' 함수에서 그것을 수정하지 않습니다. –
RIP, 코드가 잘못되었습니다. 예, self.point = (200,200) belove def resetpong (self) – puckerjugs
그래도 그렇다고해도 코드를 유지하는 것은 여전히 공을 재설정하지 않습니다. – puckerjugs