Заготовка для игры о шарах-мутантах

Заготовка для игры о шарах-мутантах
from random import randrange as rnd, choice
from tkinter import *
import time, math

root = Tk()
fr = Frame(root)
root.geometry('800x600')
bt = Button(root,width= 8, text = 'new')
bt.pack()
canv = Canvas(root, bg = 'white')
canv.pack(fill=BOTH,expand=1)

class new_gamer():
    def __init__(self,x = 400, y = 300):
        self.x = x
        self.y = y
        r = self.r = 20
        d = 10
        k = 0
        while canv.find_overlapping(x - r, y - r, x + r, y + r):
            x = rnd(x-d,x+d)
            y = rnd(x-d,x+d)
            k += 1
            if k == 20:
                k = 0
                d += 10
            #print (k,x,y,d)
        self.x = x
        self.y = y
        canv.create_oval(x - r, y - r, x + r, y + r, fill = 'blue', width=5)
        
    def shot(self,tx,ty):
        s = canv.create_line(self.x,self.y,tx,ty,fill = 'orange', width = 4)
        canv.update()
        time.sleep(0.04)
        canv.delete(s)
        
        canv.delete(canv.find_overlapping(tx-5,ty-5,tx+2,ty+5)[:1])
        canv.update()
        

class new_spider():
    def __init__(self, x = 0, y = 0, r = 15, color = 'red'):
        self.x = x
        self.y = y
        self.r = r
        self.color = color
        self.id = canv.create_oval(self.x-self.r,self.y -self.r, self.x + self.r, self.y + self.r, fill = color)
        self.mode = 'on'

def new(event=0):
    global spiders
    spiders = []
    canv.delete(ALL)
    colors = ['red','blue','green']
    for z in range(rnd(10,30)):
        spiders += [new_spider(rnd(50,700),rnd(50,500))]
        
    global gamer
    gamer = new_gamer()

def task(event):
    #gamer.shot(event.x,event.y)
    target = spiders[0]
    for spider in spiders:
        if math.sqrt(((target.x-gamer.x)**2+(target.y-gamer.y)**2)) > math.sqrt((spider.x-gamer.x)**2+(spider.y-gamer.y)**2):
            target = spider
            
        
    gamer.shot(target.x,target.y)
    spiders.pop(spiders.index(target))
    

            
bt.bind('<Button-1>',new)
canv.bind('<Button-1>',task)

new()

mainloop()

Добавить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *