Заготовка для игры о шарах-мутантах 4 (последняя)

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

Теперь два «игрока», два попадания для уничтожения.

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.live = 100
        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)
        target.hit()
        canv.delete(s)
        canv.update()
    def hit(self):
        global gamers
        self.live -= 50
        if self.live <= 0:
            gamers.pop(gamers.index(self))
            canv.itemconfig(self.id, fill = 'red')
        
        
    def find_target(self,targets):
        spiders = targets
        if spiders:
            target = spiders[0]
            td = math.sqrt(((target.x-self.x)**2+(target.y-self.y)**2))
            for spider in spiders:
                d = math.sqrt((spider.x-self.x)**2+(spider.y-self.y)**2)
                if d < (self.r + target.r):
                    self.hit()
                if td > d:
                    target = spider
                    td = d
            return target
        
        

class new_spider():
    def __init__(self, x = 0, y = 0, r = 20, color = 'red'):
        self.x = x
        self.y = y
        self.r = r
        self.v = 8
        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 hit(self):
        global spiders
        if self.r > 10:
            canv.delete(self.id)
            spiders.pop(spiders.index(self))
            x = int(self.x)
            y = int(self.y)
            r = 5
            spiders += [new_spider(x-3*r,y-3*r,r, 'gray')]
            spiders += [new_spider(x+3*r,y+3*r,r, 'gray')]
            for sp in spiders[-4:]:
                sp.v = 1
            
        else:
            canv.delete(self.id)
            spiders.pop(spiders.index(self))

    def find_target(self,targets):
        spiders = targets
        if spiders:
            target = spiders[0]
            td = math.sqrt(((target.x-self.x)**2+(target.y-self.y)**2))
            for spider in spiders:
                d = math.sqrt((spider.x-self.x)**2+(spider.y-self.y)**2)
                if d < (self.r + target.r):
                    self.live = 0
                if td > d:
                    target = spider
                    td = d
            return target
            
    def move(self,x=0,y=0):
        if not x and not y:
            target = self.find_target(gamers)
            if target:
                x,y = target.x,target.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, gamers
    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 = 100
    t.y = 500
    add_spiders(t)
    
    gamers = [new_gamer(600,300),new_gamer(200,300)]
    
sleep = 0.04
def task(event):
    global gamers
    while gamers and spiders:
        for spider in spiders:
            spider.move()
        for gamer in gamers:
            target = gamer.find_target(spiders)
            if target:
                gamer.shot(target)
        canv.update()
        time.sleep(sleep)

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

new()

mainloop()

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

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