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

2014-07-08 00-13-01 Скриншот экрана
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,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.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),15, choice(colors))]
        
    global gamer
    gamer = new_gamer()

def task(event):
    #gamer.shot(event.x,event.y)
    red_spiders = [x for x in spiders if x.color == 'red']
    if red_spiders:
        target = red_spiders[0]
        for spider in red_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)
        spiders.pop(spiders.index(target))
    

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

new()

mainloop()

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

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