Advertisement
here2share

# tk_rock_paper_scissors_sim2.py

Mar 7th, 2024
755
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.90 KB | None | 0 0
  1. # tk_rock_paper_scissors_sim2.py
  2.  
  3. import tkinter as tk
  4. import random
  5. import math
  6. from PIL import Image, ImageTk, ImageDraw
  7.  
  8. WINDOW_WIDTH = 480
  9. WINDOW_HEIGHT = 480
  10. CX, CY = WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2
  11. ROCK_COUNT = PAPER_COUNT = SCISSORS_COUNT = 120
  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 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. r = 10
  69. def draw_entity(x, y, entity):
  70.     color = tribe_color[entity]
  71.     draw.ellipse((x - r, y - r, x + r, y + r), fill=color, outline=color)
  72.  
  73. def start(event=0):
  74.     global entities
  75.     entities = [(random.randint(0, WINDOW_WIDTH), random.randint(0, WINDOW_HEIGHT), "rock") for _ in range(ROCK_COUNT)]
  76.     entities += [(random.randint(0, WINDOW_WIDTH), random.randint(0, WINDOW_HEIGHT), "paper") for _ in range(PAPER_COUNT)]
  77.     entities += [(random.randint(0, WINDOW_WIDTH), random.randint(0, WINDOW_HEIGHT), "scissors") for _ in range(SCISSORS_COUNT)]
  78.     random.shuffle(entities)
  79.  
  80. start()
  81. root.bind("<space>", start)
  82.  
  83. while True:
  84.     pil_image = Image.new('RGB', (WINDOW_WIDTH, WINDOW_HEIGHT), color='white')
  85.     draw = ImageDraw.Draw(pil_image)
  86.  
  87.     for i, (x, y, entity) in enumerate(entities):
  88.         if_rev = math.pi
  89.         x, y = avoid_edges()
  90.         x, y = avoid_entities(x, y)
  91.         draw_entity(x, y, entity)
  92.         entities[i] = (x, y, entity)
  93.  
  94.     photo_image = ImageTk.PhotoImage(pil_image)
  95.  
  96.     canvas.create_image(0, 100, anchor='nw', image=photo_image)
  97.     canvas.create_text(CX, 36, text=food_chain, anchor='center', fill="gray", font=("Verdana", 24, "italic"))
  98.     root.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement