-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
160 lines (135 loc) · 5.74 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
import pygame
from settings import *
from support import import_folder
from entity import Entity
class Player(Entity):
def __init__(self, pos, group, obstacle_sprites, create_attack, destroy_weapon, create_magic):
super().__init__(group)
self.image = pygame.image.load('player1.png').convert_alpha()
self.rect = self.image.get_rect(topleft=pos)
self.offset_visual = self.rect.inflate(0, -20)
self.import_player_assets()
self.state = 'down'
self.attacking = False
self.attack_cooldown = 400
self.attack_time = None
self.create_attack = create_attack
self.remedy_index = 0
self.weapon = list(weapons.keys())[self.remedy_index]
self.destroy_weapon = destroy_weapon
self.can_switch_weapon = True
self.weapon_switch_time = None
self.switch_duration_cooldown = 150
self.magic_index = 0
self.magic = list(magic_tools.keys())[self.magic_index]
self.can_switch_magic = True
self.magic_switch_time = None
self.create_magic = create_magic
self.obstacle_sprites = obstacle_sprites
self.stats = {'health': 100, 'energy': 50, 'healing_power': 10, 'magic': 5, 'speed': 5}
self.health = self.stats['health'] * 0.5
self.energy = self.stats['energy'] * 0.8
self.speed = self.stats['speed']
self.exp = 100
def import_player_assets(self):
char_path = './graphics/player/'
self.animations = {'up': [], 'down': [], 'left': [], 'right': [],
'right_idle': [], 'left_idle': [], 'up_idle': [], 'down_idle': [],
'right_attack': [], 'left_attack': [], 'up_attack': [], 'down_attack': []}
for animation in self.animations.keys():
full_path = char_path + animation
self.animations[animation] = import_folder(full_path)
#print(self.animations)
def input(self):
if not self.attacking:
keys = pygame.key.get_pressed()
# move
if keys[pygame.K_UP]:
self.direction.y = -1
self.state = 'up'
elif keys[pygame.K_DOWN]:
self.direction.y = 1
self.state = 'down'
else:
self.direction.y = 0
if keys[pygame.K_LEFT]:
self.direction.x = -1
self.state = 'left'
elif keys[pygame.K_RIGHT]:
self.direction.x = 1
self.state = 'right'
else:
self.direction.x = 0
# attack
if keys[pygame.K_SPACE]:
self.attacking = True
self.attack_time = pygame.time.get_ticks()
self.create_attack()
#print('attack')
# use magic
if keys[pygame.K_LCTRL]:
self.attacking = True
self.attack_time = pygame.time.get_ticks()
style = list(magic_tools.keys())[self.magic_index]
strength = list(magic_tools.vales())['strength'] + self.stats['magic']
cost = list(magic_tools.vales())['cost']
self.create_magic(style, strength, cost)
#print('magic')
if keys[pygame.K_q] and self.can_switch_weapon:
self.can_switch_weapon = False
self.weapon_switch_time = pygame.time.get_ticks()
if self.remedy_index < len(list(weapons.keys()))-1:
self.remedy_index += 1
else:
self.remedy_index = 0
self.weapon = list(weapons.keys())[self.remedy_index]
if keys[pygame.K_e] and self.can_switch_magic:
self.can_switch_magic = False
self.magic_switch_time = pygame.time.get_ticks()
if self.magic_index < len(list(magic_tools.keys()))-1:
self.magic_index += 1
else:
self.magic_index = 0
self.magic = list(magic_tools.keys())[self.magic_index]
def get_state(self):
# idle
if self.direction.x == 0 and self.direction.y == 0:
if not 'idle' in self.state and not 'attack' in self.state:
self.state = self.state + '_idle'
if self.attacking:
self.direction.x = 0
self.direction.y = 0
if not 'attack' in self.state:
if 'idle' in self.state:
self.state = self.state.replace('_idle', '_attack')
else:
self.state = self.state + '_attack'
else:
if 'attack' in self.state:
self.state = self.state.replace('_attack', '')
def cooldowns(self):
current_time = pygame.time.get_ticks()
if self.attacking:
if current_time - self.attack_time >= self.attack_cooldown:
self.attacking = False
self.destroy_weapon()
if not self.can_switch_weapon:
if current_time - self.weapon_switch_time >= self.switch_duration_cooldown:
self.can_switch_weapon = True
if not self.can_switch_magic:
if current_time - self.magic_switch_time >= self.switch_duration_cooldown:
self.can_switch_magic = True
def animate(self):
animation = self.animations[self.state]
# play animation
self.frame_index += self.anim_speed
if self.frame_index >= len(animation):
self.frame_index = 0
self.image = animation[int(self.frame_index)]
self.rect = self.image.get_rect(center=self.offset_visual.center)
def update(self):
self.input()
self.cooldowns()
self.get_state()
self.animate()
self.move(self.speed)