Как исправить цикл, чтобы игра запускалась при нажатии мыши?
(В самом конце кода цикл, в котором, скорее всего и заключается ошибка)
Сам phyton не выдает никакой ошибки, просто при нажатии ничего не происходит:(
from tkinter import*
import random, time
from tkinter.messagebox import askyesno
import sys
tk = Tk()
tk.title('Приг - Скок')
tk.wm_attributes('-topmost', 1)
tk.resizable(0,0)
canvas = Canvas(tk,width = 900, height = 700, bd=0, highlightthickness = 0, bg = 'pink')
canvas.pack()
tk.update()
class Ball():
def __init__(self, canvas, paddle, color):
self.canvas = canvas
self.paddle = paddle
self.id = canvas.create_oval(20,20,45,45, 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_floor = False
def hit_paddle(self, pos):
paddle_pos = self.canvas.coords(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
else:
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_floor = 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(400, 500, 500, 510, fill = color)
self.canvas_width = self.canvas.winfo_width()
self.x = 0
self.starting = False
self.canvas.bind_all('<KeyPress-Right>', self.moving_forward)
self.canvas.bind_all('<KeyPress-Left>', self.moving_back)
self.canvas.bind_all('<Button-1>', self.start)
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 moving_forward(self, event):
self.x = 10
def moving_back(self, event):
self.x = -10
def start(self, event):
self.starting = True
colors = ['blue', 'red', 'purple', 'grey', 'white', 'orange', 'yellow']
rand = random.choice(colors)
while True:
paddle = Paddle(canvas, 'blue')
ball = Ball(canvas, paddle, rand)
if ball.hit_floor == False:
ball.draw()
paddle.draw()
paddle.x = 0
tk.update()
time.sleep(0.01)
if ball.hit_floor == True:
answer = askyesno(message = 'Игра окончена! Хотите продолжить?')
if answer == False:
sys.exit()
if answer == True:
canvas.delete('all')