Python 我怎样才能重新开始我的乒乓球比赛,并保持我的分数工作?
我一直在为我的第一个完整Python项目开发一个乒乓球游戏,大部分操作都使用tkinter。但是,如果不禁用左上角的分数计数器,我就无法使用我添加的重新启动按钮重新启动程序。 以下是我的代码:Python 我怎样才能重新开始我的乒乓球比赛,并保持我的分数工作?,python,tkinter,Python,Tkinter,我一直在为我的第一个完整Python项目开发一个乒乓球游戏,大部分操作都使用tkinter。但是,如果不禁用左上角的分数计数器,我就无法使用我添加的重新启动按钮重新启动程序。 以下是我的代码: from tkinter import * import random import time import pygame import os, sys import time class Ball: def __init__(self, canvas, paddle, color):
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 ()
从tkinter导入*
随机输入
导入时间
导入pygame
导入操作系统,系统
导入时间
班级舞会:
定义初始(自我、画布、桨、颜色):
self.canvas=画布
自桨
self.id=canvas.create_oval(10,10,25,25,fill='red')
self.canvas.move(self.id,245100)
开始=[-3,-2,-1,1,2,3]
随机。洗牌(开始)
self.x=开始[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_挡板(自身,位置):
palle\u pos=self.canvas.coords(self.palle.id)
如果位置[2]>=拨杆位置[0]和位置[0]=拨杆位置[1]和位置[3]更换:
canvas.itemconfig(label, text="Score: "+str(ball.score))
与:
在重新启动
方法中
我是如何调试它的:
复制并运行整个代码,尝试按钮,看到分数静态为0
在脚本中搜索“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))