-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfighter.py
185 lines (177 loc) · 7.36 KB
/
fighter.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
import pygame
import time
class Fighter():
def __init__(self, player, x, y, flip, fighter_data,sprite_sheet, animation_steps, sound):
self.player = player
self.size = fighter_data[0]
self.image_scale = fighter_data[1]
self.offset = fighter_data[2]
self.flip = flip
self.animation_list = self.load_images(sprite_sheet,animation_steps)
self.action = 0 #0idle 1:run 2:jump 3:attack1 4:attack2 5:death
self.frame_index = 0
self.image = self.animation_list[self.action][self.frame_index]
self.update_time = pygame.time.get_ticks()
self.rect=pygame.Rect((x,y,80,180))
self.vel_y = 0
self.running = False
self.jump = False
self.attacking = False
self.attack_type = 0
self.attack_cooldown = 0
self.attack_sound = sound
self.hit = False
self.health = 100
self.alive = True
def load_images(self, sprite_sheet, animation_steps):
# exract images
animation_list = []
for y, animation in enumerate(animation_steps):
temp_img_list = []
for x in range(animation):
temp_img = sprite_sheet.subsurface(x * self.size, y * self.size, self.size, self.size)
temp_img_list.append(pygame.transform.scale(temp_img,(self.size * self.image_scale, self.size * self.image_scale)))
animation_list.append(temp_img_list)
return animation_list
def move(self,width,height,surface,target, round_over):
SPEED = 10
GRAVITY = 2
dx = 0
dy = 0
self.running = False
self.attack_type = 0
key = pygame.key.get_pressed()
# if not attacking then performs other action
if self.attacking==False and self.alive == True and round_over == False:
# check the player 1 controls
if self.player == 1:
# movement
if key[pygame.K_a]:
dx = -SPEED
self.running = True
if key[pygame.K_d]:
dx = SPEED
self.running = True
# jump
if key[pygame.K_w] and self.jump == False:
self.vel_y = -30
self.jump = True
# Attack
if key[pygame.K_r] or key[pygame.K_t]:
self.attack(surface,target)
# determine the attack type
if key[pygame.K_r]:
self.attack_type = 1
if key[pygame.K_t]:
self.attack_type = 2
# check the player 2 controls
elif self.player == 2:
# movement
if key[pygame.K_LEFT]:
dx = -SPEED
self.running = True
if key[pygame.K_RIGHT]:
dx = SPEED
self.running = True
# jump
if key[pygame.K_UP] and self.jump == False:
self.vel_y = -30
self.jump = True
# Attack
if key[pygame.K_KP1] or key[pygame.K_KP2]:
self.attack(surface,target)
# determine the attack type
if key[pygame.K_KP1]:
self.attack_type = 1
if key[pygame.K_KP2]:
self.attack_type = 2
# -----------------------------------------------player or gravity Hndler
# apply gravity
self.vel_y += GRAVITY
dy += self.vel_y
# check player stays on screen
if self.rect.left+dx < 0:
dx = -self.rect.left
if self.rect.right+dx > width:
dx = width-self.rect.right
if self.rect.bottom+dx > height-110:
self.vel_y = -1
self.jump = False
dy = height-110 - self.rect.bottom
#------------------------------------------ensure player face each other
if target.rect.centerx > self.rect.centerx:
self.flip = False
else:
self.flip = True
# attack cooldown
if self.attack_cooldown > 0:
self.attack_cooldown = 0
# update player position
self.rect.x += dx
self.rect.y += dy
# handle update mathod
def update(self):
# check what action performing
if self.health <= 0:
self.health = 0
self.alive = False
self.update_action(6) # 6:death
elif self.hit == True:
self.update_action(5) # 5:hit
elif self.attacking == True:
if self.attack_type == 1:
self.update_action(3) # 3:attack1
elif self.attack_type == 2:
self.update_action(4) # 4:attack2
elif self.jump == True:
self.update_action(2) # 2:jump
elif self.running == True:
self.update_action(1) # 1:run
else:
self.update_action(0) # 0:normal
animation_cooldown = 50
# update image
self.image = self.animation_list[self.action][self.frame_index]
# check if enough time has passed since the last update
if pygame.time.get_ticks() - self.update_time > animation_cooldown:
self.frame_index += 1
self.update_time - pygame.time.get_ticks()
# check if the animation has finished
if self.frame_index >= len(self.animation_list[self.action]):
# check the player dead then end of the animation
if self.alive == False:
self.frame_index = len(self.animation_list[self.action]) - 1
else:
self.frame_index = 0
# check if an attack was executed
if self.action == 3 or self.action == 4:
self.attacking = False
self.attack_cooldown = 50
# damage taken animation reset
if self.action == 5:
self.hit = False
# if player was in the middle of an attack, then the attack is stopped
self.attacking = False
self.attack_cooldown = 20
# --------------------------------------------------Attack Function
def attack(self,surface,target):
if self.attack_cooldown == 0:
self.attacking=True
self.attack_sound.play()
attack_rect = pygame.Rect(self.rect.centerx - (2*self.rect.width*self.flip),self.rect.y, 2 * self.rect.width, self.rect.height)
# if attack_rect touches to other rectangles
if attack_rect.colliderect(target.rect):
target.health -= 10
target.hit = True
# pygame.draw.rect(surface,(0,255,0), attack_rect)
def update_action(self,new_action):
# check if the new action is different to the previous one
if new_action != self.action:
self.action = new_action
# update the animation setting
self.frame_index = 0
self.update_time = pygame.time.get_ticks()
def draw(self,surface):
img= pygame.transform.flip(self.image, self.flip, False)
# pygame.draw.rect(surface,(255,0,0),self.rect)
surface.blit(img, (self.rect.x - (self.offset[0] * self.image_scale), self.rect.y - (self.offset[1] * self.image_scale)))