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

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

root = Tk()
fr = Frame(root)
root.geometry('800x600')
bt = Button(root,width= 8, text = 'new')
bt.pack()

image = Image.open("3.jpg")
photo = ImageTk.PhotoImage(image)

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
        self.id = canv.create_oval(x - r, y - r, x + r, y + r, fill = 'blue', width=5)
        
    def shot(self,target):
        tx = target.x
        ty = target.y
        s = canv.create_line(self.x,self.y,tx,ty,fill = 'orange', width = 4)
        canv.update()
        time.sleep(0.04)
        canv.delete(target.id)
        canv.delete(s)
        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.v = 15
        self.angle = 0
        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 move(self,x,y):
        self.angle = math.atan2((self.y-y),(self.x-x))
        self.x -= self.v*(math.cos(self.angle))
        self.y -= self.v*(math.sin(self.angle))
        canv.coords(self.id,self.x-self.r,self.y -self.r, self.x + self.r, self.y + self.r)
        
colors = ['red','blue','green']

def new(event=0):
    global spiders, gamer
    spiders = []
    canv.delete(ALL)
    for xx in range(0,800,144):
        for yy in range(0,600,144):
            canv.create_image(xx,yy, anchor=NW, image=photo)
    class t():
        pass
    t.x = 100
    t.y = 100
    add_spiders(t)

    t.x = 400
    t.y = 100
    add_spiders(t)

    t.x = 700
    t.y = 100
    add_spiders(t)

    t.x = 700
    t.y = 500
    add_spiders(t)

    t.x = 700
    t.y = 100
    add_spiders(t)
    
    gamer = new_gamer(400,300)
sleep = 0.04
def task(event):
    live = 1
    while live:
        if spiders:
            target = spiders[0]
            td = math.sqrt(((target.x-gamer.x)**2+(target.y-gamer.y)**2))
            for spider in spiders:
                spider.move(gamer.x,gamer.y)
                d = math.sqrt((spider.x-gamer.x)**2+(spider.y-gamer.y)**2)
                if d < (gamer.r + target.r):
                    live = 0
                if td > d:
                    target = spider
                    td = d
            if live:
                gamer.shot(target)
                spiders.pop(spiders.index(target))
        canv.update()
        time.sleep(sleep)

def add_spiders(event):
    global spiders
    r = 100
    for z in range(5):
        spiders += [new_spider(rnd(event.x-r,event.x+r),rnd(event.y-r,event.y),15, choice(colors))]
    
            
bt.bind('<Button-1>',new)
canv.bind('<Button-3>',task)
canv.bind('<Button-1>',add_spiders)

new()

mainloop()

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

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