備忘録として残しておきます。
import pygame
import random
import ctypes
import math
# 画面サイズ設定
WIDTH, HEIGHT = 1920, 1080 # FHDサイズ
# 初期化
pygame.init()
display_flags = pygame.NOFRAME | pygame.SRCALPHA
screen = pygame.display.set_mode((WIDTH, HEIGHT), display_flags)
pygame.display.set_caption("リアルな透明な葉っぱ")
# Windows の場合、ウィンドウを透過
hwnd = pygame.display.get_wm_info()["window"]
ctypes.windll.user32.SetWindowLongW(hwnd, -20, ctypes.windll.user32.GetWindowLongW(hwnd, -20) | 0x00080000)
ctypes.windll.user32.SetLayeredWindowAttributes(hwnd, 0x000000, 255, 0x00000001)
# 葉っぱを描く関数
def create_leaf():
leaf = pygame.Surface((80, 40), pygame.SRCALPHA) # 透明なSurfaceを作成
leaf.fill((0, 0, 0, 0)) # 完全に透明にする
# 葉っぱの形を曲線で描く
points = [(40 + 30 * math.cos(math.radians(angle)), 20 + 15 * math.sin(math.radians(angle))) for angle in range(0, 360, 10)]
pygame.draw.polygon(leaf, (34, 139, 34, 150), points) # 葉っぱ色 (深い緑色, 透明度150)
pygame.draw.aalines(leaf, (0, 100, 0, 180), True, points) # 葉脈のような輪郭線を描画
return leaf
# 葉っぱのリスト
leaves = [(create_leaf(), random.randint(0, WIDTH), random.randint(0, HEIGHT)) for _ in range(100)]
# メインループ
running = True
while running:
screen.fill((0, 0, 0, 0)) # 背景を透明にする
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 葉っぱを描画
for leaf, x, y in leaves:
screen.blit(leaf, (x, y))
pygame.display.update()
pygame.quit()
コメント