程序员情人节正确打开方式
程序员情人节正确打开方式
效果展示
源码
1 import pygame 2 import random 3 import sys 4 # 设置窗口大小 5 WIDTH, HEIGHT = 1024, 576 6 # 不全屏 7 screen = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32) 8 # 全屏 9 # screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN, 32) 10 pygame.display.set_caption('小姐姐,你的快递到了。') 11 # 添加文本信息 12 def title(text, screen, scale, color=(0, 0, 0)): 13 font = pygame.font.SysFont('SimHei', 27) 14 textRender = font.render(text, True, color) 15 # 初始化文本的坐标 16 screen.blit(textRender, (WIDTH / scale[0], HEIGHT / scale[1])) 17 # 按钮 18 def button(text, x, y, w, h, color, screen): 19 pygame.draw.rect(screen, color, (x, y, w, h)) 20 font = pygame.font.SysFont('SimHei', 20) 21 textRender = font.render(text, True, (255, 255, 255)) 22 textRect = textRender.get_rect() 23 textRect.center = ((x+w/2), (y+h/2)) 24 screen.blit(textRender, textRect) 25 # 生成随机的位置坐标 26 def get_random_pos(): 27 x, y = random.randint(10, 600), random.randint(20, 500) 28 return x, y 29 # 点击答应按钮后显示的页面 30 def show_like_interface(screen): 31 screen.fill((255, 255, 255)) 32 #需要的话取消下面两行注释,括号里是文件路径 33 #background1 = pygame.image.load(' ').convert() 34 #screen.blit(background1, (0, 0)) 35 pygame.display.update() 36 while True: 37 for event in pygame.event.get(): 38 if event.type == pygame.QUIT: 39 sys.exit() 40 def main(): 41 pygame.init() 42 clock = pygame.time.Clock() 43 #添加背景音乐,需要的话取消下面三行注释,括号里是文件路径 44 #pygame.mixer.music.load(' ') 45 #pygame.mixer.music.play(-1, 20) 46 #pygame.mixer.music.set_volume(0.5) 47 # 设置不同意按钮属性 48 unlike_pos_x = 130 49 unlike_pos_y = 375 50 unlike_pos_width = 450 51 unlike_pos_height = 55 52 unlike_color = (115, 76, 243) 53 # 设置同意按钮属性 54 like_pos_x = 130 55 like_pos_y = 280 56 like_pos_width = 450 57 like_pos_height = 55 58 like_color = (115, 76, 243) 59 60 running = True 61 while running: 62 # 填充窗口 63 screen.fill((255, 255, 255)) 64 #添加背景图,需要的话去掉下面两行注释,括号里是文件路径 65 #background = pygame.image.load(' ').convert() 66 #screen.blit(background, (0, 0)) 67 68 # 获取鼠标坐标 69 pos = pygame.mouse.get_pos() 70 # 判断鼠标位置,不同意时,按钮不断变化 71 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: 72 while True: 73 unlike_pos_x, unlike_pos_y = get_random_pos() 74 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: 75 continue 76 break 77 # 设置标题及按钮文本信息 78 title('1.做我女朋友吧?', screen, scale=[8, 3]) 79 button('A.好!', like_pos_x, like_pos_y, like_pos_width, like_pos_height, like_color, screen) 80 button('B.拒绝!', unlike_pos_x, unlike_pos_y, unlike_pos_width, unlike_pos_height, unlike_color, screen) 81 # 设置关闭选项属性 82 for event in pygame.event.get(): 83 if event.type == pygame.QUIT: 84 sys.exit() 85 # 当鼠标点击同意按钮后,跳转结束页面 86 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: 87 if event.type == pygame.MOUSEBUTTONDOWN: 88 show_like_interface(screen) 89 90 pygame.display.flip() 91 pygame.display.update() 92 clock.tick(60) 93 main() 94 --------------------- 95 作者:空影星辉 96 来源:CSDN 97 原文:https://blog.csdn.net/net1801hui/article/details/87257277 98 版权声明:本文为博主原创文章,转载请附上博文链接!
常见问题

更多精彩