![]()
作者 | 小F
责编 | 伍杏玲
自古真情留不住,唯有套路得人心。
今天是情人节,程序员如何撩动心仪女神的心?当然要用程序员的方法了!
这马上给大家带来一个Python的小套路:利用Python的pygame库,生成一个套路神器。
![]()
无套路版本
无套路版本和抖音上的一些视频差不多,就是点不了拒绝按钮!是不是很实用捏?
详细代码如下:
import pygameimport randomimport sys# 根据背景图大小,设置游戏屏幕大小WIDTH, HEIGHT = 1024, 576# 不全屏screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)# 全屏# screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN, 32)pygame.display.set_caption('小姐姐,你的快递到了。')# 添加文本信息def title(text, screen, scale, color=(0, 0, 0)): font = pygame.font.SysFont('SimHei', 27) textRender = font.render(text, True, color) # 初始化文本的坐标 screen.blit(textRender, (WIDTH / scale[0], HEIGHT / scale[1]))# 按钮def button(text, x, y, w, h, color, screen): pygame.draw.rect(screen, color, (x, y, w, h)) font = pygame.font.SysFont('SimHei', 20) textRender = font.render(text, True, (255, 255, 255)) textRect = textRender.get_rect() textRect.center = ((x+w/2), (y+h/2)) screen.blit(textRender, textRect)# 生成随机的位置坐标def get_random_pos(): x, y = random.randint(10, 600), random.randint(20, 500) return x, y# 点击答应按钮后显示的页面def show_like_interface(screen): screen.fill((255, 255, 255)) background1 = pygame.image.load('214_1.jpg').convert() screen.blit(background1, (0, 0)) pygame.display.update() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit()def main(): pygame.init() clock = pygame.time.Clock() # 添加背景音乐 pygame.mixer.music.load('214_1.mp3') pygame.mixer.music.play(-1, 20) pygame.mixer.music.set_volume(0.5) # 设置不同意按钮属性 unlike_pos_x = 130 unlike_pos_y = 375 unlike_pos_width = 450 unlike_pos_height = 55 unlike_color = (115, 76, 243) # 设置同意按钮属性 like_pos_x = 130 like_pos_y = 280 like_pos_width = 450 like_pos_height = 55 like_color = (115, 76, 243) running = True while running: # 填充窗口 screen.fill((255, 255, 255)) # 添加背景图 background = pygame.image.load('214_2.jpg').convert() screen.blit(background, (0, 0)) # 获取鼠标坐标 pos = pygame.mouse.get_pos() # 判断鼠标位置,不同意时,按钮不断变化 if pos[0] < unlike_pos_x + unlike_pos_width + 5 and pos[0] > unlike_pos_x - 5 and pos[1] < unlike_pos_y + unlike_pos_height + 5 and pos[1] > unlike_pos_y - 5: while True: unlike_pos_x, unlike_pos_y = get_random_pos() if pos[0] < unlike_pos_x + unlike_pos_width + 5 and pos[0] > unlike_pos_x - 5 and pos[1] < unlike_pos_y + unlike_pos_height + 5 and pos[1] > unlike_pos_y - 5: continue break # 设置标题及按钮文本信息 title('1.如果有一天我向你表白,你会怎么样?', screen, scale=[8, 3]) button('A.你小子终于开窍了,你敢表白我就敢答应!', like_pos_x, like_pos_y, like_pos_width, like_pos_height, like_color, screen) button('B.我拿你当闺蜜,你居然想睡我!果断拒绝!', unlike_pos_x, unlike_pos_y, unlike_pos_width, unlike_pos_height, unlike_color, screen) # 设置关闭选项属性 for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() # 当鼠标点击同意按钮后,跳转结束页面 if pos[0] < like_pos_x + like_pos_width + 5 and pos[0] > like_pos_x - 5 and pos[1] < like_pos_y + like_pos_height + 5 and pos[1] > like_pos_y - 5: if event.type == pygame.MOUSEBUTTONDOWN: show_like_interface(screen) pygame.display.flip() pygame.display.update() clock.tick(60)main()
运行代码,效果如下。
[iframe]https://v.qq.com/iframe/preview.html?width=500&height=375&auto=0&vid=u0837locfx6[/iframe]
![]()
打包程序
看了上面一行行的代码,我们能直接交给女神吗?那样无疑是自取灭亡。
所以使用pyinstaller库将代码、图片及音乐素材打包成exe文件。
![]()
直接点击love.exe程序,即可运行。
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
总结
什么快递? 相关的图片,音乐、代码以及打包程序已上传公众号,菜单栏加微信即可获取。免责声明:文章广告为微信自动匹配,与本平台无关,如遇假冒伪劣请联系微信进行举报。 # 欢迎来评论区留言 #
你打算情人节送什么礼物呢?
【完】
![]()
热 文 推 荐
承认《流浪地球》就那么难吗?
手机辐射排行榜:小米、一加远超 iPhone;阿里开工彩票最高奖金 1000 万;苹果再遭集体诉讼 | 极客头条
TIOBE 2 月编程语言排行榜:Python 逼近 C,Groovy 重回 TOP20
35 岁程序员,年后第一天被辞退
分布式系统与消息投递
回乡偶记|从这个四省交界的苏北大城, 看二三线城市区块链现状
她说:是的,嫁人当嫁程序员!
百度发力医疗器械?回应称为了公益捐赠
print_r('点个好看吧!');
var_dump('点个好看吧!');
NSLog(@"点个好看吧!");
System.out.println("点个好看吧!");
console.log("点个好看吧!");
print("点个好看吧!");
printf("点个好看吧!\n");
cout |
|