-
Notifications
You must be signed in to change notification settings - Fork 0
/
Character.py
213 lines (170 loc) · 8.11 KB
/
Character.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
import pygame
import os
from Entity import Entity
def get_and_load_sprits(foldername, character):
current_path = os.getcwd()
path = os.path.join(current_path, "images", "sprits", character.name, "resized", f"{foldername}", "")
sprits = []
for sprit in os.listdir(path):
sprit = os.path.join(path, f"{sprit}")
sprits.append(pygame.image.load(sprit))
return (sprits, len(sprits))
class Character(Entity):
def __init__(self, name, screen, planet, position):
super().__init__(name, screen, planet, position)
self.walk_speed = 3
self.run_speed = self.walk_speed + 4
self.speed = self.walk_speed
self.jump_power = 10
self.jump_step = self.jump_power # jump_step must take same value with the INITIAL value of jump_power
self.points = 0
self.health = 100
self.controllers = {"jump": pygame.K_UP,
"right": pygame.K_RIGHT,
"left": pygame.K_LEFT,
"run": pygame.K_RSHIFT}
self.is_ = {"right":False, "left":False, "jump":False, "fall":False, "run":False, "dead":False}
self.counts = {"walk":0, "idle":0, "jump":0, "run":0, "dead":0}
self.last_direction = "right"
self.sprits_r = {"Walk":get_and_load_sprits("Walk", self), # "Walk":get_and_load_sprits("Walk", self, (64, 64))
"Run":get_and_load_sprits("Run", self),
"Idle":get_and_load_sprits("Idle", self),
"Jump":get_and_load_sprits("Jump", self),
"Dead":get_and_load_sprits("Dead", self)
}
self.sprits_l = {"Walk":[list(map(lambda img:pygame.transform.flip(img, True, False), self.sprits_r["Walk"][0])), self.sprits_r["Walk"][1]],
"Run":[list(map(lambda img:pygame.transform.flip(img, True, False), self.sprits_r["Run"][0])), self.sprits_r["Run"][1]],
"Idle":[list(map(lambda img:pygame.transform.flip(img, True, False), self.sprits_r["Idle"][0])), self.sprits_r["Idle"][1]],
"Jump":[list(map(lambda img:pygame.transform.flip(img, True, False), self.sprits_r["Jump"][0])), self.sprits_r["Jump"][1]],
"Dead":[list(map(lambda img:pygame.transform.flip(img, True, False), self.sprits_r["Dead"][0])), self.sprits_r["Dead"][1]]
}
self.sprits = self.sprits_r
self.rect = self.sprits_r["Idle"][0][0].get_rect()
# self.ground_y = 200
@property
def collision(self): return pygame.Rect(self.hitbox)
@collision.setter
def collision(self, value): self.collision = value
@property
def hitbox(self): return (self.vec.x + 10, self.vec.y + 5, 45, 60)
@hitbox.setter
def hitbox(self, value): self.hitbox = value
def walkto(self, direction):
if direction == "right" and self.vec.x < self.screen.width - (self.width - 5):
self.vec.x += self.speed
elif direction == "left" and self.vec.x > 0 - 5:
self.vec.x -= self.speed
def jumpto(self):
if self.is_["jump"]:
if self.jump_step >= -self.jump_power:
self.vec.y -= (self.jump_step * abs(self.jump_step)) * 0.33
self.jump_step -= 1
if self.jump_step <= 0:
self.screen.background.scroll(0, int(-self.speed * 2))
#self.is_["fall"] = True
else:
self.screen.background.scroll(0, int(self.speed * 4))
if self.jump_power >= 20 and self.jump_step == -self.jump_power - 1:
self.is_["dead"] = True
else:
self.jump_step = self.jump_power
self.is_["jump"] = False
self.is_["fall"] = False
def draw(self, window):
if not self.visible: return
if self.counts["idle"] + 1 >= self.sprits["Idle"][1] * 4:
self.counts["idle"] = 0
if self.counts["walk"] + 1 >= self.sprits["Walk"][1] * 4:
self.counts["walk"] = 0
if self.counts["jump"] +1 >= self.sprits["Jump"][1] * 4:
self.counts["jump"] = 0
if self.counts["run"] + 1 >= self.sprits["Run"][1] * 4:
self.counts["run"] = 0
if self.counts["dead"] + 1 >= self.sprits["Dead"][1] * 4:
self.counts["dead"] = 0
global main_menu_loop
main_menu_loop = False
if self.is_["dead"]:
window.blit(self.sprits["Dead"][0][self.counts["dead"]//4], (self.vec.x, self.vec.y))
self.counts["dead"] += 1
else:
if self.is_["jump"]:
# self.jumpto()
window.blit(self.sprits["Jump"][0][self.counts["jump"]//4], (self.vec.x, self.vec.y))
if self.is_["right"]:
# self.walkto("right")
if self.is_["run"]:
window.blit(self.sprits["Run"][0][self.counts["run"]//4], (self.vec.x, self.vec.y))
self.counts["run"] += 1
else:
window.blit(self.sprits["Walk"][0][self.counts["walk"]//4], (self.vec.x, self.vec.y))
self.counts["walk"] += 1
elif self.is_["left"]:
# self.walkto("left")
if self.is_["run"]:
window.blit(self.sprits["Run"][0][self.counts["run"]//4], (self.vec.x, self.vec.y))
self.counts["run"] += 1
else:
window.blit(self.sprits["Walk"][0][self.counts["walk"]//4], (self.vec.x, self.vec.y))
self.counts["walk"] += 1
else:
window.blit(self.sprits["Idle"][0][self.counts["idle"]//4], (self.vec.x, self.vec.y))
self.counts["idle"] += 1
pygame.draw.rect(window, (255, 0, 0), self.hitbox, 2)
def keyboard_behaviours(self):
if self.is_["right"] == False and self.is_["left"] == False:
self.last_direction = self.last_direction
else:
if self.is_["right"]:
self.last_direction = "right"
self.sprits = self.sprits_r
elif self.is_["left"]:
self.last_direction = "left"
self.sprits = self.sprits_l
keys = pygame.key.get_pressed()
# for event in pygame.event.get():
# if event.type == pygame.KEYDOWN:
# if event.key == pygame.K_1:
# camera.setmethod(follow)
# elif event.key == pygame.K_2:
# camera.setmethod(auto)
# elif event.key == pygame.K_3:
# camera.setmethod(border)
if keys[self.controllers["right"]]:
self.is_["right"] = True
self.is_["left"] = False
elif keys[self.controllers["left"]]:
self.is_["right"] = False
self.is_["left"] = True
else:
self.is_["right"] = False
self.is_["left"] = False
self.counts["walk"] = 0
if keys[self.controllers["run"]]:
self.speed = self.run_speed
self.is_["run"] = True
else:
self.speed = self.walk_speed
self.is_["run"] = False
self.counts["run"] = 0
if keys[self.controllers["jump"]]:
self.is_["jump"] = True
def action(self):
if self.is_["jump"]:
self.jumpto()
if self.is_["right"]:
if self.screen.width - self.vec.x <= self.screen.width / 12:
self.screen.background.scroll(-self.speed, 0)
else:
self.walkto("right")
# self.walkto(self.last_direction)
# self.screen.background.scroll(-self.speed, 0)
elif self.is_["left"]:
if self.vec.x <= self.screen.width / 12:
self.screen.background.scroll(self.speed, 0)
else:
self.walkto("left")
# self.walkto(self.last_direction)
# self.screen.background.scroll(self.speed, 0)
else:
self.screen.background.scroll(0, 0)