Advertisement
here2share

# tk_war.py

Mar 7th, 2024
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.80 KB | None | 0 0
  1. # tk_war.py
  2.  
  3. import tkinter as tk
  4. import random
  5. import math
  6. from PIL import Image, ImageTk, ImageDraw
  7.  
  8. WINDOW_WIDTH = 500
  9. WINDOW_HEIGHT = 500
  10. CX, CY = WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2
  11. ROCK_COUNT = PAPER_COUNT = SCISSORS_COUNT = 150
  12. EDGE_AVOID_RADIUS = 20
  13. REPULSION_RADIUS = 25
  14. CONVERT_RADIUS = 20
  15. SPEED = 1
  16.  
  17. root = tk.Tk()
  18. root.title("tk_war")
  19. root.geometry("%dx%d+%d+%d" % (WINDOW_WIDTH, WINDOW_HEIGHT, 10, 10))
  20.  
  21. canvas = tk.Canvas(root, width=WINDOW_WIDTH, height=WINDOW_HEIGHT, bg="white")
  22. canvas.pack()
  23.  
  24. tribe_color = {
  25.     "rock": "red",
  26.     "paper": "lime",
  27.     "scissors": "black"
  28. }
  29.  
  30. chase = {
  31.     "rock": "scissors",
  32.     "scissors": "paper",
  33.     "paper": "rock",
  34. }
  35.  
  36. cxy = {
  37.     "rock": "",
  38.     "scissors": "",
  39.     "paper": ""
  40. }
  41.  
  42. food_chain = '{rock} > {scissors} > {paper} > {rock}'.format_map(tribe_color)
  43.  
  44. def offset(t=1.2):
  45.     return random.uniform(-t, t)
  46.  
  47. def avoid_edges():
  48.     x0 = min(WINDOW_WIDTH - EDGE_AVOID_RADIUS, max(x, EDGE_AVOID_RADIUS))
  49.     y0 = min(WINDOW_HEIGHT - EDGE_AVOID_RADIUS, max(y, EDGE_AVOID_RADIUS))
  50.     return x0, y0
  51.    
  52. def avoid_entities(x, y):
  53.     for j, (x2, y2, entity2) in enumerate(entities):
  54.         if i != j:
  55.             distance = distance_to(x, y, x2, y2)
  56.             if distance < REPULSION_RADIUS:
  57.                 angle = math.atan2(y - y2, x - x2)
  58.                 x += 5 * math.cos(angle)
  59.                 y += 5 * math.sin(angle)
  60.                 if distance < CONVERT_RADIUS:
  61.                     entities[j] = (x2, y2, entity)
  62.                 return x, y
  63.     return movement(x, y, CX, CY)
  64.  
  65. def movement(x, y, target_x, target_y):
  66.     angle = math.atan2(y - target_y, x - target_x) + if_rev
  67.     x += SPEED * math.cos(angle + offset())
  68.     y += SPEED * math.sin(angle + offset())
  69.     return x, y
  70.  
  71. def distance_to(x1, y1, x2, y2):
  72.     return math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
  73.  
  74. def draw_entity(x, y, entity):
  75.     color = tribe_color[entity]
  76.     draw.ellipse((x - 2, y - 2, x + 2, y + 2), fill=color, outline=color)
  77.  
  78. def start(event=0):
  79.     global entities
  80.     entities = [(random.randint(0, WINDOW_WIDTH), random.randint(0, WINDOW_HEIGHT), "rock") for _ in range(ROCK_COUNT)]
  81.     entities += [(random.randint(0, WINDOW_WIDTH), random.randint(0, WINDOW_HEIGHT), "paper") for _ in range(PAPER_COUNT)]
  82.     entities += [(random.randint(0, WINDOW_WIDTH), random.randint(0, WINDOW_HEIGHT), "scissors") for _ in range(SCISSORS_COUNT)]
  83.     random.shuffle(entities)
  84.  
  85. start()
  86. root.bind("<space>", start)
  87.  
  88. pil_image = Image.new('RGB', (WINDOW_WIDTH, WINDOW_HEIGHT), color='white')
  89. draw = ImageDraw.Draw(pil_image)
  90. while True:
  91.  
  92.     for i, (x, y, entity) in enumerate(entities):
  93.         if_rev = math.pi
  94.         x, y = avoid_edges()
  95.         x, y = avoid_entities(x, y)
  96.         draw_entity(x, y, entity)
  97.         entities[i] = (x, y, entity)
  98.  
  99.     photo_image = ImageTk.PhotoImage(pil_image)
  100.  
  101.     canvas.create_image(0, 0, anchor='nw', image=photo_image)
  102.     root.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement