-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_game.py
283 lines (226 loc) · 11.9 KB
/
test_game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#config
image_adds="E:\python_file\yu.png"
image_bg = "E:\python_file\sea.jpg"
import pygame
import sys
from pygame.locals import*
import math
#基本图形绘制
def test_figure():
pygame.init()
clock = pygame.time.Clock()
bg = (255,255,255)
BLACK = (0,0,0)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
size = width,height = 700,700
screen = pygame.display.set_mode(size,RESIZABLE)
pygame.display.set_caption('基本图形绘制测试')
#多边形顶点坐标
pointlist1 = [(50,200),(100,200),(100,220),(100,250),(50,250),(50,220)]
pointlist2 = [(150,200),(200,200),(200,220),(200,250),(150,250),(150,220)]
pointlist3 = [(250,200),(300,200),(300,220),(300,250),(250,250),(250,220)]
point1=(250,75)
pointlist4 = [(point1[0]-200,point1[1]),(point1[0]-100,point1[1]-50),point1,(point1[0]+50,point1[1]-50),(point1[0]+50,point1[1]+50),point1,(point1[0]-100,point1[1]+50)]
left_button_move = False
#圆形圆心位置
position = (450,450)
right_button_move = False
#椭圆中点位置
position1 = (175,350)
#线段坐标
pointlist_line1 = [(500,60),(600,100),(650,60)]
pointlist_line2 = [(500,160),(600,200),(650,160)]
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONDOWN:
if event.button ==1:
left_button_move = True
if event.button == 3:
right_button_move = True
if event.type == MOUSEBUTTONUP:
if event.button == 1:
left_button_move = False
if event.button == 3:
right_button_move = False
#使用鼠标右键控制椭圆的位置
if right_button_move:
position1 = pygame.mouse.get_pos()
#使用鼠标左键控制多边形某一点位置,从而移动多边形
if left_button_move:
point1 = pygame.mouse.get_pos()
pointlist4 = [(point1[0]-200,point1[1]),(point1[0]-100,point1[1]-50),point1,(point1[0]+50,point1[1]-50),(point1[0]+50,point1[1]+50),point1,(point1[0]-100,point1[1]+50)]
screen.fill(bg)
#绘制矩形
pygame.draw.rect(screen,BLACK,(50,300,50,50),0)
pygame.draw.rect(screen,BLACK,(150,300,50,50),1)
pygame.draw.rect(screen,BLACK,(250,300,50,50),5)
#绘制多边形
pygame.draw.polygon(screen,BLACK,pointlist1,0)
pygame.draw.polygon(screen,BLACK,pointlist2,1)
pygame.draw.polygon(screen,BLACK,pointlist3,5)
pygame.draw.polygon(screen,RED,pointlist4,0)
#绘制圆形
pygame.draw.circle(screen,RED,position,25,1)
pygame.draw.circle(screen,GREEN,position,75,1)
pygame.draw.circle(screen,BLUE,position,100,1)
pygame.draw.circle(screen,BLACK,position,125,1)
#绘制椭圆形
pygame.draw.ellipse(screen,RED,(position1[0]-150,position1[1]+50,300,100),1)
pygame.draw.ellipse(screen,RED,(position1[0]-100,position1[1],200,200),1)
#绘制弧线
pygame.draw.arc(screen,RED,(300,50,300,100),math.pi*1.5,math.pi*2,1)
#绘制线段
pygame.draw.line(screen,RED,(500,50),(500,100),1)
pygame.draw.lines(screen,RED,1,pointlist_line1,1)
pygame.draw.lines(screen,RED,0,pointlist_line2,1)
pygame.draw.aaline(screen,BLUE,(500,10),(600,60),1)
pygame.draw.aaline(screen,BLUE,(400,10),(500,60),0)
pygame.display.flip()
clock.tick(100)
#测试对象移动
def test_game1():
#初始化pygame
pygame.init()
#实例化帧率设置对象
clock = pygame.time.Clock()
#速度,每次向左移动2个单位,向上移动1个单位
speed = [2,1]
#鱼头方向
fish_head = 1
#全屏标志,默认为非全屏
fullscreen = False
#创建窗口并设置窗口的大小,返回一个surface对象
size = width,height = 500,500
screen = pygame.display.set_mode(size,RESIZABLE)
#背景颜色RGB格式
bg = pygame.image.load(image_bg).convert()
#bg = (255,255,255)
#设置窗口标题
pygame.display.set_caption('pygame第一个测试游戏')
#加载主要处理图片,返回一个surface对象
image_actor = pygame.image.load(image_adds).convert()
#获得图片的位置矩形
position = image_actor.get_rect()
while True:
#检测是否点击关闭按钮,查看是否需要结束事件
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
#键盘输入
if event.type == KEYDOWN:
if event.key == K_LEFT:
if fish_head>0:
image_actor = pygame.transform.flip(image_actor,True,False)
fish_head = -fish_head
speed = [-1,0]
if event.key == K_RIGHT:
if fish_head<0:
image_actor = pygame.transform.flip(image_actor,True,False)
fish_head = -fish_head
speed =[1,0]
if event.key == K_UP:
if fish_head>0:
fish_head = -fish_head
image_actor = pygame.transform.flip(image_actor,True,False)
speed =[0,-1]
if event.key == K_DOWN:
if fish_head>0:
fish_head = -fish_head
image_actor = pygame.transform.flip(image_actor,True,False)
speed =[0,1]
#全屏设置(F11)
if event.key == K_F11:
fullscreen = not fullscreen
if fullscreen:
size_new = width,height = pygame.display.list_modes()[0]
position[0]=int((position[0]/screen.get_width())*size_new[0])
position[1]=int((position[1]/screen.get_height())*size_new[1])
screen = pygame.display.set_mode(size_new,FULLSCREEN)
else:
size_new = width,height = size
position[0]=int((position[0]/screen.get_width())*size_new[0])
position[1]=int((position[1]/screen.get_height())*size_new[1])
screen = pygame.display.set_mode(size_new,RESIZABLE)
#调整窗口大小
if event.type == VIDEORESIZE:
size_new = width,height = event.size
position[0]=int((position[0]/screen.get_width())*size_new[0])
position[1]=int((position[1]/screen.get_height())*size_new[1])
screen = pygame.display.set_mode(size_new,RESIZABLE)
#移动主角图像
position = position.move(speed)
#当图片x方向到达边界时,图片翻转,并反方向移动
if position.left <0 or position.right >width:
image_actor = pygame.transform.flip(image_actor,True,False)
fish_head = -fish_head
speed[0] = -speed[0]
#当图片y方向到达边界时,图片反方向移动
if position.top<0 or position.bottom > height:
speed[1] = -speed[1]
#填充背景
#screen.fill(bg)
screen.blit(bg,(0,0))
#更新图片位置
screen.blit(image_actor,position)
#更新界面
pygame.display.flip()
#延迟10毫秒
pygame.time.delay(10)
#设置帧率为1
clock.tick(30)
def play_music():
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(r"E:\python_file\ballgame\G.E.M.mp3")
pygame.mixer.music.set_volume(0.2)
pygame.mixer.music.play()
bg_size = width, height = 300, 200
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("泡沫播放器")
pause_image = pygame.image.load(r"E:\python_file\ballgame\pause.JPG").convert_alpha()
start_image = pygame.image.load(r"E:\python_file\ballgame\start.jpg").convert_alpha()
pause_rect = pause_image.get_rect()
pause_rect.left, pause_rect.top = (width - pause_rect.width) // 2, (height - pause_rect.height) // 2
start_rect = start_image.get_rect()
start_rect.left, start_rect.top = (width - start_rect.width) // 2, (height - start_rect.height) // 2
clock = pygame.time.Clock()
pause = False
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_SPACE:
pause = not pause
if event.type == MOUSEBUTTONDOWN:
if event.button == 1 and (
(pause_rect.left + pause_rect.width) >= pygame.mouse.get_pos()[0] >= pause_rect.left \
and (pause_rect.top + pause_rect.height) >= pygame.mouse.get_pos()[1] >= pause_rect.top):
pause = not pause
screen.fill((255, 255, 255))
if pause:
screen.blit(pause_image, pause_rect)
pygame.mixer.music.pause()
else:
screen.blit(start_image, start_rect)
pygame.mixer.music.unpause()
pygame.display.flip()
clock.tick(30)
if __name__=='__main__':
try:
#test_game1()
#test_figure()
play_music()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()