Advertisement
here2share

# tk_rock_paper_scissors_sim.py

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