from tkinter import *
import time
import random
class Ball():
def __init__(self, canvas, platform, color):
self.canvas = canvas
self.platform = platform
# Создаем шарик
self.oval = canvas.create_oval(10, 10, 25, 25, fill=color)
self.canvas.move(self.oval, 245, 100)
self.dir = [-3, -2, -1, 1, 2, 3]
self.x = random.choice(self.dir)
self.y = -3
self.touch_bottom = False
def touch_platform(self, ball_pos):
platform_pos = self.canvas.coords(self.platform.rect)
if ball_pos[2] >= platform_pos[0] and ball_pos[0] <= platform_pos[2]:
if ball_pos[3] >= platform_pos[1] and ball_pos[3] <= platform_pos[3]:
return True
return False
def draw(self):
self.canvas.move(self.oval, self.x, self.y)
pos = self.canvas.coords(self.oval)
if pos[1] <= 0:
self.y = 3
if pos[3] >= 400:
self.touch_bottom = True
if self.touch_platform(pos) == True:
self.y = -3
if pos[0] <= 0:
self.x = 3
if pos[2] >= 500:
self.x = -3
class Platform():
def __init__(self, canvas, color):
self.canvas = canvas
self.rect = canvas.create_rectangle(0, 0, 100, 10, fill=color)
self.canvas.move(self.rect, 200, 300)
self.x = 0
self.canvas.bind_all('<KeyPress-Left>', self.left)
self.canvas.bind_all('<KeyPress-Right>', self.right)
def left(self, event):
self.x = -2
def right(self, event):
self.x = 2
def draw(self):
self.canvas.move(self.rect, self.x, 0)
pos = self.canvas.coords(self.rect)
if pos[0] <= 0:
self.x = 0
elif pos[2] >= 500:
self.x = 0
# --- Настройка окна и запуск игры ---
window = Tk()
window.title("Аркада")
window.resizable(0, 0)
window.wm_attributes("-topmost", 1)
canvas = Canvas(window, width=500, height=400, bd=0, highlightthickness=0)
canvas.pack()
window.update()
platform = Platform(canvas, 'green')
ball = Ball(canvas, platform, 'red')
# Игровой цикл
while True:
if ball.touch_bottom == False:
ball.draw()
platform.draw()
else:
# Если проиграли, можно добавить текст "Game Over"
break
window.update_idletasks()
window.update()
time.sleep(0.01)
window.mainloop()