我是编程新手。我从这本书开始,名为" Python for kids"。我想知道如何用鼠标左键重启游戏。我真的不知道如何编程。这本书对我学习python非常好,但我还需要一些额外的信息。
你们能帮助我吗?
这是我的代码:
from tkinter import *
import random
import time
class Bal:
def __init__(self, canvas, paddle, score, color):
self.canvas = canvas
self.paddle = paddle
self.score = score
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]:
self.score.hit()
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.started = False
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)
self.canvas.bind_all('<Button-1>', self.start_game)
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 start_game(self, evt):
self.started = True
def turn_right(self, evt):
self.x = 3
class score:
def __init__(self, canvas, color):
self.score = 0
self.canvas = canvas
self.id = canvas.create_text(450,20, text=self.score,font=("Times", 20), fill=color)
def hit(self):
self.score += 1
self.canvas.itemconfig(self.id, text=self.score)
tk = Tk()
tk.title("bouncespel made by Robel Tewolde")
tk.resizable(0,0)
tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0)
canvas.configure(background='black')
canvas.pack()
tk.update()
score = score(canvas, 'red')
paddle = paddle(canvas, 'White')
bal = Bal(canvas, paddle, score,'red')
spel_over = canvas.create_text(250, 200,font="Times", text='HET SPEL IS AFGELOPEN', state='hidden')
while 1:
if bal.hit_bottom == False and paddle.started == True:
bal.draw()
paddle.draw()
if bal.hit_bottom == True:
time.sleep(2)
canvas.itemconfig(spel_over, state='normal')
tk.update_idletasks()
tk.update()
time.sleep(0.01)
答案 0 :(得分:0)
您可能必须将所有变量重置为初始状态才能重新启动。 我添加了一个重启方法,首先停止球拍,摧毁球,重置比分并将球初始化到起始位置。还可以在类中找到相应的方法。
from tkinter import *
import random
import time
class Bal:
def __init__(self, canvas, paddle, score, color):
self.canvas = canvas
self.paddle = paddle
self.score = score
self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
self.canvas.move(self.id, 200, 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 restart(self, canvas, paddle, score, color):
self.__init__(canvas, paddle, score, color)
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]:
self.score.hit()
return True
return False
def destroy(self):
self.canvas.delete(self.id)
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.started = False
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)
self.canvas.bind_all('<Button-1>', self.start_game)
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 start_game(self, evt):
self.started = True
def stop(self):
self.started = False
def turn_right(self, evt):
self.x = 3
class score:
def __init__(self, canvas, color):
self.score = 0
self.canvas = canvas
self.id = canvas.create_text(450,20, text=self.score,font=("Times", 20), fill=color)
def hit(self):
self.score += 1
self.canvas.itemconfig(self.id, text=self.score)
def reset_score(self):
self.score = 0
tk = Tk()
tk.title("bouncespel made by Robel Tewolde")
tk.resizable(0,0)
tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0)
canvas.configure(background='black')
canvas.pack()
tk.update()
score = score(canvas, 'red')
paddle = paddle(canvas, 'White')
bal = Bal(canvas, paddle, score,'red')
def restart():
bal.destroy()
paddle.stop()
score.reset_score()
bal.restart(canvas, paddle, score, 'red')
spel_over = canvas.create_text(400, 200,font="Times", text='HET SPEL IS AFGELOPEN', state='hidden')
def quit(event):
print("Double Click, so let's stop")
import sys; sys.exit()
while 1:
if bal.hit_bottom == False and paddle.started == True:
bal.draw()
paddle.draw()
if bal.hit_bottom == True:
restart()
canvas.itemconfig(spel_over, state='normal')
tk.update_idletasks()
tk.update()
time.sleep(0.01)