我怎么能重启我的Pong游戏并保持我的分数工作?

时间:2018-03-11 03:10:06

标签: python tkinter

我一直在为我的第一个完整的Python项目开发一个Pong游戏,主要是使用tkinter进行大多数操作。但是,我无法弄清楚如何使用我添加的重启按钮重启程序而不禁用左上角的分数计数器。 以下文字是我的代码:

from tkinter import *
import random
import time
import pygame
import os, sys
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='red')
        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
        self.score = 0

    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
        if self.hit_paddle(pos) == True:
            self.score += 1

class Paddle:
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_rectangle(0, 0, 150, 10, fill='blue')
        self.canvas.move(self.id, 450, 750)
        self.x = 0
        self.y = 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)
        self.canvas.bind_all('<KeyRelease-Left>', self.stop_left)
        self.canvas.bind_all('<KeyRelease-Right>', self.stop_right)
        self.canvas.bind_all('<KeyPress-Up>', self.turn_up)
        self.canvas.bind_all('<KeyPress-Down>', self.turn_down)
        self.canvas.bind_all('<KeyRelease-Up>', self.stop_up)
        self.canvas.bind_all('<KeyRelease-Down>', self.stop_down)

    def draw(self):
        self.canvas.move(self.id, self.x, self.y)
        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
    def stop_left(self, evt):
        self.x = 0
    def stop_right(self, evt):
        self.x = 0
    def turn_up(self, evt):
        self.y = -0
    def turn_down(self, evt):
        self.y = 0
    def stop_up(self, evt):
        self.y = 0
    def stop_down(self, evt):
        self.y = 0
def restart ():
    global paddle, ball
    canvas.delete (ALL)
    paddle = Paddle(canvas, 'blue')
    ball = Ball(canvas, paddle, 'red')
    label = canvas.create_text(5, 5, anchor=NW, text="Score: 0", font=('Courier, 40'), fill='white')
    canvas.itemconfig(label, text="Score: "+str(ball.score))

def main ():
    global paddle, ball
    tk.update()

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

    while 1:
        if ball.hit_bottom == False:
            ball.draw()
            paddle.draw()
        tk.update_idletasks()
        tk.update()
        time.sleep(0.01)
        if ball.hit_bottom == True:
            canvas.create_text(525, 300, text='Game Over', fill='red', font=('Courier', 60))
        canvas.itemconfig(label, text="Score: "+str(ball.score))

tk = Tk()
tk.title("User's Pong Game")
tk.resizable(0, 0)
tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width=1000, height=800, bd=0, highlightthickness=0)
canvas.configure(background='black')
canvas.grid (row = 0)
label = canvas.create_text(5, 5, anchor=NW, text="Score: 0", font=('Courier, 40'), fill='white')
Button = Button(tk, anchor=NE, text = "Start Over", command = restart)
Button.grid(row=1, column=1)

tk.configure(background='white')

main ()

1 个答案:

答案 0 :(得分:1)

替换:

canvas.itemconfig(label, text="Score: "+str(ball.score))

使用:

while 1:
    if ball.hit_bottom == False:
        ball.draw()
        paddle.draw()
    tk.update_idletasks()
    tk.update()
    time.sleep(0.01)
    if ball.hit_bottom == True:
        canvas.create_text(525, 300, text='Game Over', fill='red', font=('Courier', 60))
    canvas.itemconfig(label, text="Score: "+str(ball.score))

restart方法中。

我如何调试它:

  1. 复制并运行整个代码,尝试按钮,看到得分静态为0
  2. 在脚本中搜索“得分”以查看脚本中的修改位置
  3. 注意到缺乏时间,因此解决了它。