Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_centralizing_art.py
- import tkinter as tk
- import random
- import math
- from PIL import Image, ImageTk, ImageDraw
- WINDOW_WIDTH = 600
- WINDOW_HEIGHT = 600
- CX, CY = WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2
- ROCK_COUNT = PAPER_COUNT = SCISSORS_COUNT = 400
- EDGE_AVOID_RADIUS = 20
- REPULSION_RADIUS = 100
- SPEED = 1
- def offset(t=1.5):
- return random.uniform(-t, t)
- def avoid_edges():
- x0 = min(WINDOW_WIDTH - EDGE_AVOID_RADIUS, max(x, EDGE_AVOID_RADIUS))
- y0 = min(WINDOW_HEIGHT - EDGE_AVOID_RADIUS, max(y, EDGE_AVOID_RADIUS))
- return x0, y0
- def movement(x, y, target_x, target_y):
- angle = math.atan2(y - target_y, x - target_x) + if_rev
- x += SPEED * math.cos(angle + offset())
- y += SPEED * math.sin(angle + offset())
- return x, y
- def distance_to(x1, y1, x2, y2):
- return math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
- root = tk.Tk()
- root.title("tk_centralizing_art")
- root.geometry("%dx%d+%d+%d" % (WINDOW_WIDTH, WINDOW_HEIGHT, 10, 10))
- pil_image = Image.new('RGB', (WINDOW_WIDTH, WINDOW_HEIGHT), color='white')
- draw = ImageDraw.Draw(pil_image)
- canvas = tk.Canvas(root, width=WINDOW_WIDTH, height=WINDOW_HEIGHT, bg="white")
- canvas.pack()
- tribe_color = {
- "rock": "red",
- "paper": "lime",
- "scissors": "black"
- }
- chase = {
- "rock": "scissors",
- "scissors": "paper",
- "paper": "rock",
- }
- cxy = {
- "rock": "",
- "scissors": "",
- "paper": ""
- }
- food_chain = '{rock} > {scissors} > {paper} > {rock}'.format_map(tribe_color)
- def draw_entity(x, y, entity):
- color = tribe_color[entity]
- draw.ellipse((x - 2, y - 2, x + 2, y + 2), fill=color, outline=color)
- P = 200
- def start(event=0):
- global entities
- entities = [(random.randint(-P, WINDOW_WIDTH + P), random.randint(-P, WINDOW_HEIGHT + P), "rock") for _ in range(ROCK_COUNT)]
- entities += [(random.randint(-P, WINDOW_WIDTH + P), random.randint(-P, WINDOW_HEIGHT + P), "paper") for _ in range(PAPER_COUNT)]
- entities += [(random.randint(-P, WINDOW_WIDTH + P), random.randint(-P, WINDOW_HEIGHT + P), "scissors") for _ in range(SCISSORS_COUNT)]
- random.shuffle(entities)
- start()
- root.bind("<space>", start)
- while True:
- # draw.rectangle((0, 0, WINDOW_WIDTH, WINDOW_HEIGHT), fill='white')
- for j in '.'*P:
- for i, (x, y, entity) in enumerate(entities):
- x0, y0 = CX, CY
- if_rev = math.pi
- x, y = movement(x, y, x0, y0)
- draw_entity(x, y, entity)
- entities[i] = (x, y, entity)
- photo_image = ImageTk.PhotoImage(pil_image)
- canvas.create_image(0, 0, anchor='nw', image=photo_image)
- root.update()
- start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement