Advertisement
here2share

# tk_centralizing_art.py

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