个人家庭使用
import pygame
import random
import math
import os
# 初始化 pygame
pygame.init()
# 设置窗口大小和标题
screen = pygame.display.set_mode((800, 600)) # 宽度稍微增加以放置图例
pygame.display.set_caption("抽奖转盘")
# 定义转盘的奖品和对应的概率
options = ["特等奖", "一等奖", "二等奖", "三等奖", "四等奖", "幸运奖"]
# 极低的特等奖和一等奖概率,其他奖项的概率适当提高
probabilities = [0.00001, 0.00005, 0.001, 0.02, 0.1, 0.87894]
# 圆盘的中心和半径
center_x, center_y = 550, 280
radius = 140
arrow_length = 30
# 确认字体路径
font_path = "C:\\Windows\\Fonts\\msyh.ttc" # 修改为你系统中的中文字体路径
# 如果字体文件不存在,使用默认字体
if not os.path.exists(font_path):
font = pygame.font.Font(None, 24) # 如果没有中文字体,使用pygame默认字体
print("字体文件未找到,使用默认字体")
else:
font = pygame.font.Font(font_path, 24) # 使用指定字体
# 设置一个较小的字体用于奖品
small_font = pygame.font.Font(font_path, 16) if os.path.exists(font_path) else pygame.font.Font(None, 16)
bold_font = pygame.font.Font(font_path, 16) # 你可以根据需要调整字体大小
bold_font.set_bold(True)
# 画转盘的函数
def draw_wheel():
angle_step = 360 / len(options)
for i, option in enumerate(options):
angle = i * angle_step
x1 = center_x + radius * math.cos(math.radians(angle))
y1 = center_y + radius * math.sin(math.radians(angle))
x2 = center_x + (radius - 30) * math.cos(math.radians(angle))
y2 = center_y + (radius - 30) * math.sin(math.radians(angle))
# 使用较小字体绘制奖品名称
text = small_font.render(option, True, (255, 255, 0))
screen.blit(text, (x2 - text.get_width() / 2, y2 - text.get_height() / 2))
# 画指针的函数(箭头)
def draw_pointer(angle):
# 计算箭头的末端位置
arrow_x = center_x + (radius - arrow_length) * math.cos(math.radians(angle))
arrow_y = center_y + (radius - arrow_length) * math.sin(math.radians(angle))
# 计算箭头两侧的点(小三角形)
arrow_left_x = center_x + (radius - arrow_length - 8) * math.cos(math.radians(angle - 5))
arrow_left_y = center_y + (radius - arrow_length - 8) * math.sin(math.radians(angle - 5))
arrow_right_x = center_x + (radius - arrow_length - 8) * math.cos(math.radians(angle + 5))
arrow_right_y = center_y + (radius - arrow_length - 8) * math.sin(math.radians(angle + 5))
# 绘制从圆心到箭头的线
pygame.draw.line(screen, (255, 255, 0), (center_x, center_y), (arrow_x, arrow_y), 3) # 红色线
# 绘制箭头(三角形)
pygame.draw.polygon(screen, (255, 255, 0), [(arrow_x, arrow_y), (arrow_left_x, arrow_left_y), (arrow_right_x, arrow_right_y)])
# 旋转转盘并选择奖品
def spin_wheel():
winner = random.choices(options, probabilities, k=1)[0]
rotations = random.randint(5, 10)
final_angle = random.randint(0, 360)
total_angle = 0
return winner, rotations, final_angle, total_angle
# 计算指针最终位置对应的奖品
def get_winner_angle(angle):
angle_step = 360 / len(options)
index = int((angle + 90) // angle_step) % len(options) # 根据旋转后的角度计算指向的奖品
return options[index]
# 加载背景图
background_image = pygame.image.load('choujiang.jpg')
# 设置主循环
running = True
clock = pygame.time.Clock()
# 按钮状态和结果显示
spinning = False
winner_text = "祝您好运"
# 主循环
while running:
# 每次绘制背景图
screen.blit(background_image, (0, 0)) # 绘制背景图
# 绘制转盘
pygame.draw.circle(screen, (0, 0, 255), (center_x, center_y), radius, 2)
draw_wheel()
# 绘制旋转指针
if spinning:
draw_pointer(total_angle)
else:
draw_pointer(-90) # 初始指针位置指向12点方向
# 绘制“恭喜你赢得了”的文本,放到转盘上方
result_surface = font.render(winner_text, True, (255, 255, 0))
screen.blit(result_surface, (center_x - result_surface.get_width() / 2, center_y - radius - 50))
# 绘制“点击开始抽奖”按钮,放到转盘下方
start_text = font.render("点击开始抽奖", True, (0, 0, 0))
screen.blit(start_text, (center_x - start_text.get_width() / 2, center_y + radius + 20))
# 绘制图例框(左侧)
legend_rect = pygame.Rect(50, 180, 230, 240) # 图例的位置和大小
pygame.draw.rect(screen, (230, 230, 250), legend_rect) # 背景色
pygame.draw.rect(screen, (0, 0, 0), legend_rect, 2) # 边框
# 绘制奖品内容
legend_title = font.render("奖品内容", True, (0, 0, 0))
screen.blit(legend_title, (legend_rect.x + (legend_rect.width - legend_title.get_width()) // 2, legend_rect.y + 5))
prize1_text = small_font.render("特等奖:", True, (255, 0, 0))
prize2_text = small_font.render("一等奖", True, (255, 0, 255))
prize3_text = small_font.render("二等奖", True, (0, 0, 255))
prize4_text = small_font.render("三等奖", True, (0, 255, 0))
prize5_text = small_font.render("四等奖", True, (255, 165, 0))
prize6_text = small_font.render("幸运奖", True, (0, 0, 0))
screen.blit(prize1_text, (legend_rect.x + 10, legend_rect.y + 40))
screen.blit(prize2_text, (legend_rect.x + 10, legend_rect.y + 70))
screen.blit(prize3_text, (legend_rect.x + 10, legend_rect.y + 100))
screen.blit(prize4_text, (legend_rect.x + 10, legend_rect.y + 130))
screen.blit(prize5_text, (legend_rect.x + 10, legend_rect.y + 160))
screen.blit(prize6_text, (legend_rect.x + 10, legend_rect.y + 190))
# 事件处理
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if not spinning:
spinning = True
winner, rotations, final_angle, total_angle = spin_wheel()
# 模拟旋转
while total_angle < rotations * 360 + final_angle:
total_angle += 30
# 重新绘制背景图
screen.blit(background_image, (0, 0)) # 每次都重新绘制背景图
# 保持图例和奖品内容
pygame.draw.rect(screen, (230, 230, 250), legend_rect) # 背景色
pygame.draw.rect(screen, (0, 0, 0), legend_rect, 2) # 边框
screen.blit(legend_title, (legend_rect.x + (legend_rect.width - legend_title.get_width()) // 2, legend_rect.y + 5))
screen.blit(prize1_text, (legend_rect.x + 10, legend_rect.y + 40))
screen.blit(prize2_text, (legend_rect.x + 10, legend_rect.y + 70))
screen.blit(prize3_text, (legend_rect.x + 10, legend_rect.y + 100))
screen.blit(prize4_text, (legend_rect.x + 10, legend_rect.y + 130))
screen.blit(prize5_text, (legend_rect.x + 10, legend_rect.y + 160))
screen.blit(prize6_text, (legend_rect.x + 10, legend_rect.y + 190))
# 绘制转盘和指针
pygame.draw.circle(screen, (0, 0, 255), (center_x, center_y), radius, 2)
draw_wheel()
draw_pointer(total_angle)
# 更新屏幕
pygame.display.update()
pygame.time.delay(20)
# 确定最终角度指向的奖品
winner_text = f"恭喜!你赢得了 {get_winner_angle(total_angle)}!"
spinning = False
pygame.display.update() # 更新屏幕
clock.tick(60) # 控制帧率
pygame.quit()
评论