Advertisement
Benkex

Game 5.0

Oct 27th, 2019
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.56 KB | None | 0 0
  1. from tkinter import *
  2. from math import *
  3. from time import time
  4. from random import *
  5. from keyboard import is_pressed
  6.  
  7. ##############################
  8. #Defining too many variables
  9.  
  10. #!!!!!!!!!!!!!---Nenyúljhozzá részleg---!!!!!!!!!!!!!!!
  11.  
  12. #~~~~~~~~~~~Setup~~~~~~~~~~~
  13. timeborder=0.04
  14.  
  15. width = 1500
  16. height = 800
  17. x, y = width/2, height/2
  18. change=90.0
  19. point=[]
  20.  
  21. pressing=False
  22. first=True
  23. firewhenrelease=False
  24. shootnow=0
  25. direction=0
  26. shoots=[]
  27. getpts=[]
  28.  
  29. #~~~~~~~~~~Design~~~~~~~~~~~
  30. r=1
  31. cr=20
  32. scr=10
  33. br=2
  34. d=br
  35. kulso=5000
  36.  
  37. gosmaller=90
  38. gosmperiod=35
  39. delay=60
  40. acc=250
  41.  
  42. reduce_shield=1
  43. rotate_shield=2
  44.  
  45. color='red'
  46. simplecolor='black'
  47. ammo_ene_color='#ffffff'
  48. def_ene_color='#00deff'
  49. outlinecolor='white'
  50.  
  51.  
  52. #*****************************************
  53. #@@@---Hozzányúlhatsz részleg---@@@
  54.  
  55. db=13# töltények száma
  56. plus=5.5# lőtorony forgási sebesség
  57.  
  58. enenum=40 # enemyk száma
  59. ammoene=10 # golyós enemyk száma
  60. defene=20 # pajzsos enemyk száma
  61. enespeed=30 # enemyk sebessége
  62. startdist=700 # első enemy milyen korán jön
  63. between=35# enemyk közötti táv
  64.  
  65. shield_r=40
  66. width_shield=3
  67.  
  68. #Ezekkel vicces játszani ;)
  69. min=70# enemyk mininum mérete
  70. max=80 # enemyk maximum mérete
  71.  
  72. ##############################
  73. eredb=db
  74. enespeed/=10
  75. ##############################
  76. #Defining messy functions
  77.  
  78. def cd(which, i):
  79.     if db<=15: vmi=db
  80.     else: vmi=15
  81.     if not which: #tehát x coord
  82.         return x+cos(radians(360*(i/vmi) -change))*cr
  83.     else: #tehát y coord
  84.         return y+sin(radians(360*(i/vmi) -change))*cr
  85.        
  86. def cdp(which, i, rad):
  87.     if db<=15: vmi=db
  88.    
  89.     else: vmi=db-15
  90.     if not shootnow: chan=change+360/(db-15)/2
  91.     else: chan=change
  92.     if not which: #tehát x coord
  93.         return x+cos(radians(360*(i/vmi) -chan))*rad
  94.     else: #tehát y coord
  95.         return y+sin(radians(360*(i/vmi) -chan))*rad
  96.        
  97. #~~~~~~~~~~~~~~~~~~~~~~~~~~
  98.  
  99. def rotate(dir):
  100.     global pressing, direction
  101.     direction=dir
  102.     pressing=True
  103.    
  104. def eneinside(xy):
  105.     if xy[0] >= 0-max and xy[0] <= width+max and xy[1] >= 0-max and xy[1] <= height+max:
  106.         return True
  107.     else: return False
  108.    
  109. #~~~~~~~~~~~~~~~~~~~~~~~~~~
  110.    
  111. def fire():
  112.     global shoots, alive, enedied, lab, shootnow
  113.     if db==1 or not alive:
  114.         return 0
  115.     shootnow=True
  116.     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()])
  117.     shootnow=False
  118.    
  119.     for enemy in enearr:
  120.         enexy=[x+cos(enemy[1]) *enemy[2], y+sin(enemy[1])*enemy[2]]
  121.         diff=radians(change*-1) - enemy[1]
  122.         if abs(sin(diff)*enemy[2])< (min+max)/2 and eneinside(enexy) and cos(diff)>0 and enemy[3]==1:
  123.             enemy[3]=0
  124.            
  125.     changepts(db-1)
  126.    
  127. #~~~~~~~~~~~~~~~~~~~~~~~~~~
  128.    
  129. def changepts(how):
  130.     global point, db
  131.     for pt in point:
  132.         rajz.delete(pt)
  133.     db=how
  134.     if db<=15:
  135.         point=[rajz.create_oval(cd(0, i)-r, cd(1, i)-r, cd(0, i)+r, cd(1, i)+r, width=r*2, outline=outlinecolor) for i in range(db)]
  136.     else:
  137.         point=[rajz.create_oval(cd(0, i)-r, cd(1, i)-r, cd(0, i)+r, cd(1, i)+r, width=r*2, outline=outlinecolor) for i in range(15)]
  138.         point+=[rajz.create_oval(cdp(0, i, scr)-r, cdp(1, i, scr)-r, cdp(0, i, scr)+r*2, cdp(1, i, scr)+r, width=r*2, outline=outlinecolor) for i in range(db-15)]
  139.            
  140.     rad=br
  141.     rajz.coords(point[0], cd(0, 0)-rad, cd(1, 0)-rad, cd(0, 0)+rad, cd(1, 0)+rad)
  142.     rajz.itemconfig(point[0], outline=color, width=br*2)
  143.    
  144. #~~~~~~~~~~~~~~~~~~~~~~~~~~
  145.    
  146. def randdeg():
  147.     return uniform(0, 2*pi)
  148.  
  149. def help(i):
  150.     global min, max
  151.     r=radians(uniform(0, 90)+i*90)
  152.     d=uniform(min, max)
  153.     #alseg.append([r, d])
  154.     enedeg=deg[ind]
  155.     if first:
  156.         enedist=dist[ind]
  157.     else:
  158.         enedist=enearr[ind][2]
  159.     kx, ky= x+cos(enedeg)*enedist, y+sin(enedeg)* enedist
  160.     a, b = cos(r)*d+kx, sin(r)*d+ky
  161.     return [a, b]
  162.    
  163. def getshape(index):
  164.     global ind
  165.     ind=index
  166.     return help(0)+help(1)+help(2)+help(3)
  167.    
  168. #~~~~~~~~~~~~~~~~~~~~~~~~~~
  169.  
  170. def calcDeg(start, Ln):
  171.     def easy(x):
  172.         if x >= 0:
  173.             return 1
  174.         elif x < 0:
  175.             return -1
  176.     """def correct(x):
  177.        if x<0:
  178.            return 360+x
  179.        else:
  180.            return x"""
  181.     try:
  182.         c = (Ln[2]-Ln[0])/hypot(Ln[1]-Ln[3], Ln[0]-Ln[2])
  183.         s = (Ln[3]-Ln[1])/hypot(Ln[1]-Ln[3], Ln[0]-Ln[2])
  184.     except:
  185.         c, s = 0, 0
  186.     recovery=acos(c)*easy(s)-radians(start)
  187.     dgc=acos(cos(recovery))
  188.     elojel=sin(recovery)
  189.     return dgc*easy(elojel)
  190.  
  191. #~~~~~~~~~~~~~~~~~~~~~~~~~~
  192.  
  193. def drawshield(arr):
  194.     size, rot = arr
  195.     rajz.itemconfig(shield[0], start=rot, extent=size)
  196.    
  197. #~~~~~~~~~~~~~~~~~~~~~~~~~~
  198.  
  199. def animation():
  200.     global shield
  201.     for enemy in enearr:
  202.         if enemy[3]<1 and len(rajz.coords(enemy[0]))>0:
  203.             enexy=[x+cos(enemy[1])*enemy[2], y+sin(enemy[1])*enemy[2]]
  204.             if (enemy[4]==simplecolor and enemy[3] > -gosmperiod) or (enemy[4]==ammo_ene_color and enemy[3] > -gosmperiod/2):
  205.                 eneshape= [[rajz.coords(enemy[0])[2*j+i] for i in range(2)] for j in range(4)]
  206.                 enedegs=[calcDeg(0, enexy+eneshape[i]) for i in range(4)]
  207.                 eneptdists=[hypot(enexy[0]- eneshape[i][0], enexy[1]-eneshape[i][1])* gosmaller/100 for i in range(4)]
  208.                 def shape(i):
  209.                     return [enexy[0]+ cos(enedegs[i])* eneptdists[i], enexy[1]+ sin(enedegs[i])*eneptdists[i]]
  210.                 newshape=[]
  211.                 for i in range(4):
  212.                     newshape+=shape(i)
  213.                 rajz.coords(enemy[0], newshape)
  214.                 enemy[3]-=1
  215.             else:
  216.                 rajz.coords(enemy[0], enexy+enexy+enexy+enexy)
  217.                 enemy[3]=2
  218.                 if enemy[4]==ammo_ene_color:
  219.                     for i in range(randint(2, 5)):
  220.                         getpts.append([rajz.create_oval(enexy[0]-r, enexy[1]-r, enexy[0]+r, enexy[1]+r, width=r*2, outline=outlinecolor), -i*delay, time(), enemy[1]+pi, rajz.coords(enemy[0])[:2]])
  221.                 elif enemy[4]==def_ene_color:
  222.                     if shield[1] < 359:
  223.                         shield[1] = 359
  224.        
  225.         rem=[]
  226.         for pt in getpts:
  227.             t=pow(time()-pt[2], 2)
  228.             npx, npy = pt[4][0]+ cos(pt[3])*acc*t, pt[4][1]+sin(pt[3])*acc*t
  229.             pdist=hypot(x-npx, y-npy)
  230.             if pt[1]<0:
  231.                 pt[1]+=1
  232.                 pt[2]=time()
  233.             elif pdist > cr:
  234.                 rajz.coords(pt[0], npx-r, npy-r, npx+r, npy+r)
  235.             else:
  236.                 changepts(db+1)
  237.                 rem.append(pt)
  238.                 rajz.delete(pt[0])
  239.         for item in rem:
  240.             getpts.remove(item)
  241.            
  242.     for sh in shoots:
  243.         kul=time()-sh[1]
  244.         if kul <= 1:
  245.             ertek=str(hex(int(255- 255*kul)))[2:]
  246.             if len(ertek)==1:
  247.                 ertek='0'+ertek
  248.             #rajz.itemconfig(sh[0], fill='#ff'+ertek+ertek)
  249.             rajz.itemconfig(sh[0], fill='#'+ertek+'0000')
  250.         else:
  251.             rajz.delete(sh[0])
  252.             shoots.remove(sh)
  253.  
  254.     if shield[1]>0:
  255.         shield[1]-=reduce_shield
  256.         shield[2]+=rotate_shield
  257.         rajz.itemconfig(shield[0], drawshield(shield[1:3]))
  258.  
  259. #~~~~~~~~~~~~~~~~~~~~~~~~~~
  260. #For test purposes
  261.  
  262. def stop(arg):
  263.     f=open('sth.txt', 'w')
  264.     f.write(str(arg))
  265.     f.close()
  266.     while 1:
  267.         pass
  268.        
  269. #~~~~~~~~~~~~~~~~~~~~~~~~~~
  270.        
  271. def setnewgame():
  272.     global centralline, deg, dist, enearr, first, shield
  273.     first=True
  274.     changepts(eredb)
  275.  
  276.     centralline=rajz.create_line(x, y, x, y-cr, fill='red', width=br-1)
  277.  
  278.     deg=[randdeg() for i in range(enenum)]
  279.     dist=[startdist+i*between for i in range(enenum)]
  280.     shuffle(dist)
  281.     enearr=[[rajz.create_polygon(getshape(i), fill=simplecolor, outline=outlinecolor), deg[i],dist[i], 1, simplecolor] for i in range(enenum)]
  282.     for i in range(ammoene):
  283.         enearr[i][4]=ammo_ene_color
  284.         rajz.itemconfig(enearr[i][0], fill=ammo_ene_color)
  285.     for i in range(ammoene, ammoene+defene):
  286.         enearr[i][4]=def_ene_color
  287.         rajz.itemconfig(enearr[i][0], fill=def_ene_color)
  288.     first=False
  289.    
  290.     shield = [rajz.create_arc(x-shield_r, y-shield_r, x+shield_r, y+shield_r, extent=0, width=width_shield, outline=def_ene_color, style='arc'), 0, 0]
  291.  
  292. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  293.  
  294. def control():
  295.     global change, firewhenrelease, alive
  296.     if not alive:
  297.         return 0
  298.     if is_pressed('space'):
  299.         firewhenrelease = True
  300.     if not is_pressed('space') and firewhenrelease:
  301.         fire()
  302.         firewhenrelease=False
  303.     if is_pressed('left_arrow'):
  304.         change+=abs(plus)
  305.         changepts(db)
  306.         rajz.coords(centralline, x, y, cd(0, 0), cd(1, 0))
  307.     if is_pressed('right_arrow'):
  308.         change+=abs(plus)*-1
  309.         changepts(db)
  310.         rajz.coords(centralline, x, y, cd(0, 0), cd(1, 0))
  311.     if is_pressed('n'):
  312.         rajz.delete(ALL)
  313.         change=90.0
  314.         setnewgame()
  315.         alive=True
  316.        
  317. """hatar1=2
  318. hatar2=2
  319.  
  320. def mozgat():
  321.    global alak
  322.    for i in range(4):
  323.        r=alseg[i][0]+uniform(-hatar1, hatar1)
  324.        d=alseg[i][1]+uniform(-hatar2, hatar2)
  325.        if r>90+i*90:
  326.            r=90+i*90
  327.        elif r<i*90:
  328.            r=i*90
  329.        if d < min:
  330.            d=min
  331.        elif d > max:
  332.            d=max
  333.        a, b = cos(r)*d+x, sin(r)*d+y
  334.        alak[i][0]=a
  335.        alak[i][1]=b"""
  336.  
  337. ##############################
  338. #Getting program ready
  339.  
  340. ablak=Tk()
  341.  
  342. rajz=Canvas(width=width, height=height, bg=simplecolor)
  343. rajz.grid(columnspan=2)
  344.  
  345. alive=True
  346. rogzit=time()
  347.  
  348. setnewgame()
  349.  
  350. #~~~~~~~~~Main loop~~~~~~~~~~~
  351.  
  352. # I know it's kinda ugly but... it works xd
  353. # Actually now it looks better
  354. """
  355. RÉGI STATISZTIKA: csinálj újat!
  356. Alap várási idő: 0.02 sec
  357. Iterációk 0.025 sec feletti idővel: 45%
  358. Iterációk 0.03 sec feletti idővel: 14%
  359. Iterációk 0.035 sec feletti idővel: 5%
  360.  
  361. Ebbe beleszámítandó az iterációnkénti
  362. időellenőrzés
  363. """
  364. while 1:
  365.     while time()-rogzit < timeborder:
  366.         pass
  367.     rogzit=time()
  368.     control()
  369.     animation()
  370.     #ablak.update_idletasks()
  371.     ablak.update()
  372.     h=0
  373.     for enemy in enearr:
  374.         if enemy[2]<cr:
  375.             alive=False
  376.             break
  377.         elif enemy[3]==1:
  378.             enemy[2] -= enespeed
  379.             rajz.coords(enemy[0], getshape(h))
  380.         h+=1
  381.     if not alive:
  382.         rajz.delete(ALL)
  383.         rajz.create_text(x, y, text='GAME OVER', fill=outlinecolor)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement