python实现跨年表白神器--你值得拥有


Posted in Python onJanuary 04, 2021

hello,大家好,我是Dream。马上就跨年了,为了广大的单身男性成员,我就慈悲一下,把我的存货?表白神器拿出来了,百试百灵(虽然我一次也没试过),今天分享给大家,别忘了给我点赞哟~
话不多说,先看效果图:

python实现跨年表白神器--你值得拥有

从图上看,很明显这是一个选择题,但代码的神奇之处就在这里,当她把鼠标拖到‘不行'的地方时,奇迹发生了,当当当~

python实现跨年表白神器--你值得拥有
python实现跨年表白神器--你值得拥有
python实现跨年表白神器--你值得拥有

屏幕上会轮番展示出你的优点,这是我的优点(我只是实话实说的哟)
最最最重要的是她关不掉窗口,重要的事说三遍:关不掉 关不掉 关不掉 就是关不掉!!!气死她哈哈哈。。。(你好贱,我好爱)
她只能选择好的,然后…(你懂得嘿嘿嘿)

python实现跨年表白神器--你值得拥有

说了折磨多,你们是不是非常期待我的代码呀,看代码之前,别忘了先点个关注哟~
接下来,代码展示:

# sys是python的标准库
# 提供了python运行时环境变量的操控
# sys.exit()用于结束游戏退出
import sys
import pygame
import random

# 游戏的高宽
WIDTH, HEIGHT = 640, 360
# 把颜色值(230, 230, 230)赋值给 bg_color 变量
# 三个整数依次是三原色中红色、绿色和蓝色的浓度值。浓度值是一个整数,最大为255,最小为0。
bg_color = (255, 255, 255)
button_text_list = ['徐以鹏比易烊千玺帅亿点', '徐以鹏脾气好', '徐以鹏会洗衣服', '徐以鹏体贴']


# 点击喜欢按钮后显示的页面
def show_like_interface(text, screen, color=(255, 0, 0)):
  screen.fill(bg_color)
  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 == pygame.QUIT:
        pygame.quit()
        sys.exit()


# 按钮
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=(0, 0, 0)):
  # pygame.font.Font("字体","字号",*)
  font = pygame.font.Font('./font/simkai.ttf', WIDTH // (len(text) * 2))
  # 使用已有的文本创建一个位图image,返回值为一个image;对于位图可用get_height(),get_width()的方法获得高与宽;True表示是否抗锯齿,第三个为字体颜色,还可以有第四个为背景色,没有时就为默认的透明;
  textRender = font.render(text, True, color)
  # Rect对象有一些重要的属性,如:top,botton,letf、right表示上下左右
  # width,height表示宽高  我有这些值之后,对于我们编写程序十分方便
  textRect = textRender.get_rect()
  # 中央x坐标整数值 顶部y坐标的整数值
  textRect.midtop = (WIDTH / scale[0], HEIGHT / scale[1])
  # 将位图绘制到屏幕上,screen为建立的主屏;
  screen.blit(textRender, textRect)

  # 生成随机的位置坐标


def get_random_pos():
  x, y = random.randint(20, WIDTH - 20), random.randint(20, HEIGHT - 20)
  return x, y


def main():
  text = "不行"
  # 在我们要动手用它完成我们的想法之前,电脑这个强迫症需要我们检查一遍,这个工具包是否完整,能否正常给我们提供帮助。而这个检查的动作, pygame.init()  检查,电脑上一些需要的硬件调用接口、基础功能是否有问题。如果有,他会在程序运行之前就反馈给你,方便你进行排查和规避。

  # 对pygame内部各种功能进行初始化创建及变量设置,比如pygmae里面的窗体,键盘的使用的事件队列,等等都需要我们pygame.init()初始化
  pygame.init()
  # 调用 display 模块的 set_mode 函数,作用是初始化屏幕对象(也即窗口对象)。此处传入一个参数,即(640, 360)元组,这使得窗口的分辨率是640*360
  screen = pygame.display.set_mode((WIDTH, HEIGHT))
  # 窗口标题
  pygame.display.set_caption("小徐的表白")
  # 不喜欢按钮的初始位置和大小
  unlike_pos_x = 330
  unlike_pos_y = 250
  unlike_pos_width = 100
  unlike_pos_height = 50

  # 喜欢按钮的初始位置和大小
  like_pos_x = 180
  like_pos_y = 250
  like_pos_width = 100
  like_pos_height = 50
  # 标识位,作为小姐姐之后点击了同意后退出的标准
  running = True
  # 按钮颜色
  like_color = (216, 191, 216)
  while running:

    # 填充屏幕背景色
    # 显示窗口背景填充bg_color眼神
    screen.fill(bg_color)
    # 加载图片,从文件加载新图片
    img = pygame.image.load("./imgs/3.jpg")
    # Surface对象与图像时一一对应关系
    # 简单理解在pygame里导入的任何图片都是Surface对象
    # pygame使用内部定义的Surface对象表示所有载入的图像,其中get_rect()反法返回一个覆盖图像的矩形Rect对象
    # Rect对象有一些重要的属性,如:top,botton,letf、right表示上下左右
    # width,height表示宽高  我有这些值之后,对于我们编写程序十分方便
    imgRect = img.get_rect()
    # 图片位置
    # 中央x坐标整数值 顶部y坐标的整数值
    imgRect.midtop = 80, 10
    # 将一个图像绘制在一个图像上,及将img绘制在imgRect位置上。通过Rect对象上引导对图片的绘制
    screen.blit(img, imgRect)
    # 监听事件
    # pygame.event.get() 的作用是获取事件列表。事件列表内包含0个或多个事件对象 (点击 鼠标移动 关闭窗口)
    # 依次赋值给 event 变量
    for event in pygame.event.get():
      # 检测到鼠标
      if event.type == pygame.MOUSEBUTTONDOWN:
        # 获取鼠标位置
        mouse_pos = pygame.mouse.get_pos()
        # 若点击了喜欢按钮,停止 while 循环
        if mouse_pos[0] < like_pos_x + like_pos_width and mouse_pos[0] > like_pos_x and mouse_pos[
          1] < like_pos_y + like_pos_height and mouse_pos[1] > like_pos_y:
          like_color = bg_color
        running = False
      # 获取鼠标位置
      # 若鼠标位置位于按钮区域内
      # 则随机生成按钮位置进行显示
    mouse_pos = pygame.mouse.get_pos()
    if mouse_pos[0] < unlike_pos_x + unlike_pos_width and mouse_pos[0] > unlike_pos_x and \
        mouse_pos[1] < unlike_pos_y + unlike_pos_height and mouse_pos[1] > unlike_pos_y:
      while True:
        unlike_pos_x, unlike_pos_y = get_random_pos()
        text = button_text_list[random.randint(0, len(button_text_list) - 1)]
        if mouse_pos[0] < unlike_pos_x + unlike_pos_width and mouse_pos[0] > unlike_pos_x and \
            mouse_pos[1] < unlike_pos_y + unlike_pos_height and mouse_pos[1] > unlike_pos_y:
          continue
        break
    title('宝贝,我观察你很久了', screen, scale=[1.8, 10])
    title('做我女朋友好不好呀', screen, scale=[1.8, 3])
    button('好的', like_pos_x, like_pos_y, like_pos_width,
        like_pos_height, like_color, screen)
    button(text, unlike_pos_x, unlike_pos_y, unlike_pos_width,
        unlike_pos_height, (216, 191, 216), screen)
    # 显示游戏
    # 刷新屏幕,以使最近的绘制操作生效。
    pygame.display.flip()
    # 对窗口进行更新
    pygame.display.update()
  # 创建Clock对象,用于操作时间
  # tick(60)控制帧速度,即窗口刷新速度,每秒钟60次帧刷新,视频中每次展示的静态图像称为帧
  pygame.time.Clock().tick(60)
  show_like_interface('我就知道小姐姐你也喜欢我~', screen, color=(0, 0, 0))


# 表示程序的主入口。所以以后为了避免该文件被外部文件调用,一般建议加上
if __name__ == '__main__':
  main()

当然在运行这个代码之前,你还需要在同一路径下装上这个楷体语言包

python实现跨年表白神器--你值得拥有

还有这张图片哟~

python实现跨年表白神器--你值得拥有

祝你表白成功,在新的一年里可以和自己喜欢的人在一起!

到此这篇关于python实现跨年表白神器--你值得拥有的文章就介绍到这了,更多相关python跨年表白神器内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python利用装饰器进行运算的实例分析
Aug 04 Python
python 调用win32pai 操作cmd的方法
May 28 Python
深入理解Python中的*重复运算符
Oct 28 Python
Python使用SQLite和Excel操作进行数据分析
Jan 20 Python
详解Python中的动态属性和特性
Apr 07 Python
python 读文件,然后转化为矩阵的实例
Apr 23 Python
python 将对象设置为可迭代的两种实现方法
Jan 21 Python
python os.path.isfile 的使用误区详解
Nov 29 Python
Python 读取有公式cell的结果内容实例方法
Feb 17 Python
使用python-Jenkins批量创建及修改jobs操作
May 12 Python
详解python百行有效代码实现汉诺塔小游戏(简约版)
Oct 30 Python
基于Python绘制子图及子图刻度的变换等的问题
May 23 Python
Python列表元素删除和remove()方法详解
Jan 04 #Python
python3列表删除大量重复元素remove()方法的问题详解
Jan 04 #Python
关于python中remove的一些坑小结
Jan 04 #Python
python中remove函数的踩坑记录
Jan 04 #Python
python 日志模块logging的使用场景及示例
Jan 04 #Python
python 邮件检测工具mmpi的使用
Jan 04 #Python
Python3中的tuple函数知识点讲解
Jan 03 #Python
You might like
关于PHP中Object对象的笔记分享
2011/06/28 PHP
基于PHP实现简单的随机抽奖小程序
2016/01/05 PHP
php in_array() 检查数组中是否存在某个值详解
2016/11/23 PHP
解决php extension 加载顺序问题
2019/08/16 PHP
javascript Array.sort() 跨浏览器下需要考虑的问题
2009/12/07 Javascript
jquery常用技巧及常用方法列表集合
2011/04/06 Javascript
详解JavaScript中undefined与null的区别
2014/03/29 Javascript
理解js对象继承的N种模式
2016/01/25 Javascript
jQuery mobile在页面加载时添加加载中效果 document.ready 和window.onload执行顺序比较
2016/07/14 Javascript
纯js实现手风琴效果代码
2020/04/17 Javascript
vue2的todolist入门小项目的详细解析
2017/05/11 Javascript
Node.JS使用Sequelize操作MySQL的示例代码
2017/10/09 Javascript
写gulp遇到的ES6问题详解
2018/12/03 Javascript
nodejs搭建本地服务器并访问文件操作示例
2019/05/11 NodeJs
Vue CLI3创建项目部署到Tomcat 使用ngrok映射到外网
2019/05/16 Javascript
[42:32]完美世界DOTA2联赛PWL S2 LBZS vs FTD.C 第二场 11.27
2020/12/01 DOTA
Python中的fileinput模块的简单实用示例
2015/07/09 Python
Python实现删除当前目录下除当前脚本以外的文件和文件夹实例
2015/07/27 Python
浅谈pyhton学习中出现的各种问题(新手必看)
2017/05/17 Python
Python字符串格式化的方法(两种)
2017/09/19 Python
深入理解Python单元测试unittest的使用示例
2017/11/18 Python
Python3.x爬虫下载网页图片的实例讲解
2018/05/22 Python
django删除表重建的实现方法
2019/08/28 Python
python网络编程 使用UDP、TCP协议收发信息详解
2019/08/29 Python
快速解决docker-py api版本不兼容的问题
2019/08/30 Python
python函数中将变量名转换成字符串实例
2020/05/11 Python
超级实用的8个Python列表技巧
2020/08/24 Python
THE OUTNET英国官网:国际设计师品牌折扣网站
2016/08/14 全球购物
美国婴儿用品店:Babies”R”Us
2017/10/12 全球购物
有趣、实用和鼓舞人心的产品:Inspire Uplift
2019/11/05 全球购物
大学生职业生涯设计书
2014/01/02 职场文书
文明美德伴我成长演讲稿
2014/05/12 职场文书
4s店活动策划方案
2014/08/25 职场文书
观看安全警示教育片心得体会
2016/01/15 职场文书
2017元旦晚会开幕词
2016/03/03 职场文书
MyBatis-Plus 批量插入数据的操作方法
2021/09/25 Java/Android