为什么我的乒乓球比赛在球触底之后冻结了,并且没有显示“游戏结束”屏幕?

时间:2018-06-30 04:59:40

标签: python tkinter

我是一名高中生,也是Python爱好者。我正在尝试使用python制作乒乓球游戏,并且大多数程序都可以正常工作,但是我正在尝试让程序在球开始移动之前等待3秒钟,并且当球撞击底部时,程序只会冻结并且不会显示“游戏结束”屏幕。有人可以帮助我吗?这是我正在处理的代码。

 from tkinter import *
 import random
 import time

 class Ball:
     def __init__(self, canvas, color, size, paddle):
         self.canvas = canvas
         self.paddle = paddle
         self.id = canvas.create_oval(15, 15, size, size, fill=color)
         self.canvas.move(self.id, 245, 100)
         self.xspeed = random.randrange(-3,3)
         self.yspeed = -1
         self.hit_bottom = False
         self.score = 0

     def draw(self):
         self.canvas.move(self.id, self.xspeed, self.yspeed)
         pos = self.canvas.coords(self.id)
         if pos[1] <= 0:
             self.yspeed = 9
         if pos[3] >= 400:
             self.hit_bottom = True
         if pos[0] <= 0:
             self.xspeed = 9
         if pos[2] >= 500:
             self.xspeed = -9
         if self.hit_paddle(pos) == True:
             self.yspeed = -9
             self.xspeed = random.randrange(-3,3)
             self.score += 1

    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

class Paddle:
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_rectangle(0,0, 168, 20, fill=color)
        self.canvas.move(self.id, 200, 300)
        self.xspeed = 0
        self.canvas.bind_all('<KeyPress-Left>', self.move_left)
        self.canvas.bind_all('<KeyPress-Right>', self.move_right)

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

    def move_left(self, evt):
        self.xspeed = -12
    def move_right(self, evt):
        self.xspeed = 12

tk = Tk()
tk.title("Pong")
canvas = Canvas(tk, width=500, height=400, bd=0, bg='#ff4fea')
canvas.pack()
label = canvas.create_text(5, 5, anchor=NW, text="Score: 0")
tk.update()
paddle = Paddle(canvas, '#2be2ff')
ball = Ball(canvas, '#ddff00', 38, paddle)

while ball.hit_bottom == False:
    ball.draw()
    paddle.draw()
    canvas.itemconfig(label, text="Score: "+str(ball.score))
    tk.update_idletasks()
    tk.update()
    time.sleep(0.01)

while ball.hit_bottom == True:
    print
    go_label = canvas.create_text(250,200,text="Game Over Play Again")
tk.update()

1 个答案:

答案 0 :(得分:1)

您不应在tkinter应用程序中使用time.sleep;请改用root.after
您也不需要在while循环内创建自己的游戏循环; tk.mainloop为您做到了。
几乎不需要调用updateupdate_idle_tasks;让tkinter为您处理所有这些低级操作。

这是Pong应用程序的有效版本,可以解决主要问题并回答您的问题。它不会重新启动游戏,您必须自己解决这个问题。也许您想将游戏逻辑放在可以为您处理重新启动的ans设置的类中?如果您遇到困难,请提出一个新问题。

我将星形导入更改为import tkinter as tk,这使命名空间保持整洁,然后将变量tk重命名为 而是使用root

import tkinter as tk
import random


class Ball:

    def __init__(self, canvas, color, size, paddle):
        self.canvas = canvas
        self.paddle = paddle
        self.id = canvas.create_oval(15, 15, size, size, fill=color)
        self.canvas.move(self.id, 245, 100)
        self.xspeed = random.randrange(-3,3)
        self.yspeed = -1
        self.hit_bottom = False
        self.score = 0

    def draw(self):
        self.canvas.move(self.id, self.xspeed, self.yspeed)
        pos = self.canvas.coords(self.id)
        if pos[1] <= 0:
            self.yspeed = 9
        if pos[3] >= 400:
            self.hit_bottom = True
        if pos[0] <= 0:
            self.xspeed = 9
        if pos[2] >= 500:
            self.xspeed = -9
        if self.hit_paddle(pos):
            self.yspeed = -9
            self.xspeed = random.randrange(-3,3)
            self.score += 1

    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] and \
                pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
            return True
        return False


class Paddle:

    def __init__(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_rectangle(0, 0, 168, 20, fill=color)
        self.canvas.move(self.id, 200, 300)
        self.xspeed = 0
        self.canvas.bind_all('<KeyPress-Left>', self.move_left)
        self.canvas.bind_all('<KeyPress-Right>', self.move_right)

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

    def move_left(self, evt):
        self.xspeed = -12

    def move_right(self, evt):
        self.xspeed = 12


def run_game():
    ball.draw()
    paddle.draw()
    canvas.itemconfig(label, text="Score: "+str(ball.score))
    if not ball.hit_bottom:
        root.after(10, run_game)
    else:
        canvas.create_text(250, 200, text="Game Over Play Again")


if __name__ == '__main__':

    root = tk.Tk()
    root.title("Pong")
    canvas = tk.Canvas(root, width=500, height=400, bd=0, bg='#ff4fea')
    canvas.pack()
    label = canvas.create_text(5, 5, anchor=tk.NW, text="Score: 0")
    paddle = Paddle(canvas, '#2be2ff')
    ball = Ball(canvas, '#ddff00', 38, paddle)

    run_game()
    tk.mainloop()