游戏结束后停止球动画的问题

Issue with stopping ball animation after game ends

对于我正在编写的基本乒乓球游戏,我正在处理结束游戏(不是复仇者联盟)的效果。我做了一个按钮,说再次播放,我正在尝试发出命令,以便当我按下按钮时,球 pos.x 和球 pos.y 将 "reset" 到中间屏幕,然后继续移动。

from tkinter import*
import random
import time

class Ball:
    def __init__(self, canvas, paddle, color):
        self.canvas = canvas
        self.paddle = paddle
        self.id = canvas.create_oval(10,10,25,25, fill=color)
        self.canvas.move(self.id, 245, 100)
        starts = [-3, -2, -1, 1, 2, 3]
        random.shuffle(starts)
        self.x = starts[0]
        self.y = -3
        self.canvas_height = self.canvas.winfo_height()
        self.canvas_width = self.canvas.winfo_width()
        self.hit_bottom = False


    def hit_paddle(self, pos):
        paddle_pos = self.canvas.coords(self.paddle.id)
        if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
            if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
                return True
        return False


    def draw(self):
        self.canvas.move(self.id, self.x, self.y)
        pos = self.canvas.coords(self.id)
        if pos[1] <= 0:
            self.y = 3
        if pos[3] >= self.canvas_height:
            self.hit_bottom = True
        if self.hit_paddle(pos) == True:
            self.y = -3
        if pos[0] <= 0:
            self.x = 3
        if pos[2] >= self.canvas_width:
            self.x = -3



class Paddle:
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_rectangle(0, 0, 100, 10, fill=color)
        self.canvas.move(self.id, 200, 300)
        self.x = 0
        self.canvas_width = self.canvas.winfo_width()
        self.canvas.bind_all('<KeyPress-Left>',self.turn_left)
        self.canvas.bind_all('<KeyPress-Right>', self.turn_right)


    def draw(self):
        self.canvas.move(self.id, self.x, 0)
        pos = self.canvas.coords(self.id)
        if pos[0] <= 0:
            self.x = 0
        elif pos[2] >= self.canvas_width:
            self.x = 0


    def turn_left(self, evt):
        self. x = -3


    def turn_right(self, evt):
        self.x = 3


tk = Tk()
tk.title("Game")
tk.resizable(0, 0)
tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0)
canvas.pack()
tk.update()

paddle = Paddle(canvas, 'blue')
ball = Ball(canvas, paddle, 'red')



def Restart():
    #This is where the code for restart was, but it failed so...



button = Button(tk, text="Play Again", command=Restart)
button.config(height=3, width= 15, fg='white', bg='#28fc03', activebackground='#28fc03')
canvas.configure(bg='black')


while 1:
    if ball.hit_bottom == True:
        Restart()
        button.place(x=195, y=125)
    if ball.hit_bottom == False:
        ball.draw()
        paddle.draw()
    tk.update_idletasks()
    tk.update()
    time.sleep(0.01)

唯一的问题是,当我制作一个重置功能将球移到中间时,我在底部也有一个 'while 1' 循环 运行,我认为它有一些东西与这样一个事实有关,即无论我尝试做什么,球都不会重置,只会停留在屏幕底部。我还没有编写让再次播放按钮消失的代码,但这并不难,我很快就会这样做。

简单解释一下,有没有办法停止 ball.draw() 和paddle.draw() 循环?

经过测试,我发现你的 Restart 可以正常工作,但是重复运行,所以它确实可以工作,但也许你在 def restart 中的程序有一些错误。

为了猜测,我想你忘了将 ball.hit_bottom 设置回 false。

几分钟后,我用另一种方式修复它,我想这样你就可以把它做成你想要的东西,因为我很困惑。

import random
import time

class Ball:
    def __init__(self, canvas, paddle, color):
        self.canvas = canvas
        self.paddle = paddle
        self.id = canvas.create_oval(10,10,25,25, fill=color)
        self.canvas.move(self.id, 245, 100)
        starts = [-3, -2, -1, 1, 2, 3]
        random.shuffle(starts)
        self.x = starts[0]
        self.y = -3
        self.canvas_height = self.canvas.winfo_height()
        self.canvas_width = self.canvas.winfo_width()
        self.hit_bottom = False


    def hit_paddle(self, pos):
        paddle_pos = self.canvas.coords(self.paddle.id)
        if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
            if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
                return True
        return False


    def draw(self):
        self.canvas.move(self.id, self.x, self.y)
        pos = self.canvas.coords(self.id)
        if pos[1] <= 0:
            self.y = 3
        if pos[3] >= self.canvas_height:
            self.hit_bottom = True
        if self.hit_paddle(pos) == True:
            self.y = -3
        if pos[0] <= 0:
            self.x = 3
        if pos[2] >= self.canvas_width:
            self.x = -3
#here's the part i added
    def place(self,x,y):
        starts = [-3, -2, -1, 1, 2, 3]#confuse
        self.x = starts[0]
        self.y = -3


class Paddle:
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_rectangle(0, 0, 100, 10, fill=color)
        self.canvas.move(self.id, 200, 300)
        self.x = 0
        self.canvas_width = self.canvas.winfo_width()
        self.canvas.bind_all('<KeyPress-Left>',self.turn_left)
        self.canvas.bind_all('<KeyPress-Right>', self.turn_right)


    def draw(self):
        self.canvas.move(self.id, self.x, 0)
        pos = self.canvas.coords(self.id)
        if pos[0] <= 0:
            self.x = 0
        elif pos[2] >= self.canvas_width:
            self.x = 0


    def turn_left(self, evt):
        self. x = -3


    def turn_right(self, evt):
        self.x = 3


tk = Tk()
tk.title("Game")
tk.resizable(0, 0)
tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0)
canvas.pack()
tk.update()

paddle = Paddle(canvas, 'blue')
ball = Ball(canvas, paddle, 'red')



def Restart():# i changed some stuff for testing,but it manages to work a bit
    print(1)
    ball.place(0,0)
    ball.hit_bottom = False
    #This is where the code for restart was, but it failed so...



button = Button(tk, text="Play Again", command=Restart)
button.config(height=3, width= 15, fg='white', bg='#28fc03', activebackground='#28fc03')
canvas.configure(bg='black')

while 1:

    if ball.hit_bottom == True:
        button.place(x=195, y=125)

        #Restart() #try with that?
    elif ball.hit_bottom == False:
        ball.draw()
        paddle.draw()
    tk.update_idletasks()
    tk.update()
    time.sleep(0.01)