前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python制作表白小助手

Python制作表白小助手

作者头像
PM小王
发布2019-07-01 12:26:45
1.7K0
发布2019-07-01 12:26:45
举报

程序介绍

<br />

Python版本:3.6.5 相关模块: pygame模块; 以及一些Python自带的模块。

鼠标无法点击“滚”按钮,只能点击“好呀”按钮。抖音上的程序据说是用C#写的,具体的源码自己没有去找,用上次文章提到的方法将txt改成vbs应该也能实现,但是今天我们利用python实现这一有趣好玩的功能。

具体实现

按钮实现

# 按钮
def button(text, x, y, w, h, color, screen):
    pygame.draw.rect(screen, color, (x, y, w, h))
    font = pygame.font.Font('./font/simkai.ttf', 20)
    textRender = font.render(text, True, (0, 0, 0))
    textRect = textRender.get_rect()
    textRect.center = ((x+w/2), (y+h/2))    
    screen.blit(textRender, textRect)

按钮的实现通过定义一个函数,将大小,颜色,位置等等都设置好

标题实现

# 标题
def title(text, screen, scale, color=( 255,0,255)):
    font = pygame.font.Font('./font/simkai.ttf', WIDTH//(len(text)*2))
    textRender = font.render(text, True, color)
    textRect = textRender.get_rect()
    textRect.midtop = (WIDTH/scale[0], HEIGHT/scale[1])
    screen.blit(textRender, textRect)

这里要设置的是字体的一些样式,同样使用函数封装

跳转后的页面

# 点击喜欢按钮后显示的页面
def show_like_interface(text, screen, color=( 255,0,255)):
    screen.fill(BACKGROUND)
    font = pygame.font.Font('./font/simkai.ttf', WIDTH//(len(text)))
    textRender = font.render(text, True, color)
    textRect = textRender.get_rect()
    textRect.midtop = (WIDTH/2, HEIGHT/2)
    screen.blit(textRender, textRect)
    pygame.display.update()
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

设置跳转后页面的相关属性,按钮,标题和跳转后的页面的代码非常类似,大家可以对比着来看。

主函数

# 主函数
def main():
    pygame.init()
    screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
    pygame.display.set_caption('FROM全网最帅Ahab')
    clock = pygame.time.Clock()
    pygame.mixer.music.load('./bg_music/1.mp3')
    pygame.mixer.music.play(-1, 30.0)
    pygame.mixer.music.set_volume(0.25)
    unlike_pos_x = 330
    unlike_pos_y = 300
    unlike_pos_width = 100
    unlike_pos_height = 50
    like_pos_x = 180
    like_pos_y = 300
    like_pos_width = 100
    like_pos_height = 50
    running = True
    like_color = (255,192,203)
    while running:
        screen.fill(BACKGROUND)
        img = pygame.image.load("./imgs/1.png")
        imgRect = img.get_rect()
        imgRect.midtop = WIDTH//2, HEIGHT//4
        screen.blit(img, imgRect)
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = pygame.mouse.get_pos()
                if mouse_pos[0] < like_pos_x+like_pos_width+5 and mouse_pos[0] > like_pos_x-5 and\
                    mouse_pos[1] < like_pos_y+like_pos_height+5 and mouse_pos[1] > like_pos_y-5:
                    like_color = BACKGROUND
                    running = False
        mouse_pos = pygame.mouse.get_pos()
        if mouse_pos[0] < unlike_pos_x+unlike_pos_width+5 and mouse_pos[0] > unlike_pos_x-5 and\
            mouse_pos[1] < unlike_pos_y+unlike_pos_height+5 and mouse_pos[1] > unlike_pos_y-5:
            while True:
                unlike_pos_x, unlike_pos_y = get_random_pos()
                if mouse_pos[0] < unlike_pos_x+unlike_pos_width+5 and mouse_pos[0] > unlike_pos_x-5 and\
                    mouse_pos[1] < unlike_pos_y+unlike_pos_height+5 and mouse_pos[1] > unlike_pos_y-5:
                    continue
                break

因为代码比较多这里我只上传了一部分代码,这里将一些素材进行引入,设置主要逻辑,原理就是不断捕获鼠标位置,当检测到鼠标出现在我们不希望被点击的按钮上时,就改变该按钮的位置,使得对方无法点击到该按钮。

1

教你一点社交知识

2

Python助你抢红包

3

我是如何自学 Python 的?

4

用Python做一个三阶拼图

··· END ···

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-02-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序员小王 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 程序介绍
  • 具体实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档