程序員情人節正確打開方式

程序員情人節正確打開方式

效果展示

《程序員情人節正確打開方式》

源碼

 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 版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

常見問題

点赞

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *