-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_state.py
313 lines (245 loc) · 8.08 KB
/
main_state.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import random
from pico2d import *
import bakground_bg
import Player_item
import monsterpy
import game_framework
import title_state
import hold_state
import end_state
import json
import boss
name = "MainState"
running = None
monster=[]
monster_arrow=[]
player_arrow=[]
items=[]
boss_arrow=[]
def collide(a, b):
left_a,top_a , right_a, bottom_a = a.get_hitbox()
left_b, top_b, right_b, bottom_b= b.get_hitbox()
if left_a > right_b: return False
if right_a < left_b: return False
if top_a < bottom_b: return False
if bottom_a > top_b: return False
return True
def enter():
game_framework.reset_time()
#open_canvas(800,600)
global player,font,background,running,stage,total_time,creating,create_next_time,score,monster_number,next_time
stage=1
font=load_font('./resource/ENCR10B.TTF')
background=bakground_bg.BackGround()
player=Player_item.Player()
score=0
running = True;
total_time=0.0
creating=False
create_time=0.0
monster_number=2
create_next_time=0.0
pass
def exit():
global player,font,background
# del(font)
#close_canvas()
pass
def pause():
pass
def resume():
pass
def handle_events(frame_time):
global player,total_time,stage
events = get_events()
for event in events:
if event.type == SDL_QUIT:
game_framework.quit()
elif event.type == SDL_KEYDOWN and event.key == SDLK_ESCAPE:
game_framework.quit()
elif (event.type, event.key) == (SDL_KEYDOWN, SDLK_h):
game_framework.push_state(hold_state)
elif event.type == SDL_KEYDOWN and event.key == SDLK_F2:
total_time=148
elif event.type == SDL_KEYDOWN and event.key == SDLK_F1:
total_time=58
else:
player.handle_event(event,stage);
def collide(a, b):
left_a,top_a , right_a, bottom_a = a.get_hitbox()
left_b, top_b, right_b, bottom_b= b.get_hitbox()
if left_a > right_b: return False
if right_a < left_b: return False
if top_a < bottom_b: return False
if bottom_a > top_b: return False
return True
def update(frame_time):
global current_time,creating,score,create_next_time,background,monster_number,stage,total_time,bossi,player
# handle_events(frame_time)
# frame_time = get_time() - current_time
total_time+= frame_time
if stage==1 or stage==2:
for monsteri in monster:
for p_arrow in player_arrow:
if collide(p_arrow,monsteri):
monsteri.hp-=1;
score+=100
player_arrow.remove(p_arrow)
if monsteri.hp==0:
if random.randint(0,2)==0:
items.append(Player_item.Item(monsteri.x,monsteri.y))
# print("item creat")
monster.remove(monsteri)
if collide(player,monsteri):
monsteri.hp-=1
player.hp-=1
#print("crash")
if monsteri.detecting(player) and monsteri.sense==False:
monsteri.sense=True
monsteri.change_state()
elif monsteri.detecting(player)==False and monsteri.sense==True:
monsteri.sense=False
monsteri.change_state()
for arrow in monster_arrow:
if collide(player,arrow):
monster_arrow.remove(arrow)
player.hp-=1;
# print("mon aro crash")
if total_time>= 100:
create_time=0.5
elif total_time>= 80:
create_time=0.8
elif total_time>= 60:
create_time=1.2
elif total_time>= 40:
create_time=0.8
elif total_time>= 20:
create_time=1.0
elif total_time>= 0:
create_time=1.5
if total_time%create_time<0.05 and creating==False:
creating=True
temp=random.randint(1,monster_number)
if temp==1:
monster.append(monsterpy.Monster_Lion())
elif temp==2:
monster.append(monsterpy.Monster_Sheep())
else:
monster.append(monsterpy.Monster_Ghost())
create_next_time=total_time+create_time
if total_time>create_next_time:
creating=False
for item in items:
if item.x<-100:
items.remove(item)
# clear_canvas()
for monsteri in monster:
monsteri.update(frame_time)
for arrow in monster_arrow:
arrow.update(frame_time)
if arrow.x<-150:
monster_arrow.remove(arrow)
#print("arrow remove")
for p_arrow in player_arrow:
p_arrow.update(frame_time)
if p_arrow.x>900 or p_arrow.x<-50:
player_arrow.remove(p_arrow)
for item in items:
if collide(player,item):
#item.eat()
if item.item_num<3:
score+=20
elif item.item_num==3:
player.skill_gauge+=1
elif item.item_num==4:
player.hp+=2
items.remove(item)
#background.bgm_call(stage)
else:
item.update(frame_time)
background.update(frame_time,stage)
player.update(frame_time,stage)
if total_time>=40 and stage==1:
background.potal=True
if player.x>background.potal_x:
stage=2
background.bgm2.repeat_play()
#game_framework.reset_time()
player.hp=10
player.skill_gauge=2
creating=False
create_time=0.0
monster_number=3
create_next_time=0.0
for monsteri in monster:
monster.remove(monsteri)
#reset_time()
if total_time>=80 and stage==2:
background.potal=True
if player.x>background.potal_x:
background.potal=False
stage=3
background.bgm3.repeat_play()
player.hp=10
player.skill_gauge=0
create_time=5.0
creating=False
for monsteri in monster:
monster.remove(monsteri)
bossi=boss.Boss()
next_time=0
if stage==3:
global next_time
create_time=5.0
bossi.update(frame_time,player.x,player.y)
if total_time>=next_time :
if collide(player,bossi):
bossi.hp-=1
player.hp-=2
#print("crash")
next_time=total_time+2
for p_arrow in player_arrow:
if collide(p_arrow,bossi):
bossi.hp-=1;
score+=100
player_arrow.remove(p_arrow)
for boss_a in boss_arrow:
boss_a.update(frame_time)
if total_time%create_time<0.05 and creating==False:
creating=True
temp=random.randint(1,5)
if temp==1:
bossi.state=0
elif temp==2:
bossi.state=1
elif temp==3:
bossi.state=3
create_next_time=total_time+create_time
if total_time>create_next_time:
creating=False
if bossi.hp==0:
game_framework.change_state(end_state)
global player,bossi
if player.hp<=0 :
game_framework.change_state(end_state)
#update_canvas()
def draw(frame_time):
clear_canvas()
background.draw(stage)
if stage!=3:
for monster_a in monster_arrow:
monster_a.draw()
for monsteri in monster:
monsteri.draw()
else:
bossi.draw()
for monsteri in boss_arrow:
monsteri.draw()
for p_a in player_arrow:
p_a.draw()
for item in items:
item.draw()
player.draw()
font.draw(50,50,'score: %d time: %f'%(score,total_time))
update_canvas()
pass