13点21分打卡 你就是真爱
文章转载自公众号法纳斯特
自古真情留不住,唯有套路得人心。今天就是情人节了,这就给大家带来一个Python 的小套路。刷抖音的小伙伴,也许会有点印象。利用Python的pygame库,生成一个套路神器。
/ 01 / 无套路版本
无套路版本和抖音上的一些视频差不多。
就是点不了拒绝按钮...
详细代码如下。
importpygame
importrandom
importsys
# 根据背景图大小,设置游戏屏幕大小
WIDTH, HEIGHT =1024,576
# 不全屏
screen = pygame.display.set_mode((WIDTH, HEIGHT),,32)
# 全屏
# screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN, 32)
pygame.display.set_caption('小姐姐,你的快递到了。')
# 添加文本信息
deftitle(text, screen, scale, color=(,,)):
font = pygame.font.SysFont('SimHei',27)
textRender = font.render(text,True, color)
# 初始化文本的坐标
screen.blit(textRender, (WIDTH / scale[], HEIGHT / scale[1]))
# 按钮
defbutton(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)
# 生成随机的位置坐标
defget_random_pos():
x, y = random.randint(10,600), random.randint(20,500)
returnx, y
# 点击答应按钮后显示的页面
defshow_like_interface(screen):
screen.fill((255,255,255))
background1 = pygame.image.load('214_1.jpg').convert()
screen.blit(background1, (,))
pygame.display.update()
whileTrue:
foreventinpygame.event.get():
ifevent.type == pygame.QUIT:
sys.exit()
defmain():
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
whilerunning:
# 填充窗口
screen.fill((255,255,255))
# 添加背景图
background = pygame.image.load('214_2.jpg').convert()
screen.blit(background, (,))
# 获取鼠标坐标
pos = pygame.mouse.get_pos()
# 判断鼠标位置,不同意时,按钮不断变化
ifpos[] unlike_pos_x -5andpos[1] unlike_pos_y -5:
whileTrue:
unlike_pos_x, unlike_pos_y = get_random_pos()
ifpos[] unlike_pos_x -5andpos[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)
# 设置关闭选项属性
foreventinpygame.event.get():
ifevent.type == pygame.QUIT:
sys.exit()
# 当鼠标点击同意按钮后,跳转结束页面
ifpos[] like_pos_x -5andpos[1] like_pos_y -5:
ifevent.type == pygame.MOUSEBUTTONDOWN:
show_like_interface(screen)
pygame.display.flip()
pygame.display.update()
clock.tick(60)
main()
运行代码,效果如下。
代码里是设置有音乐的,所以运行代码后,会有背景音乐。
这里因为小F的电脑太渣,录屏下的音质特别差。
所以选择录制一个无声的视频。
大家将就着看吧。
/ 02 / 套路版本
自古真情留不住,唯有套路得人心。
或许上面的那个版本只会让小姐姐觉得你很可恨...
毕竟强迫症患者有点多,愣是不让人点。
估摸着要挨小姐姐的揍。
所以便有了下面这个套路版本。
详细代码如下。
importpygame
importrandom
importsys
# 根据背景图大小,设置游戏屏幕大小
WIDTH, HEIGHT =1024,576
screen = pygame.display.set_mode((WIDTH, HEIGHT),,32)
pygame.display.set_caption('小姐姐,你的快递到了。')
# 添加文本信息
deftitle(text, screen, scale, color=(,,)):
font = pygame.font.SysFont('SimHei',27)
textRender = font.render(text,True, color)
# 初始化文字的坐标
screen.blit(textRender, (WIDTH / scale[], HEIGHT / scale[1]))
# 按钮
defbutton(text, x, y, w, h, color, screen, color_text):
pygame.draw.rect(screen, color, (x, y, w, h))
font = pygame.font.SysFont('SimHei',20)
textRender = font.render(text,True, color_text)
textRect = textRender.get_rect()
textRect.center = ((x+w/2), (y+h/2))
screen.blit(textRender, textRect)
# 生成随机的位置坐标
defget_random_pos():
x, y = random.randint(20,620), random.randint(20,460)
returnx, y
# 点击答应后显示的页面
defshow_like_interface(screen):
screen.fill((255,255,255))
background1 = pygame.image.load('214_1.jpg').convert()
screen.blit(background1, (,))
pygame.display.update()
whileTrue:
foreventinpygame.event.get():
ifevent.type == pygame.QUIT:
sys.exit()
# 点击不答应按钮后显示的页面
defshow_unlike_interface(screen):
screen.fill((255,255,255))
background_1 = pygame.image.load('214_3.jpg').convert()
screen.blit(background_1, (,))
pygame.display.update()
whileTrue:
foreventinpygame.event.get():
ifevent.type == pygame.QUIT:
sys.exit()
defmain():
num =
pygame.init()
clock = pygame.time.Clock()
# 添加背景音乐
pygame.mixer.music.load('214_2.mp3')
pygame.mixer.music.play(-1,40)
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
whilerunning:
# 填充窗口
screen.fill((255,255,255))
# 添加背景图
background = pygame.image.load('214_2.jpg').convert()
screen.blit(background, (,))
# 获取鼠标坐标
pos = pygame.mouse.get_pos()
ifpos[] unlike_pos_x -5andpos[1] unlike_pos_y -5:
whileTrue:
ifnum >5:
break
num +=1
unlike_pos_x, unlike_pos_y = get_random_pos()
ifpos[] unlike_pos_x -5andpos[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, (255,255,255))
# 设置小套路文本
ifnum
button('B.我拿你当闺蜜,你居然想睡我!果断拒绝!', unlike_pos_x, unlike_pos_y, unlike_pos_width, unlike_pos_height, unlike_color, screen, (255,255,255))
ifnum >5:
button('B. 我拿你当闺蜜,你居然想睡我!果断答应!', unlike_pos_x, unlike_pos_y, unlike_pos_width, unlike_pos_height, unlike_color, screen, (255,255,255))
# 设置套路文本
ifnum ==1:
button('操作提示:请直接点击答案,切勿手抖!', unlike_pos_x, unlike_pos_y -50, unlike_pos_width, unlike_pos_height, (255,255,255), screen, (192,,))
ifnum ==2:
button('咋又抖了?女神是不是哪里不舒服?', unlike_pos_x, unlike_pos_y -50, unlike_pos_width, unlike_pos_height, (255,255,255), screen, (192,,))
ifnum ==3:
button('呀!看来这病得还不轻啊!', unlike_pos_x, unlike_pos_y -50, unlike_pos_width, unlike_pos_height, (255,255,255), screen, (192,,))
ifnum ==4:
button('生了病也没人照料,心疼……', unlike_pos_x, unlike_pos_y -50, unlike_pos_width, unlike_pos_height, (255,255,255), screen, (192,,))
ifnum ==5:
button('好险!差点儿就点到了呢!', unlike_pos_x, unlike_pos_y -50, unlike_pos_width, unlike_pos_height, (255,255,255), screen, (192,,))
ifnum ==6:
button('哎,算了,不躲了,你选吧', unlike_pos_x, unlike_pos_y -50, unlike_pos_width, unlike_pos_height, (255,255,255), screen, (192,,))
# 点击套路按钮
ifnum >5:
ifpos[] unlike_pos_x -5andpos[1] unlike_pos_y -5:
ifevent.type == pygame.MOUSEBUTTONDOWN:
show_unlike_interface(screen)
foreventinpygame.event.get():
ifevent.type == pygame.QUIT:
sys.exit()
# 点击同意按钮
ifpos[] like_pos_x -5andpos[1] like_pos_y -5:
ifevent.type == pygame.MOUSEBUTTONDOWN:
show_like_interface(screen)
pygame.display.flip()
pygame.display.update()
clock.tick(60)
main()
运行代码,效果如下。
看出来那满满的套路了没。
/ 03 / 打包程序
看了上面一行行的代码,我们能直接交给女神吗?
答案不用想就知道的。
肯定是不能。
因为那样无疑是自取灭亡...
所以使用pyinstaller库将代码、图片及音乐素材打包成exe文件。
直接点击love.exe程序,即可运行。