Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from math import *
- from time import time
- from random import *
- ##############################
- #Defining variables
- #~Nenyúljhozzá részleg
- timeborder=0.02
- r=3
- cr=40
- br=5
- d=br
- x, y = 350, 350
- change=90.0
- color='red'
- pressing=False
- first=True
- direction=0
- shoots=[]
- #*****************************************
- #~Hozzányúlhatsz részleg
- db=30 # töltények száma
- plus=4 # lőtorony forgási sebesség
- kulso=500 # radius a lézernek
- enenum=10 # enemyk száma
- enespeed=0.2 # enemyk sebessége
- startdist=400 # első enemy milyen korán jön
- between=40 # enemyk közötti táv
- ##############################
- #Defining good functions
- def cd(which, i):
- if not which: #tehát x coord
- return x+cos(radians(360*(i/db) -change))*cr
- else: #tehát y coord
- return y+sin(radians(360*(i/db) -change))*cr
- def cdp(which, i, rad):
- if not which: #tehát x coord
- return x+cos(radians(360*(i/db) -change))*rad
- else: #tehát y coord
- return y+sin(radians(360*(i/db) -change))*rad
- #~~~~~~~~~~~~~~~~~~~~~~~~~~
- def rotate(dir):
- global pressing, direction
- direction=dir
- pressing=True
- def stop_rot():
- global pressing
- pressing=False
- #~~~~~~~~~~~~~~~~~~~~~~~~~~
- def fire():
- global shoots, db, point, alive, enedied, lab
- if db==2 or not alive:
- alive=False
- return 0
- shoots.append([rajz.create_line(cdp(0, 0, cr+d), cdp(1, 0, cr+d), cdp(0, 0, kulso), cdp(1, 0, kulso), fill='#ff0000', width=br), time()])
- for enemy in enearr:
- diff=radians(change*-1) - enemy[1]
- if abs(sin(diff)* enemy[2])< min and enemy[2]<=hypot(350, 350)+min and cos(diff)>0:
- enemy[3]=0
- for pt in point:
- rajz.delete(pt)
- db-=1
- point=[rajz.create_oval(cd(0, i)-r, cd(1, i)-r, cd(0, i)+r, cd(1, i)+r, width=r) for i in range(db)]
- rad=br
- rajz.coords(point[0], cd(0, 0)-rad, cd(1, 0)-rad, cd(0, 0)+rad, cd(1, 0)+rad)
- rajz.itemconfig(point[0], outline=color, width=br)
- #~~~~~~~~~~~~~~~~~~~~~~~~~~
- def randdeg():
- return uniform(0, 2*pi)
- def help(i):
- global min, max
- min=20
- max=40
- r=radians(uniform(0, 90)+i*90)
- d=uniform(min, max)
- #alseg.append([r, d])
- enedeg=deg[ind]
- if first:
- enedist=dist[ind]
- else:
- enedist=enearr[ind][2]
- kx, ky= x+cos(enedeg)*enedist, y+sin(enedeg)* enedist
- a, b = cos(r)*d+kx, sin(r)*d+ky
- return [a, b]
- def getshape(index):
- global ind
- ind=index
- return [help(0), help(1), help(2), help(3)]
- """hatar1=2
- hatar2=2
- def mozgat():
- global alak
- for i in range(4):
- r=alseg[i][0]+uniform(-hatar1, hatar1)
- d=alseg[i][1]+uniform(-hatar2, hatar2)
- if r>90+i*90:
- r=90+i*90
- elif r<i*90:
- r=i*90
- if d < min:
- d=min
- elif d > max:
- d=max
- a, b = cos(r)*d+x, sin(r)*d+y
- alak[i][0]=a
- alak[i][1]=b"""
- ##############################
- #Setting program
- ablak=Tk()
- rajz=Canvas(width=700, height=700, bg='white')
- rajz.grid(columnspan=2)
- point=[rajz.create_oval(cd(0, i)-r, cd(1, i)-r, cd(0, i)+r, cd(1, i)+r, width=r) for i in range(db)]
- rajz.coords(point[0], x-br, y-cr-br, x+br, y-cr+br)
- rajz.itemconfig(point[0], outline=color, width=br)
- centralline=rajz.create_line(350, 350, 350, 350-cr, fill='red', width=4)
- dir=["Balra", "Jobbra"]
- for i in range(2):
- button = Button(text=dir[i], width=14, height=3)
- button.grid(column=i, row=1)
- button.bind('<ButtonPress-1>', lambda event, i=i: rotate(i))
- button.bind('<ButtonRelease-1>', lambda event: stop_rot())
- firebut=Button(text='FIRE!!!!', command=fire, width=20, height=3)
- firebut.grid(row=2, columnspan=2)
- deg=[randdeg() for i in range(enenum)]
- dist=[startdist+i*between for i in range(enenum)]
- enearr=[[rajz.create_polygon(getshape(i), fill='white', outline='black'), deg[i],dist[i], 1] for i in range(enenum)]
- first=False
- alive=True
- rogzit=time()
- lab=Label()
- lab.grid(columnspan=2)
- #~~~~~~~~~Main loop~~~~~~~~~~~
- """
- STATISZTIKA:
- Alap várási idő: 0.02 sec
- Iterációk 0.025 sec feletti idővel: 45%
- Iterációk 0.03 sec feletti idővel: 14%
- Iterációk 0.035 sec feletti idővel: 5%
- Ebbe beleszámítandó az iterációnkénti
- időellenőrzés
- """
- while alive:
- while time()-rogzit < timeborder:
- pass
- rogzit=time()
- ablak.update()
- if len(shoots)!=0:
- for sh in shoots:
- kul=time()-sh[1]
- if kul <= 1:
- ertek=str(hex(int(255*kul)))[2:]
- if len(ertek)==1:
- ertek='0'+ertek
- rajz.itemconfig(sh[0], fill='#ff'+ertek+ertek)
- else:
- rajz.delete(sh[0])
- shoots.remove(sh)
- if pressing:
- if direction:
- plus=abs(plus)*-1
- else:
- plus=abs(plus)
- change+=plus
- for i in range(db):
- if i==0: rad=br
- else: rad=r
- rajz.coords(point[i], cd(0, i)-rad, cd(1, i)-rad, cd(0, i)+rad, cd(1, i)+rad)
- rajz.coords(centralline, 350, 350, cd(0, 0), cd(1, 0))
- h=0
- for enemy in enearr:
- if enemy[2]<cr:
- alive=False
- break
- elif enemy[3]==1:
- enemy[2] -= enespeed
- rajz.delete(enemy[0])
- ind=h
- alak=[help(0), help(1), help(2), help(3)]
- enemy[0]=rajz.create_polygon(alak, fill='white', outline='black')
- else:
- #animation(enemy)
- pass
- h+=1
- if not alive:
- break
- rajz.delete(ALL)
- rajz.create_text(350, 350, text='GAME OVER')
- mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement