-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_state.py
285 lines (237 loc) · 8.23 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
from pico2d import *
import random
import json
import os
import game_framework
import game_world
import game_over_state
import pause_state
import main_state_2
import clear_game_state
from Boy import Boy
from hp_bar import HP_BAR
from ground import Ground
from flatform import Flatform
#from back_ground import Background
from back_ground import FixedBackground as Background
from spike import Spike
from monster_bear import Monster_bear
from monster_mage import Monster_mage
from chest import Chest
from heat_box import Heat_box
from gui import Gui
from flatform import Flatform
from flag import Flag
name = "MainState"
boy = None
ground = []
FlatForm = None
background = None
Hp_Bar = None
Monster_Bear_List = []
Monster_Mage_List = []
ChestList = []
spikeList = []
font = None
GUI = None
FlatFormList = []
FLAG = None
def get_boy():
return boy
def get_ground():
return ground
def get_flatform():
return FlatFormList
def get_Bground():
return background
def get_Monster_Bear_List():
return Monster_Bear_List
def get_Monster_Mage_List():
return Monster_Mage_List
def enter():
global boy, ground, background, spikeList, Hp_Bar, Monster_Bear_List, Monster_Mage_List, ChestList, CHicken,\
GUI, FlatFormList, FLAG
background = Background()
boy = Boy(background)
Hp_Bar = HP_BAR()
ground = [Ground(i, background) for i in range(len(Ground.groundList))]
Monster_Bear_List = [Monster_bear(i, background) for i in range(len(Monster_bear.posList))]
Monster_Mage_List = [Monster_mage(i) for i in range(len(Monster_mage.posList))]
spikeList = [Spike(i, background) for i in range(len(Spike.spikeList))]
ChestList = [Chest(i, background) for i in range(len(Chest.chestList))]
GUI = Gui()
FlatFormList = [Flatform(i, background) for i in range(len(Flatform.flatFormList))]
FLAG = Flag(background)
game_world.add_object(background, 0)
for i in range(len(ground)):
game_world.add_object(ground[i], 0)
game_world.add_object(boy, 2)
game_world.add_object(Hp_Bar, 1)
for i in range(len(Monster_Bear_List)):
game_world.add_object(Monster_Bear_List[i], 1)
for i in range(len(Monster_Mage_List)):
game_world.add_object(Monster_Mage_List[i], 1)
for i in range(len(ChestList)):
game_world.add_object(ChestList[i], 1)
for i in range(len(spikeList)):
game_world.add_object(spikeList[i], 1)
game_world.add_object(GUI, 1)
for i in range(len(FlatFormList)):
game_world.add_object((FlatFormList[i]), 1)
game_world.add_object(FLAG, 1)
background.set_center_object(boy)
boy.set_background(background)
def get_ground():
return ground
def exit():
game_world.clear()
def pause():
pass
def resume():
pass
def handle_events():
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 == SDL_KEYDOWN and event.key == SDLK_p:
game_framework.push_state(pause_state)
elif event.type == SDL_KEYDOWN and event.key == SDLK_x:
boy.onJump()
elif event.type == SDL_KEYDOWN and event.key == SDLK_c:
boy.act_dash()
elif event.type == SDL_KEYDOWN and event.key == SDLK_q:
boy.drinkPotion()
elif event.type == SDL_KEYDOWN and event.key == SDLK_a:
boy.spellLight()
else:
boy.handle_event(event)
if event.type == SDL_KEYDOWN and event.key == SDLK_s:
boy.spellOnFire()
elif event.type == SDL_KEYUP and event.key == SDLK_s:
boy.spellOffFire()
if event.type == SDL_KEYDOWN and event.key == SDLK_2:
boy.openCure()
elif event.type == SDL_KEYUP and event.key == SDLK_2:
boy.closeCure()
for i in range(len(ChestList)):
if collide(boy, ChestList[i]):
if event.type == SDL_KEYDOWN and event.key == SDLK_UP:
ChestList[i].open()
if boy.HP <= 0:
game_framework.change_state(game_over_state)
def collide(a, b):
left_a, bottom_a, right_a, top_a = a.get_bb()
left_b, bottom_b, right_b, top_b = b.get_bb()
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 collide_flatform(a, b):
left_a, bottom_a, right_a, top_a = a.get_flatform_bb()
left_b, bottom_b, right_b, top_b = b.get_bb()
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():
for game_object in game_world.all_objects():
game_object.update()
for i in range(len(Monster_Mage_List)):
if collide(boy, Monster_Mage_List[i]):
boy.damaged(1)
for i in range(len(Monster_Bear_List)):
if collide(boy, Monster_Bear_List[i]):
boy.damaged(3)
# --------------------플랫폼 충돌---------------------------
if collide_flatform(boy, FlatFormList[0]):
boy.flatform_stop()
boy.workingJump = True
elif collide_flatform(boy, FlatFormList[1]):
boy.flatform_stop()
boy.workingJump = True
elif collide_flatform(boy, FlatFormList[2]):
boy.flatform_stop()
boy.workingJump = True
elif collide_flatform(boy, FlatFormList[3]):
boy.flatform_stop()
boy.workingJump = True
elif collide_flatform(boy, FlatFormList[4]):
boy.flatform_stop()
boy.workingJump = True
elif collide_flatform(boy, FlatFormList[5]):
boy.flatform_stop()
boy.workingJump = True
elif collide_flatform(boy, FlatFormList[6]):
boy.flatform_stop()
boy.workingJump = True
elif collide_flatform(boy, FlatFormList[7]):
boy.flatform_stop()
boy.workingJump = True
elif collide_flatform(boy, FlatFormList[8]):
boy.flatform_stop()
boy.workingJump = True
elif collide_flatform(boy, FlatFormList[9]):
boy.flatform_stop()
boy.workingJump = True
elif collide_flatform(boy, FlatFormList[10]):
boy.flatform_stop()
boy.workingJump = True
elif collide_flatform(boy, FlatFormList[11]):
boy.flatform_stop()
boy.workingJump = True
elif collide_flatform(boy, FlatFormList[12]):
boy.flatform_stop()
boy.workingJump = True
elif collide_flatform(boy, FlatFormList[13]):
boy.flatform_stop()
boy.workingJump = True
elif collide_flatform(boy, FlatFormList[14]):
boy.flatform_stop()
boy.workingJump = True
elif collide_flatform(boy, FlatFormList[15]):
boy.flatform_stop()
boy.workingJump = True
elif collide_flatform(boy, FlatFormList[16]):
boy.flatform_stop()
boy.workingJump = True
elif collide_flatform(boy, FlatFormList[17]):
boy.flatform_stop()
boy.workingJump = True
elif collide_flatform(boy, FlatFormList[18]):
boy.flatform_stop()
boy.workingJump = True
elif collide_flatform(boy, FlatFormList[19]):
boy.flatform_stop()
boy.workingJump = True
elif collide_flatform(boy, FlatFormList[20]):
boy.flatform_stop()
boy.workingJump = True
elif collide_flatform(boy, FlatFormList[21]):
boy.flatform_stop()
boy.workingJump = True
elif collide_flatform(boy, FlatFormList[22]):
boy.flatform_stop()
boy.workingJump = True
else:
boy.onFlatform = False
for i in range(len(spikeList)):
if collide(boy, spikeList[i]):
boy.damaged(5)
Monster_Bear_List[0].x = clamp(0, Monster_Bear_List[0].x, 1000)
Monster_Bear_List[1].x = clamp(0, Monster_Bear_List[1].x, 1000)
Monster_Bear_List[2].x = clamp(0, Monster_Bear_List[2].x, 1000)
Monster_Bear_List[3].x = clamp(1050, Monster_Bear_List[3].x, 1950)
Monster_Bear_List[4].x = clamp(1050, Monster_Bear_List[4].x, 1950)
if collide(boy, FLAG):
game_framework.change_state(clear_game_state)
Hp_Bar.trueUpdate(boy.HP)
def draw():
clear_canvas()
for game_object in game_world.all_objects():
game_object.draw()
update_canvas()