Advertisement
here2share

# Tk_bots_hungry_for_dots_2017v.py

Aug 2nd, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.83 KB | None | 0 0
  1. # Tk_bots_hungry_for_dots_2017v.py
  2. # game
  3.  
  4. from Tkinter import*
  5. import time
  6. import random
  7. import math
  8.  
  9. class PsType:
  10.  
  11.     def __init__(self, max_ps):
  12.         self.list = [0]*max_ps
  13.         self.l = 0
  14.         self.r = 0
  15.  
  16.     def first(self):
  17.         if self.l == self.r:
  18.             return time.time()-1
  19.         return self.list[self.l]
  20.  
  21.     def check(self, time):
  22.         while self.l != self.r and self.list[self.l] < time-1:
  23.             self.l = (self.l+1) % len(self.list)
  24.  
  25.     def add(self, time):
  26.         self.check(time)
  27.         self.list[self.r] = time
  28.         self.r = (self.r+1) % len(self.list)
  29.  
  30.     def size(self):
  31.         if self.l <= self.r:
  32.             return self.r-self.l
  33.         else:
  34.             return len(self.list)+self.r-self.l
  35.  
  36. class Entity:
  37.  
  38.     def __init__(self):
  39.         self.score = 0
  40.         self.add_score = 0
  41.  
  42.     def __iadd__(self, other):
  43.         self.add_score += other
  44.         return self
  45.  
  46.     def add(self):
  47.         cores = 1000
  48.  
  49.         max_add = int(self.score/cores)+1
  50.  
  51.         if self.add_score > max_add:
  52.             self.add_score -= max_add
  53.             self.score += max_add
  54.             self.size = 50+math.sqrt(self.score)
  55.         elif self.add_score == 1:
  56.             self.score += self.add_score
  57.             self.add_score = 0
  58.  
  59. class Player(Entity):
  60.  
  61.     def __init__(self, name):
  62.         self.score = 0
  63.         self.add_score = 0
  64.         self.name = name
  65.         self.size = 50
  66.         self.static_size = 50
  67.         self.score = 0
  68.         self.x = 0
  69.         self.y = 0
  70.         self.sp_x = 0
  71.         self.sp_y = 0
  72.  
  73.     def draw(self, canvas):
  74.         global w, h
  75.         canvas.create_oval(w//2-self.static_size, h//2-self.static_size,
  76.                            w//2+self.static_size, h//2+self.static_size, fill="#228B22", outline="#228B22")
  77.         canvas.create_text(w // 2, h // 2, offset=CENTER, text=self.name, font="Arial 14", justify=CENTER,
  78.                            fill="black")
  79.  
  80. class Other(Entity):
  81.  
  82.     def __init__(self, name, x, y):
  83.         self.score = 0
  84.         self.add_score = 0
  85.         self.name = name
  86.         self.size = 50
  87.         self.x = x
  88.         self.y = y
  89.         self.sp_x = 0
  90.         self.sp_y = 0
  91.         self.score = 0
  92.         self.static_size = 50
  93.  
  94.     def draw(self, canvas):
  95.         global w, h, player
  96.         koeff = player.static_size/player.size
  97.         l_w = w // 2+(self.x-player.x)
  98.         l_h = h // 2+(self.y-player.y)
  99.         if-self.size*koeff < l_w < w+self.size*koeff and-self.size*koeff < l_h < h+self.size*koeff:
  100.             canvas.create_oval((l_w-self.size*koeff, l_h-self.size*koeff),
  101.                                (l_w+self.size*koeff, l_h+self.size*koeff), fill="red", outline="red")
  102.             canvas.create_text(l_w, l_h, offset=CENTER, text=self.name, font="Arial 14", justify=CENTER,
  103.                                fill="black")
  104.             canvas.create_text(l_w, l_h+18, offset=CENTER, text=str(self.score), font="Arial 10", justify=CENTER,
  105.                                fill="black")
  106.  
  107.     def move(self):
  108.         if random.randint(0, 49) == 0:
  109.             x = random.randint(-100, 100)
  110.             y = random.randint(-100, 100)
  111.             r = math.sqrt(x**2+y**2)
  112.             if r == 0:
  113.                 self.sp_x = 0
  114.                 self.sp_y = 0
  115.             else:
  116.                 self.sp_x = x/r
  117.                 self.sp_y = y/r
  118.  
  119. class Food:
  120.  
  121.     def __init__(self, x, y, static_size):
  122.         self.x = x
  123.         self.y = y
  124.         self.static_size = static_size
  125.  
  126.     def draw(self, canvas):
  127.         global w, h, player
  128.         koeff = 1 #player.static_size/player.size
  129.         if abs(self.x-player.x)*koeff <= w and abs(self.y-player.y)*koeff <= h:
  130.             canvas.create_oval((w // 2+(self.x-player.x-self.static_size)*koeff,
  131.                                 h // 2+(self.y-player.y-self.static_size)*koeff),
  132.                                (w // 2+(self.x-player.x+self.static_size)*koeff,
  133.                                 h // 2+(self.y-player.y+self.static_size)*koeff),
  134.                                fill="yellow", outline="green")
  135.  
  136. def mouse_motion(event):
  137.     global w, h, player
  138.     l_x = event.x-w // 2
  139.     l_y = event.y-h // 2
  140.     if l_x**2+l_y**2 > player.static_size**2:
  141.         player.sp_x = l_x/math.sqrt(l_x**2+l_y**2)
  142.         player.sp_y = l_y/math.sqrt(l_x**2+l_y**2)
  143.     else:
  144.         player.sp_x = 0
  145.         player.sp_y = 0
  146.  
  147. def tick():
  148.     global ent, root, map_w, map_h, h, w, food, player, tps, speed, status, add_time_tps, local_tps
  149.     tick_time = 1000 // tps
  150.     t = time.time()
  151.  
  152.     if status == "game":
  153.  
  154.         if len(ent) == 1:
  155.             status = "menu"
  156.             root.after(add_time_tps, tick)
  157.             return
  158.  
  159.         for elem in ent:
  160.             elem.add()
  161.  
  162.             if elem != player:
  163.                 elem.move()
  164.             elem.x = max(0, min(map_w, elem.x+speed*elem.sp_x))
  165.             elem.y = max(0, min(map_w, elem.y+speed*elem.sp_y))
  166.  
  167.             tmp = set()
  168.             for f in food:
  169.                 if abs(elem.x-f.x)+abs(elem.y-f.y) <= 50+f.static_size:
  170.                     if (elem.x-f.x)**2+(elem.y-f.y)**2 <= (50+f.static_size)**2:
  171.                         tmp.add(f)
  172.                         elem += f.static_size
  173.             for i in range(len(tmp)):
  174.                 new_food(food)
  175.             food = food.difference(tmp)
  176.  
  177.         tmp_ent = list(ent)
  178.         for i in range(len(tmp_ent)-1):
  179.             for j in range(i+1, len(tmp_ent)):
  180.                 elem = tmp_ent[i]
  181.                 f = tmp_ent[j]
  182.                 if abs(elem.x-f.x)+abs(elem.y-f.y) <= max(elem.size, f.size):
  183.                     if (elem.x-f.x)**2+(elem.y-f.y)**2 <= (max(elem.size, f.size))**2:
  184.                         if elem.size > f.size:
  185.                             elem += f.score
  186.                             delete(f)
  187.                         elif elem.size < f.size:
  188.                             f += elem.score
  189.                             delete(elem)
  190.  
  191.     local_tps.add(time.time())
  192.  
  193.     dt = tick_time-(time.time()-t)*1000-1
  194.     add_time_tps = max(int(dt), 0)
  195.  
  196.     root.after(add_time_tps, tick)
  197.  
  198. def delete(e):
  199.     global ent, status, player
  200.     if e == player:
  201.         status = "menu"
  202.         return
  203.     tmp = set()
  204.     tmp.add(e)
  205.     ent = ent.difference(tmp)
  206.     new_ent(ent, rand_size=int(player.score*1.5), rand_left=int(player.score*0.5))
  207.  
  208. def drawer():
  209.     global canvas, ent, player, fps, add_time_tps, local_tps, local_fps, add_time_fps
  210.     tick_time = 1000 // fps
  211.     t = time.time()
  212.  
  213.     canvas.delete("all")
  214.  
  215.     if status == "menu":
  216.         draw_menu(player.score+player.add_score)
  217.  
  218.     elif status == "game":
  219.         draw()
  220.  
  221.     elif status == "pause":
  222.         draw()
  223.         draw_pause()
  224.  
  225.     local_fps.add(time.time())
  226.  
  227.     canvas.create_text(1, h-24, text=str(local_fps.size())+" FPS   "+str(int(add_time_fps))+" ms",
  228.                        font="Arial 10 bold", anchor="w", fill="green")
  229.     canvas.create_text(1, h-8, text=str(local_tps.size())+" TPS   "+str(int(add_time_tps))+" ms",
  230.                        font="Arial 10 bold", anchor="w", fill="green")
  231.     canvas.create_text(w, 10, text="Score: "+str(player.score), font="Arial 14 bold", anchor="e", fill="green")
  232.  
  233.     canvas.update()
  234.  
  235.     dt = tick_time-(time.time()-t)*1000-1
  236.     add_time_fps  = max(int(dt), 0)
  237.  
  238.     root.after(add_time_fps, drawer)
  239.  
  240. def key_press(event):
  241.     global canvas, status
  242.  
  243.     if status == "menu":
  244.         if event.keycode == 13:
  245.             new_game()
  246.             status = "game"
  247.         return
  248.  
  249.     if status == "pause":
  250.         if event.keycode == 13:
  251.             status = "game"
  252.         return
  253.  
  254.     if event.keycode == 80 or event.keycode == 27:
  255.         status = "pause"
  256.         draw_pause()
  257.  
  258. def length(x1, y1, x2, y2):
  259.     return math.sqrt((x1-x2)**2+(y1-y2)**2)
  260.  
  261. def draw_scene():
  262.     global h, w, canvas, player, map_h, map_w, scene
  263.     size = 100 #real_size*(player.static_size/player.size)
  264.    
  265.     if not scene:
  266.         for l_x in range(0, (map_w/size)):
  267.             for l_y in range(0, (map_h/size)):
  268.                 if (l_x+l_y) % 2 == 1:
  269.                     scene.append([l_x*size+(w/2), l_y*size+(h/2)])
  270.     for l_x,l_y in scene:
  271.         l_x -= player.x
  272.         l_y -= player.y
  273.         canvas.create_rectangle((l_x, l_y), (l_x+size, l_y+size), width=0, fill="#cccccc")
  274.  
  275. def draw_ent():
  276.     global ent, canvas
  277.     for elem in ent:
  278.         elem.draw(canvas)
  279.  
  280. def draw_food():
  281.     global food, canvas
  282.     for elem in food:
  283.         elem.draw(canvas)
  284.  
  285. def draw():
  286.     draw_scene()
  287.     draw_food()
  288.     draw_ent()
  289.     draw_scores()
  290.  
  291. def size(obj):
  292.     return obj.size
  293.  
  294. def draw_scores():
  295.     global canvas, w, h, ent, status
  296.     tmp_ent = list(ent)
  297.     tmp_ent.sort(key=size)
  298.     i = 0
  299.     canvas.create_text(1, 10, text="Scores:", font="Arial 14", anchor="w", fill="Blue")
  300.     while i < min(10, len(tmp_ent)):
  301.         canvas.create_text(1, 30+i*20, text=tmp_ent[-1-i].name+": "+str(tmp_ent[-1-i].score), font="Arial 14",
  302.              anchor="w", fill="Blue")
  303.         i += 1
  304.     if time.time()-started < 120:
  305.         canvas.create_text(w // 2, h // 2-100, text=str(120-int((time.time()-started)*100)/100), font="Arial 50", fill="orange")
  306.     else:
  307.         status = "menu"
  308.  
  309. def draw_pause():
  310.     global canvas, w, h
  311.     canvas.create_text((w // 2)-200, h // 2, text="Press enter to continue", font="Arial 14", anchor="w",
  312.                        justify=LEFT, fill="black")
  313.  
  314. def draw_menu(score=0):
  315.     global canvas, w, h, started
  316.     canvas.create_text(w // 2, h // 2, text="Press enter to start", font="Arial 14", fill="black")
  317.     if score != 0:
  318.         if type(started) != type(''):
  319.             started = str(int((time.time()-started)*100)/100)+' seconds'
  320.         canvas.create_text(w // 2, h // 2-100, text="GAME OVER", font="Arial 30", fill="orange")
  321.  
  322. def new_food(food):
  323.     a = 5
  324.     b = 30
  325.     tmp = Food(random.randint(0, map_w), random.randint(0, map_h), random.randint(a, b))
  326.     food.add(tmp)
  327.  
  328. def new_ent(ent, rand_size=0, tries=0, rand_left=0):
  329.     global names_of_bots, map_w, map_h
  330.     if tries > 1:
  331.         return
  332.     tmp = Other(str(names_of_bots), random.randint(0, map_w), random.randint(0, map_h))
  333.     for elem in ent:
  334.         if abs(elem.x-tmp.x)+abs(elem.y-tmp.y) <= elem.size*2:
  335.             if (elem.x-tmp.x)**2+(elem.y-tmp.y)**2 <= (elem.size*2)**2:
  336.                 return new_ent(ent, rand_size=rand_size, tries=tries+1, rand_left=rand_left)
  337.     tmp.score = random.randint(rand_left, rand_size)
  338.     tmp.size = 50+math.sqrt(tmp.score)
  339.     ent.add(tmp)
  340.     names_of_bots += 1
  341.  
  342. def new_game():
  343.     global ent, food, names_of_bots, status, map_w, map_h, player, started
  344.     ent = set()
  345.     food = set()
  346.     status = "menu"
  347.     player = Player("player")
  348.     player.x = map_w // 2+0.01
  349.     player.y = map_h // 2+0.01
  350.     ent.add(player)
  351.     started = time.time()
  352.     for i in range(num_of_food):
  353.         new_food(food)
  354.     names_of_bots = 1
  355.     for i in range(num_of_bots):
  356.         new_ent(ent, rand_size=250)
  357.  
  358. w = 1000
  359. h = 500
  360. map_w = 2000
  361. map_h = 2000
  362. num_of_food = 100
  363. num_of_bots = 20
  364. started = 0
  365. scene = []
  366.  
  367. new_game()
  368.  
  369. fps = 30
  370. tps = 120
  371. speed = 6
  372.  
  373. local_fps = PsType(fps*2)
  374. local_tps = PsType(tps*2)
  375. add_time_fps = 1000 // fps
  376. add_time_tps = 1000 // tps
  377.  
  378. root = Tk()
  379. root.bind("<KeyPress>", key_press)
  380. root.bind("<Motion>", mouse_motion)
  381. root.geometry('%dx%d+10+10'%(w,h))
  382. canvas = Canvas(root, width=w, height=h)
  383. canvas.place(x=0, y=0)
  384.  
  385. root.after(100, tick)
  386. root.after(100, drawer)
  387.  
  388. root.mainloop()
  389. status = "end"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement