1
+ # 导入所需模块
2
+ import random
3
+ import pygame
4
+
5
+ # pygame初始化
6
+ pygame .init ()
7
+
8
+ # 设置主屏窗口
9
+ screen = pygame .display .set_mode ((600 , 800 ))
10
+
11
+ # 设置游戏名称
12
+ pygame .display .set_caption ("明月几时有" )
13
+
14
+ # 加载背景
15
+ bg_menu = pygame .image .load ("pic/bg1.jpg" )
16
+ bg_game = pygame .image .load ("pic/bg2.jpg" )
17
+ word_menu = pygame .image .load ("pic/word.png" )
18
+
19
+ # 加载UI
20
+ target = pygame .image .load ("pic/target.png" )
21
+ wrong = pygame .image .load ("pic/wrong.png" )
22
+ player = pygame .image .load ("pic/player.png" )
23
+ btn = pygame .image .load ("pic/button.png" )
24
+
25
+ # 定义文字,初始化变量
26
+ score_word = "分数"
27
+ time_word = "机会"
28
+ end_word = "你的分数是{},中秋快乐!"
29
+
30
+ font = pygame .font .SysFont ("Microsoft YaHei" , 40 )
31
+
32
+ score = 0
33
+ score_num = font .render (str (score ), True , (255 , 255 , 255 ))
34
+ time = 4
35
+ time_num = font .render (str (time ), True , (255 , 255 , 255 ))
36
+
37
+ time_text = font .render (time_word , True , (255 , 255 , 255 ))
38
+ score_text = font .render (score_word , True , (255 , 255 , 255 ))
39
+ end_text = font .render (end_word .format (score ), True , (255 , 255 , 255 ))
40
+
41
+ now_pos = 0
42
+ speed1 = 1
43
+ speed2 = 1.5
44
+ tar_pos = []
45
+ wro_pos = []
46
+
47
+
48
+ # 控制月饼密度
49
+ for i in range (0 , 8 ):
50
+ tar_pos .append ([random .randint (50 , 550 ),(i - 1 ) * 100 ])
51
+
52
+ # 控制树枝密度
53
+ for i in range (0 , 2 ):
54
+ wro_pos .append ([random .randint (50 , 550 ),(i - 1 ) * 300 ])
55
+
56
+
57
+ # 按钮类
58
+ class Button (object ):
59
+ # 初始化
60
+ def __init__ (self , btn , position ):
61
+ self .btn = btn
62
+ self .position = position
63
+
64
+ def show (self ):
65
+ weight , height = self .btn .get_size ()
66
+ x_pos , y_pos = self .position
67
+ screen .blit (self .btn , (x_pos - weight / 2 , y_pos - height / 2 ))
68
+
69
+ # 判断是否按下
70
+ def click (self ):
71
+ if event .type == pygame .MOUSEBUTTONDOWN :
72
+ point_x , point_y = pygame .mouse .get_pos ()
73
+ x_pos , y_pos = self .position
74
+ weight , height = self .btn .get_size ()
75
+ ok_x = x_pos - weight / 2 < point_x < x_pos + weight / 2
76
+ ok_y = y_pos - height / 2 < point_y < y_pos + height / 2
77
+ if ok_x and ok_y :
78
+ return True
79
+
80
+ button = Button (btn , (300 , 420 ))
81
+
82
+ while True :
83
+ for event in pygame .event .get ():
84
+ if event .type == pygame .QUIT :
85
+ exit ()
86
+
87
+ # 初始菜单
88
+ screen .blit (bg_menu , (0 , 0 ))
89
+ screen .blit (word_menu , (0 , - 70 ))
90
+ button .show ()
91
+ if button .click ():
92
+ time = 3
93
+ score = 0
94
+ score_num = font .render (str (score ), True , (255 , 255 , 255 ))
95
+ time_num = font .render (str (time ), True , (255 , 255 , 255 ))
96
+
97
+ # 开始游戏
98
+ if time > 0 and time < 4 and score >= 0 :
99
+ screen .blit (bg_game , (0 , 0 ))
100
+ screen .blit (time_text , (50 , 50 ))
101
+ screen .blit (time_num , (170 , 50 ))
102
+ screen .blit (score_text , (50 , 100 ))
103
+ screen .blit (score_num , (170 , 100 ))
104
+ screen .blit (player , (now_pos , 550 ))
105
+
106
+ # 速度变化
107
+ if score <= 30 :
108
+ speed1 = 0.8
109
+ speed2 = 1.2
110
+ elif score <= 60 :
111
+ speed1 = 1
112
+ speed2 = 1.5
113
+ elif score <= 90 :
114
+ speed1 = 1.2
115
+ speed2 = 1.8
116
+ elif score <= 120 :
117
+ speed1 = 1.4
118
+ speed2 = 2.1
119
+ elif score <= 150 :
120
+ speed1 = 1.8
121
+ speed2 = 2.7
122
+ elif score <= 200 :
123
+ speed1 = 2
124
+ speed2 = 3
125
+ elif score <= 250 :
126
+ speed1 = 3
127
+ speed2 = 4.5
128
+ else :
129
+ speed1 = 4
130
+ speed2 = 6
131
+
132
+ # 月饼运动路线
133
+ for i in range (len (tar_pos )):
134
+ screen .blit (pygame .transform .scale (target , (50 , 50 )), (tar_pos [i ][0 ], tar_pos [i ][1 ] - 800 ))
135
+ tar_pos [i ][1 ] += speed1
136
+ if tar_pos [i ][1 ] > 1600 :
137
+ tar_pos [i ][1 ] = 800
138
+ tar_pos [i ][0 ] = random .randint (50 , 550 )
139
+ score -= 2
140
+ score_num = font .render (str (score ), True , (255 , 255 , 255 ))
141
+ # 边界判定
142
+ if tar_pos [i ][0 ] + 50 > now_pos and tar_pos [i ][0 ] < now_pos + 75 and tar_pos [i ][1 ] >= 1300 and tar_pos [i ][1 ] <= 1500 :
143
+ tar_pos [i ][1 ] = 800
144
+ tar_pos [i ][0 ] = random .randint (50 , 550 )
145
+ score += 10
146
+ score_num = font .render (str (score ), True , (255 , 255 , 255 ))
147
+
148
+ # 树枝运动路线
149
+ for i in range (len (wro_pos )):
150
+ screen .blit (pygame .transform .scale (wrong , (50 , 50 )), (wro_pos [i ][0 ], wro_pos [i ][1 ] - 800 ))
151
+ wro_pos [i ][1 ] += speed2
152
+ if wro_pos [i ][1 ] > 1600 :
153
+ wro_pos [i ][1 ] = 800
154
+ wro_pos [i ][0 ] = random .randint (50 , 550 )
155
+ # 边界判定
156
+ if wro_pos [i ][0 ] + 50 > now_pos and wro_pos [i ][0 ] < now_pos + 75 and wro_pos [i ][1 ] >= 1300 and wro_pos [i ][1 ] <= 1500 :
157
+ wro_pos [i ][1 ]= 800
158
+ wro_pos [i ][0 ] = random .randint (50 , 550 )
159
+ time -= 1
160
+ time_num = font .render (str (time ), True , (255 , 255 , 255 ))
161
+
162
+ # 角色移动
163
+ if event .type == pygame .MOUSEMOTION :
164
+ move_x , move_y = pygame .mouse .get_pos ()
165
+ now_pos = move_x - 75
166
+ if now_pos < 0 :
167
+ now_pos = 0
168
+ if now_pos > 600 :
169
+ now_pos = 525
170
+
171
+ # 重新开始游戏
172
+ if time == 0 or score < 0 :
173
+ # 初始化游戏
174
+ now_pos = 0
175
+ speed = 1
176
+ # 月饼运动路线
177
+ for i in range (len (tar_pos )):
178
+ tar_pos [i ][0 ] = random .randint (50 , 550 )
179
+ tar_pos [i ][1 ] = (i - 1 ) * 100
180
+ # 树枝运动路线
181
+ for i in range (len (wro_pos )):
182
+ wro_pos [i ][0 ] = random .randint (50 , 550 )
183
+ wro_pos [i ][1 ] = (i - 1 ) * 300
184
+ end_text = font .render (end_word .format (score ), True , (255 , 255 , 255 ))
185
+ screen .blit (bg_menu , (0 , 0 ))
186
+ screen .blit (end_text , (50 , 220 ))
187
+ button .show ()
188
+ # 点击按钮重开
189
+ if button .click ():
190
+ time = 3
191
+ score = 0
192
+ score_num = font .render (str (score ), True , (255 , 255 , 255 ))
193
+
194
+ pygame .display .update ()
0 commit comments