import pygame as pg
from pygame.locals import *
import math
pg.init()
width, height = 600, 400 #寬度和高度
screen = pg.display.set_mode((width, height)) #創建一個 Pygame 視窗,並設定大小
pg.display.set_caption('Choose your lunch') #視窗標題
white = (255, 255, 255)
black = (0, 0, 0)
blue = (0, 0, 255)
lunch_options = ['Dumplings', 'Beef noodles', 'Sushi', 'Tonkatsu', 'Fried rice', 'Salad', 'Fried chicken']
num_options = len(lunch_options)
running = True
while running:
for event in pg.event.get():
if event.type == QUIT: #如果關閉視窗,就會將 running 設置為 False來結束
running = False
screen.fill(white)
center_x, center_y = width // 2, height // 2
radius = 150 #半徑
for i in range(num_options):
angle = 2 * math.pi / num_options * i # 計算每個選項的角度,math.pi為180度
x = center_x + radius * math.cos(angle) # 計算 x 坐標
y = center_y + radius * math.sin(angle) # 計算 y 坐標
text_surface = pg.font.SysFont('Arial', 20).render(lunch_options[i], True, black)
text_rect = text_surface.get_rect(center=(x, y))
screen.blit(text_surface, text_rect)
pg.draw.circle(screen, blue, (center_x, center_y), radius, 2)
pg.display.flip()
13.關閉視窗
pg.quit()
挑戰到Day30了,但未來會繼續把這個pygame完成繼續更新!