Advertisement
here2share

# tk_shape_detection.py

Jul 15th, 2024 (edited)
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.94 KB | None | 0 0
  1. # tk_shape_detection.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageDraw, ImageFont, ImageTk
  5. import random
  6.  
  7. ww = 500
  8. hh = 500
  9.  
  10. root = tk.Tk()
  11. root.title("tk_shape_detection")
  12. root.geometry(f"{ww}x{hh}+10+10")
  13. canvas = tk.Canvas(root, width=ww, height=hh, bg="black")
  14. canvas.pack()
  15.  
  16. font_size = 500
  17. font = ImageFont.truetype("times.ttf", font_size)
  18. text = "S"
  19.  
  20. image_mask = Image.new('RGB', (ww, hh), (0, 0, 0))
  21. draw_mask = ImageDraw.Draw(image_mask)
  22. bbox = draw_mask.textbbox((0, 0), text, font=font)
  23. textwidth = bbox[2] - bbox[0]
  24. textheight = bbox[3] - bbox[1]
  25. x = (ww - textwidth) // 2 - bbox[0]
  26. y = (hh - textheight) // 2 - bbox[1]
  27. draw_mask.text((x, y), text, font=font, fill=(255, 255, 255))
  28. soldier_mask = image_mask.convert('L')
  29.  
  30. soldier = Image.new('RGB', (ww, hh), (0, 0, 0))
  31. draw = ImageDraw.Draw(soldier)
  32. draw.text((x, y), text, font=font, fill=(255, 255, 255))
  33.  
  34. color_palette = [(255, 255, 0), (0, 0, 255), (128, 0, 128), (255, 165, 0), (0, 165, 255)]
  35.  
  36. colors = []
  37. xy = set()
  38. for i in range(soldier.height):
  39.     for j in range(soldier.width):
  40.         if soldier_mask.getpixel((j, i)) != 0:
  41.             xy.add((j, i))
  42.             color = color_palette[-1]
  43.             colors.append(color)
  44.             soldier.putpixel((j, i), color)
  45.             color_palette.append(color_palette.pop(j % 3))
  46.         color_palette.append(color_palette.pop(j % 2))
  47.  
  48. soldier_tk = ImageTk.PhotoImage(soldier)
  49.  
  50. soldier_id = canvas.create_image(ww//2, hh//2, image=soldier_tk)
  51.  
  52. bullet = canvas.create_rectangle(0, 0, 10, 10, outline="")
  53.  
  54. def shuffle_colors():
  55.     pixels = colors[:]
  56.     random.shuffle(pixels)
  57.     temp_soldier = soldier.copy()
  58.  
  59.     for (i, j), color in zip(xy, pixels):
  60.         temp_soldier.putpixel((i, j), color)
  61.  
  62.     temp_soldier_tk = ImageTk.PhotoImage(temp_soldier)
  63.     canvas.itemconfig(soldier_id, image=temp_soldier_tk)
  64.     return temp_soldier_tk
  65.  
  66. def move_bullet(event):
  67.     canvas.coords(bullet, event.x - 5, event.y - 5, event.x + 5, event.y + 5)
  68.  
  69. def check_collision():
  70.     bullet_coords = canvas.coords(bullet)
  71.     bx1, by1, bx2, by2 = map(int, bullet_coords)
  72.  
  73.     points_to_check = [
  74.         (bx1, by1), (bx2, by1), (bx1, by2), (bx2, by2),
  75.         ((bx1 + bx2) // 2, (by1 + by2) // 2)
  76.     ]
  77.  
  78.     for x, y in points_to_check:
  79.         if 0 < x < ww and 0 < y < hh:
  80.             if soldier_mask.getpixel((x, y)) != 0:
  81.                 return True
  82.     return False
  83.  
  84. canvas.bind("<Motion>", move_bullet)
  85.  
  86. def toggle_text():
  87.     global soldier_tk
  88.     if canvas.itemcget(soldier_id, 'state') == 'normal':
  89.         canvas.itemconfig(soldier_id, state='hidden')
  90.     else:
  91.         canvas.itemconfig(soldier_id, state='normal')
  92.     root.after(1500, toggle_text)
  93.  
  94. toggle_text()
  95.  
  96. while True:
  97.     if check_collision():
  98.         canvas.itemconfig(bullet, fill="red")
  99.     else:
  100.         canvas.itemconfig(bullet, fill="green")
  101.     soldier_tk = shuffle_colors()
  102.     root.update_idletasks()
  103.     root.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement