• PyGame实现情人节表白利器


      前提:写不出那么那个的话哇,随便写写,随便看看,重在代码(文章末尾有免费完整源代码) 

      实验环境:

        pygame 1.9.4

        pycharm

        python3.6

      实现思路:

        pygame.display实现基本框架,然后screen接收text和button内容,pygame.mouse.get_pos()获取鼠标位置,然后判断鼠标所在位置,改变B选项位置即可(后面button函数在循环中改变,给关闭选项添加事件,然后pygame.mixer.music加载背景音乐,基本就是这些知识点了)

      实验帮助:

        参考手册:Pygame帮助手册 https://blog.csdn.net/fengf2017/article/details/79300801

      实现效果:

     

       实验过程:

         1.构建基本框架:

            和之前计算器差不多,先添加标题,text,button,什么的,然后控制好背景图片和框架的大小,别留下空白即可

            构建基本框架语句

            

    #基本框架
    WIDTH, HEIGHT = 640,640
    #不全屏
    screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
    
     pygame.init()
    

      

            添加标题:

    pygame.display.set_caption('2019-2-14情人节快乐')
    

         2.添加背景音乐和text,button,首先应该规定好大小和位置,这点很重要,不然后面完全不知道放在哪里,怎么改变也不知道

          由于本人图片是640*640,所以基本规定好了。

           添加背景音乐

          

    # 添加背景音乐
        pygame.mixer.music.load('love.mp3')
        pygame.mixer.music.play(-1, 20)
        pygame.mixer.music.set_volume(0.5)
    

            添加text和button相关信息:

          

    #信息内容
    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)

          3.随机改变B选项位置和点击确定后的刷新,和绑定关闭事件

           只需要将获取鼠标位置判断一下,然后在循环中随机改变位置,赋值给B选项即可

           点击后的刷新:首先添加语句,然后执行如下语句即可

      

        pygame.display.update()
    

            给关闭键绑定事件,实现关闭

            

    # 设置关闭选项属性
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
    

          上述关闭语句在点击A选项前后都要写,因为页面刷新,需要能够关闭本软件(良心啊!!!)

      

      1 #!/usr/bin/env python 
      2 # -*- coding: utf-8 -*- 
      3 # @Time : 2019/2/14 10:27
      4 # @Author : Empirefree 
      5 # @File : 19-情人节.py 
      6 # @Software: PyCharm Community Edition
      7 
      8 import pygame
      9 import random
     10 import sys
     11 
     12 #基本框架
     13 WIDTH, HEIGHT = 640,640
     14 #不全屏
     15 screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)
     16 #标题
     17 pygame.display.set_caption('2019-2-14情人节快乐')
     18 
     19 
     20 #信息内容
     21 def title(text, screen, scale, color=(0, 0, 0)):
     22     font = pygame.font.SysFont('SimHei', 27)
     23     textRender = font.render(text, True, color)
     24     screen.blit(textRender, (WIDTH / scale[0], HEIGHT / scale[1]))
     25 
     26 # 按钮
     27 def button(text, x, y, w, h, color, screen):
     28         pygame.draw.rect(screen, color, (x, y, w, h))
     29         font = pygame.font.SysFont('SimHei', 20)
     30         textRender = font.render(text, True, (255, 255, 255))
     31         textRect = textRender.get_rect()
     32         textRect.center = ((x+w/2), (y+h/2))
     33         screen.blit(textRender, textRect)
     34 
     35 # 生成随机的位置坐标
     36 def get_random_pos():
     37         x, y = random.randint(10, 500), random.randint(20, 500)
     38         return x, y
     39 def heart(text, x, y, w, h, color, screen):
     40     pygame.draw.rect(screen, color, (x, y, w, h))
     41     font = pygame.font.SysFont('SimHei', 20)
     42     textRender = font.render(text, True, (255, 255, 255))
     43     textRect = textRender.get_rect()
     44     textRect.center = ((x + w / 2), (y + h / 2))
     45     screen.blit(textRender, textRect)
     46 #点击同意后的按钮
     47 def show_like_interface(screen):
     48     screen.fill((255, 255, 255))
     49     background1 = pygame.image.load('heart.jpg').convert()
     50     screen.blit(background1, (0, 0))
     51     heart('I like you too', 200, 300, 250, 50, (255, 0,0), screen)
     52 
     53     pygame.display.update()
     54     while True:
     55         for event in pygame.event.get():
     56             if event.type == pygame.QUIT:
     57                 sys.exit()
     58 
     59 def main():
     60     pygame.init()
     61     clock = pygame.time.Clock()
     62     # 添加背景音乐
     63     pygame.mixer.music.load('love.mp3')
     64     pygame.mixer.music.play(-1, 20)
     65     pygame.mixer.music.set_volume(0.5)
     66     # 设置不同意按钮属性
     67     unlike_pos_x = 130
     68     unlike_pos_y = 375
     69     unlike_pos_width = 450
     70     unlike_pos_height = 55
     71     unlike_color = (115, 76, 243)
     72     # 设置同意按钮属性
     73     like_pos_x = 130
     74     like_pos_y = 280
     75     like_pos_width = 450
     76     like_pos_height = 55
     77     like_color = (115, 76, 243)
     78 
     79     running = True
     80     while running:
     81         # 填充窗口
     82         screen.fill((255, 255, 255))
     83         # 添加背景图
     84         background = pygame.image.load('avatar.jpg').convert()
     85         screen.blit(background, (0, 0))
     86 
     87         # 获取鼠标坐标
     88         pos = pygame.mouse.get_pos()
     89         # 判断鼠标位置,不同意时,按钮不断变化
     90         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:
     91             while True:
     92                 unlike_pos_x, unlike_pos_y = get_random_pos()
     93                 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:
     94                     continue
     95                 break
     96 
     97         # 设置标题及按钮文本信息
     98         title('1.今天情人节,我想向你表白~~~', screen, scale=[8, 3])
     99         button('A.My love is endless', like_pos_x, like_pos_y, like_pos_width, like_pos_height, like_color, screen)
    100         button('B.渣男:不主动,不拒绝,不负责!', unlike_pos_x, unlike_pos_y, unlike_pos_width, unlike_pos_height, unlike_color, screen)
    101         # 设置关闭选项属性
    102         for event in pygame.event.get():
    103             if event.type == pygame.QUIT:
    104                 sys.exit()
    105         # 当鼠标点击同意按钮后,跳转结束页面
    106         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:
    107             if event.type == pygame.MOUSEBUTTONDOWN:
    108                 show_like_interface(screen)
    109 
    110         pygame.display.flip()
    111         pygame.display.update()
    112         clock.tick(60)
    113 
    114 if __name__ == '__main__':
    115     main()

      

      后话:祝大家情人节快乐,Happy New Year,给大家拜年啦,猪年2019年快乐,2019-2-14胡宇乔Empirefree立。

      链接: https://pan.baidu.com/s/1MT17OWOAtRT2-J6Sv_Vg2w 提取码: h15d 复制这段内容后打开百度网盘手机App,操作更方便哦

  • 相关阅读:
    面试-Hash是怎么实现的?
    教程-在Delphi中怎么查看是否有内存泄漏(Delphi2007)+WIN7
    面试-SizeOf一个对象会得到什么?
    Unity-Animator深入系列---录制与回放
    Animation.Sample用法介绍
    Unity-Animator深入系列---StateMachineBehaviour状态机脚本学习
    Unity-Animator深入系列---deltaPosition&deltaRotation
    Unity-Animato深入系列---FloatValue阻尼
    Animation在每一帧中的执行顺序测试
    Unity脚本在层级面板中的执行顺序测试2
  • 原文地址:https://www.cnblogs.com/meditation5201314/p/10380920.html
Copyright © 2020-2023  润新知