首页 > 编程语言 >【脚本项目源码】Python制作提升成功率90%的表白神器

【脚本项目源码】Python制作提升成功率90%的表白神器

时间:2022-12-26 09:55:42浏览次数:44  
标签:Python cfg self 源码 text pygame 90% screen rect

前言

今天子川就给大家带来就是的利用Python制作表白神器,让这个寒冷的冬天变得格外温馨,到了年底依然能热情拥抱,也见证了两人情意如昔;记得发给自己的心仪对象,废话不多说直接开整~

在这里插入图片描述

开发工具

Python版本: 3.6

相关模块:

random模块

pygame模块

cfg模块

sys模块

tkinter模块

环境搭建

安装Python并添加到环境变量,pip安装需要的相关模块即可。

文中图片素材实战教程,评论留言获取。
.
在这里插入图片描述

代码实现

import sys
import cfg
import random
import pygame
from tkinter import Tk, messagebox

class Button(pygame.sprite.Sprite):
	def __init__(self, x, y, width, height, text, fontpath, fontsize, fontcolor, bgcolors, edgecolor, edgesize=1, is_want_to_be_selected=True, screensize=None, **kwargs):
		pygame.sprite.Sprite.__init__(self)
		self.rect = pygame.Rect(x, y, width, height)
		self.text = text
		self.font = pygame.font.Font(fontpath, fontsize)
		self.fontcolor = fontcolor
		self.bgcolors = bgcolors
		self.edgecolor = edgecolor
		self.edgesize = edgesize
		self.is_want_tobe_selected = is_want_to_be_selected
		self.screensize = screensize
	#自动根据各种情况将按钮绑定到屏幕
	def draw(self, screen, mouse_pos):
		# 鼠标在按钮范围内
		if self.rect.collidepoint(mouse_pos):
			# --不想被选中
			if not self.is_want_tobe_selected:
				while self.rect.collidepoint(mouse_pos):
					self.rect.left, self.rect.top = random.randint(0, self.screensize[0]-self.rect.width), random.randint(0, self.screensize[1]-self.rect.height)
			pygame.draw.rect(screen, self.bgcolors[0], self.rect, 0)
			pygame.draw.rect(screen, self.edgecolor, self.rect, self.edgesize)
		# 鼠标不在按钮范围内
		else:
			pygame.draw.rect(screen, self.bgcolors[1], self.rect, 0)
			pygame.draw.rect(screen, self.edgecolor, self.rect, self.edgesize)
		text_render = self.font.render(self.text, True, self.fontcolor)
		fontsize = self.font.size(self.text)
		screen.blit(text_render, (self.rect.x+(self.rect.width-fontsize[0])/2, self.rect.y+(self.rect.height-fontsize[1])/2))


#在指定位置显示文字'
def showText(screen, text, position, fontpath, fontsize, fontcolor, is_bold=False):
	font = pygame.font.Font(fontpath, fontsize)
	font.set_bold(is_bold)
	text_render = font.render(text, True, fontcolor)
	screen.blit(text_render, position)

剩余代码

'''主函数'''
def main():
	# 初始化
	pygame.init()
	screen = pygame.display.set_mode(cfg.SCREENSIZE, 0, 32)
	pygame.display.set_icon(pygame.image.load(cfg.ICON_IMAGE_PATH))
	pygame.display.set_caption('来自一位喜欢你的小哥哥')
	# 背景音乐
	pygame.mixer.music.load(cfg.BGM_PATH)
	pygame.mixer.music.play(-1, 30.0)
	# biu爱心那个背景图片
	bg_image = pygame.image.load(cfg.BG_IMAGE_PATH)
	bg_image = pygame.transform.smoothscale(bg_image, (150, 150))
	# 实例化两个按钮
	button_yes = Button(x=20, y=cfg.SCREENSIZE[1]-70, width=120, height=35, 
						text='好呀', fontpath=cfg.FONT_PATH, fontsize=15, fontcolor=cfg.BLACK, edgecolor=cfg.SKYBLUE, 
						edgesize=2, bgcolors=[cfg.DARKGRAY, cfg.GAINSBORO], is_want_to_be_selected=True, screensize=cfg.SCREENSIZE)
	button_no = Button(x=cfg.SCREENSIZE[0]-140, y=cfg.SCREENSIZE[1]-70, width=120, height=35, 
					   text='算了吧', fontpath=cfg.FONT_PATH, fontsize=15, fontcolor=cfg.BLACK, edgecolor=cfg.DARKGRAY, 
					   edgesize=1, bgcolors=[cfg.DARKGRAY, cfg.GAINSBORO], is_want_to_be_selected=False, screensize=cfg.SCREENSIZE)
	# 是否点击了好呀按钮
	is_agree = False
	# 主循环
	clock = pygame.time.Clock()
	while True:
		# --背景图片
		screen.fill(cfg.WHITE)
		screen.blit(bg_image, (cfg.SCREENSIZE[0]-bg_image.get_height(), 0))
		# --鼠标事件捕获
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				# ----没有点击好呀按钮之前不许退出程序
				if is_agree:
					pygame.quit()
					sys.exit()
			elif event.type == pygame.MOUSEBUTTONDOWN and event.button:
				if button_yes.rect.collidepoint(pygame.mouse.get_pos()):
					button_yes.is_selected = True
					root = Tk()
					root.withdraw()
					messagebox.showinfo('', '❤❤❤么么哒❤❤❤')
					root.destroy()
					is_agree = True
		# --显示文字
		showText(screen=screen, text='小姐姐, 我观察你很久了', position=(40, 50), 
				 fontpath=cfg.FONT_PATH, fontsize=25, fontcolor=cfg.BLACK, is_bold=False)
		showText(screen=screen, text='做我女朋友好不好?', position=(40, 100), 
				 fontpath=cfg.FONT_PATH, fontsize=25, fontcolor=cfg.BLACK, is_bold=True)
		# --显示按钮
		button_yes.draw(screen, pygame.mouse.get_pos())
		button_no.draw(screen, pygame.mouse.get_pos())
		# --刷新
		pygame.display.update()
		clock.tick(60)

#run
if __name__ == '__main__':
	main()

效果展示

表白神器成功我有

最后

今天的分享到这里就结束了 ,感兴趣的朋友也可以去试试哈

对文章有问题的,或者有其他关于python的问题,可以在评论区留言或者私信我哦

觉得我分享的文章不错的话,可以关注一下我,或者给文章点赞(/≧▽≦)/

标签:Python,cfg,self,源码,text,pygame,90%,screen,rect
From: https://www.cnblogs.com/guzichuan/p/17005046.html

相关文章

  • Python小程序实现
    Python小程序实现标签(空格分隔):python目录Python小程序实现1,16进制IP地址转换为10进制2,10进制IP地址转换为16进制3,16进制字符串转换为10进制1,16进制IP地址转换为10进制......
  • [Smplify环境配置]Ubuntu 22.04+Python 3.9+pycharm 2022.3 配置Smplify
    最近在做单目3d人体姿态估计的相关项目,看了一些基于smpl的论文,smplify是smpl人体模型比较早期的应用,想着复现了研究一下,但是smplify是基于python2.7编写的,这里结合网上很......
  • python编程 ——从入门到实践——第三章,列表
    1、改变字符串大小写的三个函数name='adchanlong'print(name.title()) #title()对name每个词的首字母进行大写print(name.upper())#upper()对name全部变成大写prin......
  • LongAdder类实现原理、源码解析
    1.概述AtomicLong通过循环CAS实现原子操作,缺点是当高并发下竞争比较激烈的时候,会出现大量的CAS失败,导致循环CAS次数大大增加,这种自旋是要消耗时间cpu时间片的,......
  • Python函数用法和底层分析
    目录Python函数用法和底层分析函数的基本概念Python函数的分类核心要点形参和实参文档字符串(函数的注释)返回值函数也是对象,内存底层分析变量的作用域(全局变量和局部变......
  • 用 Python 和 OpenCV 检测图片上的条形码()
    原文地址:http://python.jobbole.com/80448/假设我们要检测下图中的条形码:图1:包含条形码的示例图片现在让我们开始写点代码,新建一个文件,命名为detect_barcode.py,打开并编码:Py......
  • 巨蟒python全栈开发数据库前端1:HTML基础
     1.HTML介绍什么是前端?前端就是我们打开浏览器的页面.,很多公司都有自己的浏览器的页面,这个阶段学习的就是浏览器界面比如京东的界面:https://www.jd.com/ 引子例1 soc......
  • 巨蟒python全栈开发linux之centos6
    1.nginx复习1.nginx是什么nginx是支持反向代理,负载均衡,且可以实现web服务器的软件在129服务器中查看,我们使用的是淘宝提供的tengine,也是一种nginx服务器我们下载的是tengi......
  • 源码编译安装httpd
     1.基础环境准备1.1创建一个系统用户 [root@node2~]#useradd-r-M-s/sbin/nologinapache [root@node2~]#idapache uid=299(apache)gid=299(apache)gro......
  • Python super()参数详解
    这里记录一下python中的super()的两个参数需要注意的关键点。具体来说如以下代码所示:#子类B父类AclassB(A):def__init__(self):super(B,self).__init......