【发布时间】:2017-09-19 04:44:09
【问题描述】:
尝试编写经典街机游戏“Pong”时,我在计算机得分后尝试将“球”重置到其原始位置时遇到了困难。
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
我已经创建了 reset 方法,无论我把它放在哪里,无论是在我的 pygame starter 的 game_logic 方法的 if 语句中,还是在 Pong 类的 game_logic 中。如果我将其设置为键绑定,它确实有效吗? 我是白痴吗?
【问题讨论】:
-
哪个变量代表球的位置?如果是
self.point,则不要在函数resetpong中修改。 -
Rip,那个代码是错误的。是的,我应该有 self.point = (200,200) belove def resetpong(self)
-
但即便如此,拥有原样的代码仍然不会重置球。
标签: python python-3.x pygame computer-science