基于pygame的玛丽快跑小游戏源代码,玛丽冒险小游戏代码,支持双人模式
按空格进入单人模式,按‘t’进入双人模式,双人模式下玛丽1采用空格键上跳,玛丽2采用方向上键上跳。
完整代码下载地址:Python版基于pygame的玛丽快跑小游戏源代码
复制代码
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301import pygame import random from pygame.locals import * import sys import os FPS = 30 from itertools import cycle class MyMap(): def __init__(self, x, y,path): self.bg = pygame.image.load(path).convert_alpha() self.x = x self.y = y def map_rolling(self,point): if self.x < point: self.x = -(point)+1 else: self.x -= 3 def map_update(self): SCREEN.blit(self.bg, (self.x, self.y)) class Stop_Button(): is_start = True def __init__(self): self.start_img = pygame.image.load('image/stopbutton.png').convert_alpha() #self.stop_img = pygame.image.load('image/stop.png').convert_alpha() def is_select(self): point_x, point_y = pygame.mouse.get_pos() w, h = self.start_img.get_size() in_x = point_x > 60 and point_x < 60 + w in_y = point_y > 20 and point_y < 20 + h return in_x and in_y class Music_Button(): is_open = True def __init__(self): self.open_img = pygame.image.load('image/btn_open.png').convert_alpha() self.close_img = pygame.image.load('image/btn_close.png').convert_alpha() self.bg_music = pygame.mixer.Sound('audio/bg_music.wav') def is_select(self): point_x, point_y = pygame.mouse.get_pos() w, h = self.open_img.get_size() in_x = point_x > 20 and point_x < 20 + w in_y = point_y > 20 and point_y < 20 + h return in_x and in_y class Marie(): def __init__(self,lowest_y): # 初始化小玛丽矩形 self.rect = pygame.Rect(0, 0, 0, 0) self.jumpState = False self.jumpHeight = 130 self.lowest_y = lowest_y self.jumpValue = 0 self.marieIndex = 0 self.marieIndexGen = cycle([0, 1, 2]) # 加载小玛丽图片 self.adventure_img = ( pygame.image.load("image/adventure1.png").convert_alpha(), pygame.image.load("image/adventure2.png").convert_alpha(), pygame.image.load("image/adventure3.png").convert_alpha(), ) self.jump_audio = pygame.mixer.Sound('audio/jump.wav') self.rect.size = self.adventure_img[0].get_size() self.x = 50 self.y = lowest_y self.rect.topleft = (self.x, self.y) self.mrect = self.adventure_img[0].get_rect() def jump(self): self.jumpState = True def move(self): if self.jumpState: if self.rect.y >= self.lowest_y: self.jumpValue = -5 if self.rect.y <= self.lowest_y - self.jumpHeight: self.jumpValue = 5 self.rect.y += self.jumpValue if self.rect.y >= self.lowest_y: self.jumpState = False def draw_marie(self): marieIndex = next(self.marieIndexGen) SCREEN.blit(self.adventure_img[marieIndex], (self.x, self.rect.y)) class Marie2(): def __init__(self,lowest_y): self.rect = pygame.Rect(0, 0, 0, 0) self.jumpState = False self.jumpHeight = 130 self.lowest_y = lowest_y self.jumpValue = 0 self.marieIndex = 0 self.marieIndexGen = cycle([0, 1, 2, 3]) self.adventure_img = ( pygame.image.load("image/bros1.png").convert_alpha(), pygame.image.load("image/bros3.png").convert_alpha(), pygame.image.load("image/bros3.png").convert_alpha(), pygame.image.load("image/bros4.png").convert_alpha(), ) self.jump_audio = pygame.mixer.Sound('audio/jump.wav') self.rect.size = self.adventure_img[0].get_size() self.x = 80; self.y = lowest_y; self.rect.topleft = (self.x, self.y) def jump(self): self.jumpState = True def move(self): if self.jumpState: if self.rect.y >= self.lowest_y: self.jumpValue = -5 if self.rect.y <= self.lowest_y - self.jumpHeight: self.jumpValue = 5 self.rect.y += self.jumpValue if self.rect.y >= self.lowest_y: self.jumpState = False def draw_marie(self): marieIndex = next(self.marieIndexGen) SCREEN.blit(self.adventure_img[marieIndex], (self.x, self.rect.y)) class Marie3(): def __init__(self,lowest_y): self.rect = pygame.Rect(0, 0, 0, 0) self.jumpState = False self.jumpHeight = 130 self.lowest_y = lowest_y self.jumpValue = 0 self.marieIndex = 0 self.marieIndexGen = cycle([0, 1, 2, 3]) self.adventure_img = ( pygame.image.load("image/p1.png").convert_alpha(), pygame.image.load("image/p2.png").convert_alpha(), pygame.image.load("image/p3.png").convert_alpha(), pygame.image.load("image/p4.png").convert_alpha(), ) self.jump_audio = pygame.mixer.Sound('audio/jump.wav') self.rect.size = self.adventure_img[0].get_size() self.x = 80; self.y = lowest_y; self.rect.topleft = (self.x, self.y) def jump(self): self.jumpState = True def move(self): if self.jumpState: if self.rect.y >= self.lowest_y: self.jumpValue = -8 if self.rect.y <= self.lowest_y - self.jumpHeight: self.jumpValue = 8 self.rect.y += self.jumpValue if self.rect.y >= self.lowest_y: self.jumpState = False def draw_marie(self): marieIndex = next(self.marieIndexGen) SCREEN.blit(self.adventure_img[marieIndex], (self.x, self.rect.y)) class Marie4(): def __init__(self,lowest_y): self.jumpState = False self.runState = False self.image = (pygame.image.load("image/adventure1.png").convert_alpha(), pygame.image.load("image/adventure2.png").convert_alpha(), pygame.image.load("image/adventure3.png").convert_alpha()) self.mrect = self.image[0].get_rect() self.jumpHeight = 130 self.mrect.y = lowest_y self.lowest_y = lowest_y self.jumpValue = 0 self.jump_audio = pygame.mixer.Sound('audio/jump.wav') self.index = 0 self.list = cycle([0,1,2]) def jump(self): self.jumpState = True def move(self): if self.jumpState: if self.mrect.y >= self.lowest_y: self.jumpValue = -8 if self.mrect.y <= self.lowest_y - self.jumpHeight: self.jumpValue = 8 self.mrect.y += self.jumpValue if self.mrect.y >= self.lowest_y: self.jumpState = False def draw_marie(self): index = next(self.list) SCREEN.blit(self.image[index],self.mrect) class Bullet(): def __init__(self): self.x = 80 self.y = 150 self.bullet_img = pygame.image.load("image/s.png") self.brect = self.bullet_img.get_rect() def move(self,x,y): self.brect.x = x self.brect.y = y def draw_Bullet(self): SCREEN.blit(self.bullet_img,self.brect) class Obstacle(): score = 1 move = 3 obstacle_y = 150 def __init__(self): self.rect = pygame.Rect(0, 0, 0, 0) self.missile = pygame.image.load("image/missile.png").convert_alpha() self.pipe = pygame.image.load("image/pipe.png").convert_alpha() self.numbers = (pygame.image.load('image/0.png').convert_alpha(), pygame.image.load('image/1.png').convert_alpha(), pygame.image.load('image/2.png').convert_alpha(), pygame.image.load('image/3.png').convert_alpha(), pygame.image.load('image/4.png').convert_alpha(), pygame.image.load('image/5.png').convert_alpha(), pygame.image.load('image/6.png').convert_alpha(), pygame.image.load('image/7.png').convert_alpha(), pygame.image.load('image/8.png').convert_alpha(), pygame.image.load('image/9.png').convert_alpha()) self.score_audio = pygame.mixer.Sound('audio/score.wav') r = random.randint(0, 1) if r == 0: self.image = self.missile self.move = 15 self.obstacle_y = 100 else: self.image = self.pipe self.rect.size = self.image.get_size() self.width, self.height = self.rect.size self.x = 800 self.y = self.obstacle_y self.rect.center = (self.x, self.y) def obstacle_move(self): self.rect.x -= self.move def draw_obstacle(self): SCREEN.blit(self.image, (self.rect.x, self.rect.y)) def getScore(self): self.score tmp = self.score; if tmp == 1: self.score_audio.play() self.score = 0; return tmp; def showScore(self, score,SCREENWIDTH,SCREENHEIGHT): self.scoreDigits = [int(x) for x in list(str(score))] totalWidth = 0 # 要显示的所有数字的总宽度 for digit in self.scoreDigits: # 获取积分图片的宽度 totalWidth += self.numbers[digit].get_width() # 分数横向位置 Xoffset = (SCREENWIDTH - (totalWidth+30)) for digit in self.scoreDigits: # 绘制分数 SCREEN.blit(self.numbers[digit], (Xoffset, SCREENHEIGHT * 0.1)) # 随着数字增加改变位置 Xoffset += self.numbers[digit].get_width()
完整代码下载地址:Python版基于pygame的玛丽快跑小游戏源代码
最后
以上就是鲤鱼大神最近收集整理的关于Python版基于pygame的玛丽快跑小游戏源代码,玛丽冒险小游戏代码,支持双人模式的全部内容,更多相关Python版基于pygame内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复