在pygame中重置对象位置

时间:2017-04-22 04:34:25

标签: python python-3.x pygame computer-science

尝试编写经典的街机游戏“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启动器的game_logic方法中的if语句中,还是在Pong类的game_logic中。如果我将它设置为键绑定,它确实有效吗? 我是白痴吗?

1 个答案:

答案 0 :(得分: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

我还建议从Pong构造函数中删除变量self.xself.y,因为它们从未被使用过。变量self.point包含这些数字,它违反了一个基本原则,可以将相同的信息保存在两个不同的地方。