-
Notifications
You must be signed in to change notification settings - Fork 0
/
mario.py
228 lines (167 loc) · 7.16 KB
/
mario.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
from pygame import *
import pyganim
import pygame
from time import sleep
from threading import Thread
WIDTH = 41
HEIGHT = 60
MOVE_SPEED = 3.5
JUMP_POWER = 11
GRAVITY = 0.35
ANIMATION_DELAY = 55
COLOR = (0, 0, 0)
i = 0
LEFT_POSE = image.load("images/marioLeft.png")
RIGHT_MOVE_ANIMATION = [("images/marioRight_0.png"), ("images/marioRight_1.png"),
("images/marioRight_2.png"), ("images/marioRight_3.png")]
LEFT_MOVE_ANIMATION = [("images/marioLeft_0.png"), ("images/marioLeft_1.png"),
("images/marioLeft_2.png"), ("images/marioLeft_3.png")]
STAY_ANIMATION = [('images/marioRight_0.png', ANIMATION_DELAY)]
JUMP_LEFT_ANIMATION = [('images/marioJumpLeft.png', ANIMATION_DELAY)]
JUMP_RIGHT_ANIMATION = [('images/marioJumpRight.png', ANIMATION_DELAY)]
class Mario(sprite.Sprite):
def __init__(self, x, y, coins = 0, lives = 5):
# Инициализация главного персонажа
super().__init__()
self.image = LEFT_POSE
self.rect = Rect(x, y, WIDTH, HEIGHT)
self.onGround = False
self.onTube = False
self.x_speed = 0
self.y_speed = 0
self.coins = coins
self.lives = lives
self.image.set_colorkey(Color(COLOR))
# Анимация движения вправо
boltAnim = []
for anim in RIGHT_MOVE_ANIMATION:
boltAnim.append((anim, ANIMATION_DELAY))
self.boltAnimRight = pyganim.PygAnimation(boltAnim)
self.boltAnimRight.play()
# Анимация движения влево
boltAnim = []
for anim in LEFT_MOVE_ANIMATION:
boltAnim.append((anim, ANIMATION_DELAY))
self.boltAnimLeft = pyganim.PygAnimation(boltAnim)
self.boltAnimLeft.play()
self.boltAnimStay = pyganim.PygAnimation(STAY_ANIMATION)
self.boltAnimStay.play()
self.boltAnimStay.blit(self.image, (0, 0))
self.boltAnimJumpLeft = pyganim.PygAnimation(JUMP_LEFT_ANIMATION)
self.boltAnimJumpLeft.play()
self.boltAnimJumpRight = pyganim.PygAnimation(JUMP_RIGHT_ANIMATION)
self.boltAnimJumpRight.play()
self.boltAnimJump = pyganim.PygAnimation(STAY_ANIMATION)
self.boltAnimJump.play()
def update(self, moves, platforms, coins, mobs, spec_platforms, sewers, stairs, flours):
# Изменение позиции персонажа
# moves - результат работы файла events.
movingLeft = moves[0]
movingRight = moves[1]
movingUp = moves[2]
# Проверка, упал ли вниз карты
if 800 < self.rect.bottom < 811:
self.lives -= 1
return True
if movingRight:
self.x_speed = MOVE_SPEED
self.image.fill(Color(COLOR))
if self.y_speed != 0.7 and self.y_speed != 0.35 and self.y_speed != 0:
self.boltAnimJumpRight.blit(self.image, (0, 0))
else:
self.boltAnimRight.blit(self.image, (0, 0))
if movingLeft:
self.x_speed = -MOVE_SPEED
self.image.fill(Color(COLOR))
if self.y_speed != 0.7 and self.y_speed != 0.35 and self.y_speed != 0:
self.boltAnimJumpLeft.blit(self.image, (0, 0))
else:
self.boltAnimLeft.blit(self.image, (0, 0))
if not movingRight and not movingLeft:
self.x_speed = 0
self.boltAnimStay.blit(self.image, (0, 0))
self.image.fill(Color(COLOR))
self.boltAnimStay.blit(self.image, (0, 0))
if movingUp:
if self.onGround:
jump_sound = pygame.mixer.Sound('music/jump.wav')
jump_sound.play(0)
self.y_speed -= JUMP_POWER
self.onGround = False
if not self.onGround:
self.y_speed += GRAVITY
self.onTube = False
self.onGround = False
self.rect.centery += self.y_speed
if self.check_for_collide(0, self.y_speed, platforms, coins, mobs, spec_platforms, sewers, stairs, flours) == -1:
return True
self.rect.centerx += self.x_speed
if self.check_for_collide(self.x_speed, 0, platforms, coins, mobs, spec_platforms, sewers, stairs, flours) == -1:
return True
def check_for_collide(self, x_speed, y_speed, platforms, coins, mobs, spec_platforms, sewers, stairs, flours):
# Проверка на столкновения с блоками с вопросами
for block in spec_platforms:
if sprite.collide_rect(self, block):
if not self.onGround and self.rect.bottom >= block.rect.top + 10:
if block.rect.left < self.rect.centerx < block.rect.right:
self.y_speed = -1
spec_platforms.remove(block)
else:
get_collide(self, block, x_speed, y_speed)
else:
self.rect.bottom = block.rect.top
self.onGround = True
self.y_speed = 0
# Проверка на столкновения с блоками
for block in platforms:
if sprite.collide_rect(self, block):
get_collide(self, block, x_speed, y_speed)
for block in stairs:
if sprite.collide_rect(self, block):
get_collide(self, block, x_speed, y_speed)
for block in flours:
if sprite.collide_rect(self, block):
get_collide(self, block, x_speed, y_speed)
# Проверка на столкновения с монетами
for coin in coins:
if sprite.collide_rect(self, coin):
self.coins += 1
coins.remove(coin)
# Проверка на столкновения с мобами
for mob in mobs:
if sprite.collide_rect(self, mob):
if not self.onGround and self.rect.bottom <= mob.rect.top + 20:
mob.is_alive = False
mob.rect.height = 15
self.y_speed = -8
else:
if self.lives > 0:
self.lives -= 1
return -1
# Проверка на столкновения с трубами
for sewer in sewers:
if sprite.collide_rect(self, sewer):
get_collide(self, sewer, x_speed, y_speed)
if y_speed > 0:
self.onTube = True
def set_position(self, x: int, y: int):
self.rect.x = x
self.rect.y = y
def set_lives(self, lives: int):
self.lives = lives
def set_x_speed(self, x_speed):
self.x_speed = x_speed
def set_y_speed(self, y_speed):
self.y_speed = y_speed
def get_collide(mario, block, x_speed, y_speed):
if x_speed > 0:
mario.rect.right = block.rect.left
if x_speed < 0:
mario.rect.left = block.rect.right
if y_speed < 0:
mario.rect.top = block.rect.bottom
mario.y_speed = 0
if y_speed > 0:
mario.rect.bottom = block.rect.top
mario.onGround = True
mario.y_speed = 0