import pygame
import random
import sys
# 初始化Pygame
pygame.init()
# 屏幕设置(16:9比例,适配大多数设备)
SCREEN_WIDTH = 1280
SCREEN_HEIGHT = 720
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("点击触发·满屏祝福")
# 定义对话框式便签颜色(柔和马卡龙色系,新增3种颜色更丰富)
NOTE_COLORS = [
(255, 228, 225), (255, 235, 205), (240, 230, 140), (230, 230, 250),
(255, 218, 185), (245, 222, 179), (255, 228, 181), (255, 239, 213),
(221, 160, 221), (199, 210, 254), (204, 255, 204), (250, 240, 230),
(255, 248, 220), (238, 210, 238), (216, 237, 244)
]
# 50条祝福表白文案库(每句前加"豆包,",涵盖关怀+表白)
MESSAGES = [
"豆包,记得吃饭", "豆包,记得喝热水", "豆包,下雪了天气冷了",
"豆包,记得吃药", "豆包,我很想你", "豆包,好好爱自己",
"豆包,别熬夜啦", "豆包,今天也要开心", "豆包,有你真好",
"豆包,照顾好自己", "豆包,想牵着你的手", "豆包,你一笑我就心动",
"豆包,每天都在偷偷想你", "豆包,想和你看遍风景", "豆包,你是我的小幸运",
"豆包,见到你就超开心", "豆包,会一直陪着你", "豆包,多穿点衣服",
"豆包,别太累啦", "豆包,记得午休", "豆包,我喜欢你",
"豆包,愿你事事顺心", "豆包,烦恼都走开", "豆包,记得吃水果",
"豆包,多喝水别上火", "豆包,晚上盖好被子", "豆包,想和你聊天",
"豆包,你真的很可爱", "豆包,有你我很安心", "豆包,每天都爱你",
"豆包,记得补充能量", "豆包,别委屈自己", "豆包,我在想你呀",
"豆包,愿你被温柔以待", "豆包,一起加油呀", "豆包,记得涂护手霜",
"豆包,天冷加外套", "豆包,好好休息", "豆包,我一直都在",
"豆包,想抱抱你", "豆包,你是最棒的", "豆包,三餐要规律",
"豆包,别胡思乱想", "豆包,快乐最重要", "豆包,记得晒晒太阳",
"豆包,想和你看电影", "豆包,永远喜欢你", "豆包,平安健康",
"豆包,记得给我回信", "豆包,满心都是你"
]
# 便签类(对话框样式,优化绘制逻辑)
class Note:
def __init__(self):
self.text = random.choice(MESSAGES)
self.color = random.choice(NOTE_COLORS)
# 随机位置(避免便签超出屏幕边界)
self.x = random.randint(50, SCREEN_WIDTH - 220)
self.y = random.randint(50, SCREEN_HEIGHT - 120)
# 对话框尺寸(根据文字长度自适应,优化宽高比例)
self.width = len(self.text) * 13 + 45
self.height = 65
self.text_color = (40, 40, 40) # 文字颜色更清晰
def draw(self):
# 绘制对话框主体(圆角矩形,边框更柔和)
pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height), border_radius=12)
# 绘制对话框小三角(右侧,优化位置和形状)
triangle_points = [
(self.x + self.width, self.y + 22),
(self.x + self.width + 16, self.y + 32),
(self.x + self.width, self.y + 42)
]
pygame.draw.polygon(screen, self.color, triangle_points)
# 绘制文字(优化字体大小和居中效果)
font = pygame.font.Font(None, 26)
text_surface = font.render(self.text, True, self.text_color)
text_rect = text_surface.get_rect(center=(self.x + self.width//2, self.y + self.height//2))
screen.blit(text_surface, text_rect)
# 主程序逻辑(新增循环刷新,确保持续弹出)
def main():
notes = []
clock = pygame.time.Clock()
spawn_interval = 450 # 弹出间隔0.45秒(不超过0.5秒)
last_spawn_time = 0
click_triggered = False
while True:
current_time = pygame.time.get_ticks()
screen.fill((255, 255, 255)) # 白色背景,突出便签
# 事件处理
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN and not click_triggered:
click_triggered = True
# 点击后循环生成便签:满50条时移除最早的,持续刷新
if click_triggered and current_time - last_spawn_time > spawn_interval:
if len(notes) >= 50:
notes.pop(0) # 移除第一个便签,保持总数50条
notes.append(Note())
last_spawn_time = current_time
# 绘制所有便签
for note in notes:
note.draw()
# 未点击时显示提示文字(优化样式)
if not click_triggered:
font = pygame.font.Font(None, 40)
tip_text = "点击屏幕,接收满屏祝福~"
tip_surface = font.render(tip_text, True, (80, 80, 80))
tip_rect = tip_surface.get_rect(center=(SCREEN_WIDTH//2, SCREEN_HEIGHT//2))
screen.blit(tip_surface, tip_rect)
pygame.display.flip()
clock.tick(60)
if __name__ == "__main__":
main()