forked from Nutrymaco/mario-2-online-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
166 lines (141 loc) · 7.05 KB
/
player.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pygame import *
from configuration import *
import pyganim
ANIMATION_RIGHT = [('%s/mario/r1.png' % ICON_DIR),
('%s/mario/r2.png' % ICON_DIR),
('%s/mario/r3.png' % ICON_DIR),
('%s/mario/r4.png' % ICON_DIR),
('%s/mario/r5.png' % ICON_DIR)]
ANIMATION_LEFT = [('%s/mario/l1.png' % ICON_DIR),
('%s/mario/l2.png' % ICON_DIR),
('%s/mario/l3.png' % ICON_DIR),
('%s/mario/l4.png' % ICON_DIR),
('%s/mario/l5.png' % ICON_DIR)]
ANIMATION_JUMP_LEFT = [('%s/mario/jl.png' % ICON_DIR, 0.1)]
ANIMATION_JUMP_RIGHT = [('%s/mario/jr.png' % ICON_DIR, 0.1)]
ANIMATION_JUMP = [('%s/mario/j.png' % ICON_DIR, 0.1)]
ANIMATION_STAY = [('%s/mario/0.png' % ICON_DIR, 0.1)]
class PlayerSprite(sprite.Sprite):
def __init__(self, x, y, name=''):
sprite.Sprite.__init__(self)
self.ignore_blocks = False
self.xvel = 0 #скорость перемещения. 0 - стоять на месте
self.startX = x # Начальная позиция Х, пригодится когда будем переигрывать уровень
self.startY = y
self.yvel = 0 # скорость вертикального перемещения
self.onGround = False # На земле ли я?
self.image = Surface((WIDTH,HEIGHT))
self.image.fill(Color(COLOR))
self.rect = Rect(x, y, WIDTH, HEIGHT) # прямоугольный объект
self.image.set_colorkey(Color(COLOR)) # делаем фон прозрачным
# Анимация движения вправо
boltAnim = []
for anim in ANIMATION_RIGHT:
boltAnim.append((anim, ANIMATION_DELAY))
self.boltAnimRight = pyganim.PygAnimation(boltAnim)
self.boltAnimRight.play()
# Анимация движения влево
boltAnim = []
for anim in ANIMATION_LEFT:
boltAnim.append((anim, ANIMATION_DELAY))
self.boltAnimLeft = pyganim.PygAnimation(boltAnim)
self.boltAnimLeft.play()
self.boltAnimStay = pyganim.PygAnimation(ANIMATION_STAY)
self.boltAnimStay.play()
self.boltAnimStay.blit(self.image, (0, 0)) # По-умолчанию, стоим
self.boltAnimJumpLeft = pyganim.PygAnimation(ANIMATION_JUMP_LEFT)
self.boltAnimJumpLeft.play()
self.boltAnimJumpRight = pyganim.PygAnimation(ANIMATION_JUMP_RIGHT)
self.boltAnimJumpRight.play()
self.boltAnimJump= pyganim.PygAnimation(ANIMATION_JUMP)
self.boltAnimJump.play()
self.name = name
self.count_jumps = 0
self.cur_anim_type = "STAY"
def update(self, left, right, up, platforms, boost=False):
if self.onGround:
self.count_jumps = 0
if up:
if self.onGround: # прыгаем, только когда можем оттолкнуться от земли
self.yvel = -JUMP_POWER
self.count_jumps = 1
elif self.count_jumps < 2 and self.yvel >= 0.35:
self.yvel = -JUMP_POWER
self.count_jumps += 1
self.image.fill(Color(COLOR))
self.boltAnimJump.blit(self.image, (0, 0))
self.cur_anim_type = "JUMP"
if left:
if boost:
self.xvel = -1.5 * MOVE_SPEED
else:
self.xvel = -MOVE_SPEED # Лево = x- n
self.image.fill(Color(COLOR))
if up: # для прыжка влево есть отдельная анимация
self.boltAnimJumpLeft.blit(self.image, (0, 0))
self.cur_anim_type = "JUMP_LEFT"
else:
self.boltAnimLeft.blit(self.image, (0, 0))
self.cur_anim_type = "LEFT"
if right:
if boost:
self.xvel = 1.5 * MOVE_SPEED
else:
self.xvel = MOVE_SPEED # Право = x + n
self.image.fill(Color(COLOR))
if up:
self.boltAnimJumpRight.blit(self.image, (0, 0))
self.cur_anim_type = "JUMP_RIGHT"
else:
self.boltAnimRight.blit(self.image, (0, 0))
self.cur_anim_type = "RIGHT"
if not(left or right): # стоим, когда нет указаний идти
self.xvel = 0
if not up:
self.image.fill(Color(COLOR))
self.boltAnimStay.blit(self.image, (0, 0))
self.cur_anim_type = "STAY"
if not self.onGround:
self.yvel += GRAVITY
self.onGround = False # Мы не знаем, когда мы на земле((
self.rect.y += self.yvel
self.collide(0, self.yvel, platforms)
self.rect.x += self.xvel # переносим свои положение на xvel
self.collide(self.xvel, 0, platforms)
def update_animation(self, anim_type):
self.cur_anim_type = anim_type
self.image.fill(Color(COLOR))
if anim_type == "STAY":
self.boltAnimStay.blit(self.image, (0, 0))
elif anim_type == "RIGHT":
self.boltAnimRight.blit(self.image, (0, 0))
elif anim_type == "LEFT":
self.boltAnimLeft.blit(self.image, (0, 0))
elif anim_type == "JUMP":
self.boltAnimJump.blit(self.image, (0, 0))
elif anim_type == "JUMP_RIGHT":
self.boltAnimJumpRight.blit(self.image, (0, 0))
elif anim_type == "JUMP_LEFT":
self.boltAnimJumpLeft.blit(self.image, (0, 0))
else:
raise AttributeError(f"not known anim type: {anim_type}")
def collide(self, xvel, yvel, platforms):
for p in platforms:
if not (self.ignore_blocks and p.is_switchable) and sprite.collide_rect(self, p): # если есть пересечение платформы с игроком
if xvel > 0: # если движется вправо
self.rect.right = p.rect.left # то не движется вправо
if xvel < 0: # если движется влево
self.rect.left = p.rect.right # то не движется влево
if yvel > 0: # если падает вниз
self.rect.bottom = p.rect.top # то не падает вниз
self.onGround = True # и становится на что-то твердое
self.yvel = 0 # и энергия падения пропадает
if yvel < 0: # если движется вверх
self.rect.top = p.rect.bottom # то не движется вверх
self.yvel = 0 # и энергия прыжка пропадает
class PlayerInfo:
def __init__(self, name, sprite):
self.name = name
self.sprite = sprite