七夕表白

利用pygame生成表白exe文件

Pygame是一个利用SDL库的写的游戏库,也是一种界面程序下面通过pygame生成exe文件。每行代码对对其进行了注释。

按钮的生成
def button(text, rect, color, screen):
    pygame.draw.rect(screen, color, rect)#在荧幕上画出区域颜色
    font=pygame.font.SysFont(FONT,20)#得到字体
    textRender = font.render(text, True, (0, 0, 0))#按照字体写出文本
    textRect = textRender.get_rect()#得到文本框大小
    textRect.center = ((rect[0] + rect[2] / 2), (rect[1] + rect[3] / 2))#设置文本框中心确保文本框与按钮保持一致
    screen.blit(textRender, textRect)#绑定

传入参数为按钮的文本内容,按钮的坐标宽高(元组),颜色和荧幕。

标题的生成
def title(text, screen, scale, color=(253, 86, 155)):
    font=pygame.font.SysFont(FONT, WIDTH // (len(text) * 2))#得到字体字号
    textRender = font.render(text, True, color)#按照字体写出文本
    textRect = textRender.get_rect()#得到文本框大小
    textRect.midtop = (250, HEIGHT / scale[1])#设置文本框中心
    screen.blit(textRender, textRect)#绑定

传入参数为按钮的文本内容,按钮的宽高,颜色和荧幕。

生成随机的位置坐标
def get_random_pos():
    x, y = random.randint(20, 500), random.randint(20, 350)
    return x, y

通过随机数产生随机坐标

判断点是否在矩形内部
def judge(mouse_pos,rect,epsilon=5):#判断
    if mouse_pos[0] < rect[0] + rect[2] + epsilon and mouse_pos[0] > rect[0] - epsilon and mouse_pos[1] < rect[1] + rect[3] + epsilon and mouse_pos[1] > rect[1] - epsilon:
        return True
    else:
        return False

判断此时的鼠标点是否在矩形内部,方便不喜欢按钮的逃离

结果页面
def show_love(text, screen, color=(253, 86, 155)):
    screen.fill(BACKGROUND)#此时从新进入了新页面
    font=pygame.font.SysFont(FONT,WIDTH // (len(text)))
    textRender = font.render(text, True, color)
    textRect = textRender.get_rect()
    textRect.midtop = (WIDTH / 2, 170)
    screen.blit(textRender, textRect)
    button('来来来,亲一个就让你重回自我', MY_BUTTON_POS['kiss'], (212, 212, 190), screen)
    pygame.display.update()
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = pygame.mouse.get_pos()# 得到鼠标的位置
                # 判断用户鼠标的位置是否在喜欢的按钮范围内
                if judge(mouse_pos,MY_BUTTON_POS['kiss']):
                    pygame.quit()
                    sys.exit()
全局变量控制
#---全局变量生成
WIDTH, HEIGHT = 700, 400
BACKGROUND = (255, 255, 255)
FONT='华文行楷'
MY_BUTTON_POS={'dislove':(300,320,100,50),'love':(80,320,100,50),'kiss':(200,320,300,50)}

主要对按钮高宽位置,字体背景定义常量。

主函数
def main():
    pygame.init()# 初始化pygame,为使用硬件做准备
    screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)# 创建一个窗口
    img = pygame.image.load('./2.png')# 导入资源
    pygame.display.set_icon(img)# 对应用程序设置图标
    pygame.display.set_caption('一个喜欢你很久的小哥哥')# 对应用程序设置标题
    clock = pygame.time.Clock()
    pygame.mixer.music.load('./music.mp3')# 导入资源 pygame.mixer声音驱动
    pygame.mixer.music.play(-1, 7.0)# 播放模式
    pygame.mixer.music.set_volume(0.35)# 播放音量模式
    running = True
    like_color = (212, 212, 190)
    while running:
        screen.fill(BACKGROUND)#填充背景
        img = pygame.image.load("./3.png")  # 导入资源
        imgRect = img.get_rect()
        imgRect.midtop = 590, 0  # 设置中上坐标
        screen.blit(img, imgRect)
        # 监听用户事件
        for event in pygame.event.get():
            # 判断用户是否点击了鼠标
            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = pygame.mouse.get_pos()# 得到鼠标的位置
                # 判断用户鼠标的位置是否在喜欢的按钮范围内
                if judge(mouse_pos,MY_BUTTON_POS['love']):
                    #like_color = BACKGROUND
                    running = False
        mouse_pos = pygame.mouse.get_pos()
        if judge(mouse_pos,MY_BUTTON_POS['dislove']):
            while True:
                MY_BUTTON_POS['dislove']=list(MY_BUTTON_POS['dislove'])
                MY_BUTTON_POS['dislove'][0], MY_BUTTON_POS['dislove'][1] = get_random_pos()
                MY_BUTTON_POS['dislove'] = tuple(MY_BUTTON_POS['dislove'])
                if judge(mouse_pos,MY_BUTTON_POS['dislove']):
                    continue
                break
        title('小姐姐,我喜欢你很久了', screen, scale=[2, 10])
        title('做我女朋友好不好呀', screen, scale=[2, 6])
        button('好呀', MY_BUTTON_POS['love'], like_color, screen)
        button('残忍拒绝', MY_BUTTON_POS['dislove'], like_color, screen)
        pygame.display.flip()
        pygame.display.update()
        clock.tick(60)
    show_love('我就知道小姐姐你也喜欢我', screen)


if __name__ == '__main__':
    main()

演示

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值