Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import Canvas, Tk
- from random import *
- root = Tk()
- canvas_w = 800# ширина холста
- canvas_h = 400# высота холста
- c = Canvas(width = canvas_w, height = canvas_h, bg = 'deepskyblue')
- c.create_rectangle(0, canvas_h // 2, canvas_w, canvas_h, fill = 'light green', width = 0)
- c.pack()
- #текстовые поля
- score = 0
- score_text = c.create_text(10, 10, anchor = 'nw', font = 'Verdana 18', fill = 'green', text = 'Счет: '+str(score))
- lives = 5
- lives_text = c.create_text(canvas_w - 10, 0, anchor = 'ne', font = 'Verdana 18', fill = 'green', text = 'Жизни: '+str(lives))
- #создали корзину
- #catch = c.create_rectangle(canvas_w//2 - 40, canvas_h - 100, canvas_w//2+40, canvas_h - 80, fill = 'orange', width = 2)
- catch = c.create_arc(canvas_w//2 - 40, canvas_h - 100, canvas_w//2+40, canvas_h - 60, start = 200, extent = 140, fill = 'yellow', outline = 'yellow', width = 3, style = 'chord')
- #движение корзины
- def move_left(event):
- (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
- if catch_x > 0:
- c.move(catch, -20, 0)
- def move_right(event):
- (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
- if catch_x2 < 800:
- c.move(catch, 20, 0)
- def move_up(event):
- (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
- if catch_y > canvas_h // 2 - 20:
- c.move(catch, 0, -20)
- def move_down(event):
- (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
- if catch_y2 < canvas_h:
- c.move(catch, 0, 20)
- c.bind('<Left>', move_left)
- c.bind('<Right>', move_right)
- c.bind('<Up>', move_up)
- c.bind('<Down>', move_down)
- c.focus_set()
- #создаем яйцо
- egg_h = 30
- egg_w = 20
- speed = 500 # как часто смещается вниз
- egg_time = 3000 # как часто появляются
- eggs = []
- def create_egg():
- global egg_time
- x = randint(0, 800)
- y = 0
- R = '%02x'%randint(0,255)
- G = '%02x'%randint(0,255)
- B = '%02x'%randint(0,255)
- egg = c.create_oval(x, y, x + egg_w, y + egg_h, fill = '#'+R+G+B)
- eggs.append(egg)
- root.after(egg_time, create_egg)
- create_egg()
- def move_egg():
- global speed
- for egg in eggs:
- c.move(egg, 0, 10)
- (egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
- if egg_y2 > canvas_h:
- eggs.remove(egg)
- c.delete(egg)
- root.after(speed, move_egg)
- move_egg()
- def check_catch():
- global egg_time, speed
- for egg in eggs:
- (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
- (egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
- if catch_x < egg_x and egg_x2 < catch_x2 and egg_y2 > catch_y2 - 30:
- eggs.remove(egg)
- c.delete(egg)
- print('Поймали!')
- #увеличить частоту появления яиц и скорость падения
- egg_time = int(0.9 * egg_time)
- speed = int(0.9 * speed)
- root.after(100, check_catch)
- check_catch()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement