-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter.py
131 lines (104 loc) · 3.75 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
# Scripts:
# Modules:
import pygame
class Character(pygame.sprite.Sprite):
def __init__(self, category, img_path, scale, vel, hp, fire_rate=None, explo_scale=None, part_range=None, bullet_group=None, effects_group=None):
super().__init__()
# Image:
self.category = category
self.img_path = img_path
self.image = pygame.image.load(f'{self.img_path}{self.category}.png').convert_alpha()
self.scale = scale
self.image = pygame.transform.scale(self.image, (self.image.get_width() * self.scale, self.image.get_height() * self.scale))
self.image_copy = self.image
self.rect = self.image.get_rect()
# Initial Movement Animation:
self.start_animation = None
# Final Movement Animation:
self.end_animation = False
# Movement:
self.vel = self.vel_x, self.vel_y = vel
self.counter = 0
# Rotation:
self.angle = 0
# HP:
self.hp = hp
# State:
self.invulnerable = False
self.invulnerable_ref_time = 120
self.invulnerable_rate = 120
# Blink:
self.empty_surface = pygame.Surface(self.image.get_size(), pygame.SRCALPHA)
self.blinks = False
self.blink_ref_time = self.invulnerable_ref_time / 20
self.blink_rate = 0
# Bullet:
self.bullet_group = bullet_group
self.ref_time = fire_rate
self.fire_rate = fire_rate
# Explosion:
self.effects_group = effects_group
self.explosion_scale = explo_scale
if part_range is not None:
self.part_min, self.part_max = part_range
# Others:
self.keyboard_blocked = False
def move_x(self):
self.rect.x += self.vel_x
def move_y(self):
self.rect.y += self.vel_y
def restrict_x(self, left_limit, right_limit):
if self.rect.left <= left_limit:
self.vel_x = self.vel_x * -1
elif self.rect.right >= right_limit:
self.vel_x = self.vel_x * -1
def restrict_x_counter(self, limit):
self.move_x()
self.counter += 1
if self.counter == limit:
self.vel_x = self.vel_x * -1
self.counter = 0
def rotate(self):
rotated_surface = pygame.transform.rotozoom(self.image_copy, self.angle, 1)
rotated_rect = rotated_surface.get_rect(center=self.rect.center)
return rotated_surface, rotated_rect
def spawn_bullet(self):
pass
def get_hit(self, pos, col_type):
pass
def destroy(self):
pass
def blink_image(self):
if self.blink_rate < self.blink_ref_time:
self.image = self.empty_surface
elif self.blink_ref_time <= self.blink_rate < 2 * self.blink_ref_time:
self.image = pygame.image.load(f'{self.img_path}{self.category}.png').convert_alpha()
else:
self.blink_rate = 0
def animate_start(self):
pass
def animate_end(self):
pass
def handle_action(self):
pass
def make_invulnerable(self):
if self.invulnerable:
if self.invulnerable_rate >= self.invulnerable_ref_time:
self.invulnerable = False
self.image = pygame.image.load(f'{self.img_path}{self.category}.png').convert_alpha()
else:
self.invulnerable_rate += 1
if self.blinks:
self.blink_rate += 1
self.blink_image()
def reset_variables(self):
pass
def update(self):
if self.start_animation:
self.animate_start()
elif self.end_animation:
self.animate_end()
else:
self.handle_action()
self.make_invulnerable()
self.reset_variables()