-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.py
173 lines (134 loc) · 4.8 KB
/
classes.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
import pygame
from pygame.sprite import Sprite
from enum import Enum
import os
import time
class CharacterStates(Enum):
IDLE = 0
MOVE = 1
ATTACK = 2
DEAD = 3
DEFENCE = 4
JUMP = 5
class GameObjectTypes(Enum):
STATIC_OBJECT = 0
MOVING_OBJECT = 1
ENEMY = 2
DARK_ANGEL = 3
OTHER = 4
class Position():
def __init__(self,x,y):
self.x = x
self.y = y
class GameObject(Sprite):
def __init__(self, x,y, name="noname"):
Sprite.__init__(self)
self.pos = Position(x,y)
self.name = name
self.current_frame_set = None
self.frames = [] #
self.current_frame = 0 #
self.image = None #
self.rect = None #
self.hidden = False
self.type = GameObjectTypes.OTHER
self.f_elapsed_time = 0.0
self.f_delay_time = 0.15
def load_frames(self):
pass
def update_frame(self,current_time):
pass
class GameCharacter(GameObject):
def __init__(self, x,y, name="noname"):
super().__init__(x,y,name)
self.name+=name
self.health = 10
self.cur_health = 10
self.type = GameObjectTypes.DARK_ANGEL
self.current_state = CharacterStates.IDLE
class CharachterDarkAngel(GameCharacter):
def __init__(self, x,y, name="Angle1"):
super().__init__(x,y,name)
self.name+=name
self.type = GameObjectTypes.DARK_ANGEL
self.frames_running = []
self.frames_idle = []
self.f_delay_time =0.07
self.current_frame_sequence = None
self.frames_running = load_sequence(os.path.normpath("data/FallenAngel/Running/0_Fallen_Angels_Running_"), "png", 12, 3, 0)
self.current_frame_sequence = self.frames_running
self.image = self.current_frame_sequence[self.current_frame]
def update_frame(self,current_time):
if current_time - self.f_elapsed_time >= self.f_delay_time:
if self.current_frame<len(self.current_frame_sequence)-1:
self.current_frame+=1
else:
self.current_frame = 0
self.image = self.current_frame_sequence[self.current_frame]
self.f_elapsed_time = current_time
print(self.current_frame,len(self.current_frame_sequence),self.f_elapsed_time,current_time)
class Render:
def __init__(self, width, height, caption, game_object_list):
# initialize the pygame module
pygame.init()
pygame.display.set_caption(caption)
self.player = None
self.game_object_list = game_object_list
# create a surface on screen that has the size of width x height
self.screen = pygame.display.set_mode((width,height))
pygame.display.flip()
def update_screen(self):
cur_time = time.time()
self.screen.fill([0,0,0])
#game objects
for i in self.game_object_list:
i.update_frame(time.time())
self.screen.blit(i.image,(i.pos.x,i.pos.y))
pygame.display.flip()
class GameLogic():
def __init__(self):
self.game_object_list = []
def create_game_object(self,type, x,y, name ="noname"):
if type == GameObjectTypes.DARK_ANGEL:
DarkAngel = CharachterDarkAngel(x,y,name);
self.game_object_list.append(DarkAngel)
def load_sequence(basename, ext, num, num_digits=1, offset=0,optimize=True):
frames = []
# format string basename+zero_padded_number+.+ext
format = "%s%0"+str(num_digits)+"d.%s"
for i in range(offset, num):
print("df")
name = format % (basename, i,ext);
print(name)
image = pygame.image.load(name) # not optimized
image = pygame.transform.scale(image, (200, 200))
if optimize:
if image.get_alpha() is not None:
image = image.convert_alpha()
else:
image = image.convert()
frames.append(image)
return frames
def main():
# initialize
print("...is loading")
AGameLogic = GameLogic()
AGameRender = Render(800,600,"Arcade_P1",AGameLogic.game_object_list)
AGameLogic.create_game_object(GameObjectTypes.DARK_ANGEL,0,0, "My Angel")
AGameLogic.create_game_object(GameObjectTypes.DARK_ANGEL,300,0, "My Angel")
# DarkAngel = CharachterDarkAngel();
#main loop
running = True
elapsed_time = 0
while (running):
AGameRender.update_screen()
# event handling, gets all event from the eventqueue
for event in pygame.event.get():
# only do something if the event if of type QUIT
if event.type == pygame.QUIT:
# change the value to False, to exit the main loop
running = False
#graphic update
if __name__=="__main__":
# call the main function
main()