1
+ import pygame
2
+ import random
3
+
4
+ def draw_button (screen , text , position , right = False ):
5
+ '''
6
+ 绘制文本按钮
7
+ :param screen: 绘制在哪一个屏幕上
8
+ :param text: 绘制的文本
9
+ :param position: 绘制的位置
10
+ :param right: 是否为右边
11
+ :return: 返回一个4个元素的元祖,即button的开始位置,结束位置
12
+ '''
13
+ # 得到系统文件中的 '华文中宋' 字体, 设置字体大小为30px
14
+ font = pygame .font .SysFont ('华文中宋' , 30 )
15
+ # 得到一个pygame.Surface对象, 绘制的时候需要这个对象
16
+ # text:绘制的文本
17
+ # antialias: True or False 是否为锯齿状
18
+ # color: 字体颜色
19
+ # background:背景颜色
20
+ button = font .render (text , True , (0 ,0 ,0 ),(96 ,96 ,96 ))
21
+ # 得到button的大小,width和height,返回的是一个两个元素的元祖
22
+ button_size = button .get_size ()
23
+ # 如果right=Ture, 表示是绘制在右边的button,就让它的位置和背景图右对齐
24
+ if right :
25
+ position = position [0 ]- button_size [0 ], position [1 ]
26
+ # 在position位置处绘制button, position是一个两个元素的元祖, button是一个pygame.Surface对象
27
+ screen .blit (button , position )
28
+ # 返回一个4个元素的元祖,即button的开始位置,结束位置
29
+ return (position [0 ], position [1 ], position [0 ]+ button_size [0 ], position [1 ]+ button_size [1 ])
30
+
31
+ def position_is_in_rect (position , rect ):
32
+ '''
33
+ 检查position是否在一个矩形内区域内,
34
+ :param position: 需要判断的position
35
+ :param rect: 在哪一个矩形区域内?
36
+ :return: True or False
37
+ '''
38
+ if position [0 ] >= rect [0 ] and position [0 ] <= rect [2 ]:
39
+ if position [1 ] >= rect [1 ] and position [1 ] <= rect [3 ]:
40
+ return True
41
+ return False
42
+
43
+ def get_random_position (size = (300 , 480 )):
44
+ '''
45
+ 得到随机位置
46
+ :param size: 窗口的大小
47
+ :return: 返回一个两个元素的元祖
48
+ '''
49
+ x = random .randint (0 , size [0 ])
50
+ y = random .randint (0 , size [1 ]- 50 )
51
+ return x , y
52
+
53
+ def main ():
54
+ # 初始化pygame
55
+ pygame .init ()
56
+
57
+ # 规定窗口的大小
58
+ size = 300 , 480
59
+ # 得到一这个窗口
60
+ screen = pygame .display .set_mode (size )
61
+ # 设置窗口的标题
62
+ pygame .display .set_caption ('表白神器' )
63
+
64
+ # 加载图片
65
+ image = pygame .image .load ('./images/biaobai.png' ).convert_alpha ()
66
+ # 得到图片的尺寸
67
+ image_size = image .get_size ()
68
+
69
+ # 计算图片的放置位置, 在窗口中间
70
+ position_x = (size [0 ]- image_size [0 ]) / 2
71
+ position_y = (size [1 ]- image_size [1 ]) / 2
72
+
73
+ # 存放所有的button的位置
74
+ buttons_position = {}
75
+ # 是否点击 yes按钮了
76
+ yes = False
77
+ # 是否改变否定按钮的位置
78
+ change_position = False
79
+ # 随机位置
80
+ random_position = None
81
+
82
+ while True :
83
+ # 遍历所有的事件
84
+ for event in pygame .event .get ():
85
+ # 如果点击关闭窗口, 程序结束
86
+ if event .type == pygame .QUIT :
87
+ pygame .quit ()
88
+ exit (0 )
89
+
90
+ # 鼠标点击事件
91
+ if event .type == pygame .MOUSEBUTTONDOWN :
92
+ # 得到鼠标点击位置
93
+ click_position = pygame .mouse .get_pos ()
94
+ # 调用这个函数判断点击位置是否在yes按钮范围内
95
+ if position_is_in_rect (click_position , buttons_position ['yes' ]):
96
+ # 加载另外一张图片
97
+ image = pygame .image .load ('./images/yes.jpg' ).convert_alpha ()
98
+ # 修改yes变量为True
99
+ yes = True
100
+ # 判断点击位置时候在no按钮区域内
101
+ elif position_is_in_rect (click_position , buttons_position ['no' ]):
102
+ # 死循环
103
+ while True :
104
+ # 得到随机位置
105
+ random_position = get_random_position ()
106
+ # 如果随机位置不在yes按钮内,退出死循环
107
+ if not position_is_in_rect (random_position , buttons_position ['yes' ]):
108
+ break
109
+ # 修改change_position变量为True
110
+ change_position = True
111
+
112
+ # 填充整个窗口为白色
113
+ screen .fill ((255 ,255 ,255 ))
114
+ # 将图片绘制到窗口中
115
+ screen .blit (image , (position_x , position_y ))
116
+
117
+ # 如果没有点击yes按钮
118
+ if not yes :
119
+ # 绘制yes按钮
120
+ yes_rect_region = draw_button (screen , '好的' , (position_x , position_y + image_size [1 ] + 10 ))
121
+ # 如果change_position为True,说明需要开始随机获取no按钮的位置
122
+ # 绘制no按钮
123
+ if change_position :
124
+ no_rect_region = draw_button (screen , '考虑一下' , random_position )
125
+ else :
126
+ no_rect_region = draw_button (screen , '考虑一下' , (position_x + image_size [0 ], position_y + image_size [1 ] + 10 ), right = True )
127
+ # 将yes按钮和no按钮的所在区域(一个4各元素的元祖,也就是矩阵)存放到buttons_position字典中
128
+ buttons_position ['yes' ] = yes_rect_region
129
+ buttons_position ['no' ] = no_rect_region
130
+
131
+ # 刷新页面内容
132
+ pygame .display .update ()
133
+
134
+ if __name__ == '__main__' :
135
+ main ()
0 commit comments