This repository has been archived by the owner on Jul 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame_loop.py
432 lines (357 loc) · 16.4 KB
/
game_loop.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 19 16:01:18 2021
@author:
"""
import json
import random
import time
import pygame.locals
from BackGroundMusic import BackGroundMusic
from Background import background_surf_init, Floor, wallBysize
from Character import Player, Bullet, Enemy, Weapon, Text
from Character import character_surf_initialize, get_update_direction
from UI_display import UI_display
with open("Json/config.json") as f:
data = json.load(f)
pygame.init()
displayInfo = pygame.display.Info()
screensize = data["preferresolution"]
SCREEN_WIDTH = screensize[0: screensize.index("x") - 1]
SCREEN_WIDTH = int(SCREEN_WIDTH)
SCREEN_HEIGHT = screensize[screensize.index("x") + 2:]
SCREEN_HEIGHT = int(SCREEN_HEIGHT)
TITLE = "Unnamed RPG"
class gameEnv:
def __init__(self, config):
# self.screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT), pygame.FULLSCREEN)
pygame.event.set_allowed([pygame.locals.KEYDOWN, pygame.locals.QUIT])
if data["windowed"]:
self.screen = pygame.display.set_mode(
[SCREEN_WIDTH, SCREEN_HEIGHT], pygame.DOUBLEBUF
)
else:
self.screen = pygame.display.set_mode(
(SCREEN_WIDTH, SCREEN_HEIGHT), pygame.FULLSCREEN | pygame.DOUBLEBUF
)
pygame.display.set_caption(TITLE)
# print(data['windowed'])
self.clock = pygame.time.Clock()
pygame.font.init()
self.font = pygame.font.Font("assets/fonts/OCRAEXT.TTF", 16)
self.music = BackGroundMusic("assets/music/backgroundMusic.mp3", -1)
# self.fps = int(data['fps'])
self.fps = 60
character_surf_initialize()
background_surf_init()
self.bullet_cd = time.time() - 10
self.dps_clock = time.time()
self.wall = pygame.sprite.Group()
self.floor = pygame.sprite.Group()
self.bullet = pygame.sprite.Group()
self.enemy = pygame.sprite.Group()
self.situation_text = pygame.sprite.Group()
self.player_group = pygame.sprite.Group()
self.all_sprite = pygame.sprite.Group()
for x in range(-SCREEN_WIDTH, SCREEN_WIDTH, SCREEN_WIDTH):
for y in range(-SCREEN_HEIGHT, SCREEN_HEIGHT, SCREEN_HEIGHT):
summon_floor = Floor([x, y])
self.floor.add(summon_floor)
self.all_sprite.add(summon_floor)
self.player = Player()
self.player_group.add(self.player)
self.all_sprite.add(self.player)
self.player.set_profession(config["profession"])
self.weapon = Weapon()
self.weapon.set_weapon(self.player.weapon)
self.att_text = Text(self.font, "att:", (5, 5))
self.dps_text = Text(self.font, "dps:", (5, 20))
self.enemy_left_text = Text(self.font, "Enemy Left:", (5, 35))
self.resolution_text = Text(self.font, "resolution:", (5, 50))
self.fps_text = Text(self.font, "fps:", (5, 65))
self.situation_text.add(self.att_text)
self.situation_text.add(self.dps_text)
self.situation_text.add(self.enemy_left_text)
self.situation_text.add(self.resolution_text)
self.resolution_text.update(
"resolution:" + str(SCREEN_WIDTH) + "x" + str(SCREEN_HEIGHT)
)
self.situation_text.add(self.fps_text)
self.fps_text.update("fps:" + str(self.fps))
# player_situation
self.player_text = Text(self.font, "player:", (SCREEN_WIDTH - 120, 5))
self.player_mp_text = Text(self.font, "mp:", (SCREEN_WIDTH - 120, 20))
self.player_hp_text = Text(self.font, "hp:", (SCREEN_WIDTH - 120, 35))
self.player_weapon_text = Text(self.font, "weapon:", (SCREEN_WIDTH - 120, 50))
self.situation_text.add(self.player_text)
self.situation_text.add(self.player_mp_text)
self.situation_text.add(self.player_hp_text)
self.situation_text.add(self.player_weapon_text)
groups = (self.all_sprite, self.wall)
wallBysize(groups, (96, 68), (-1920, -1080))
# wallGenerater('room', groups, (1000, 0))
detail = {"hp": self.player.health, "mp": self.player.mp}
self.ui = UI_display(detail)
self.att = 0
self.dps = {}
# music.playMusic()
def mainloop(self):
# switch music mute by launcher
global collided_enemy
if data["music"]:
self.music.playMusic()
else:
self.music.pauseMusic()
during_time = 0.01
while True:
start_time = time.time()
for events in pygame.event.get():
if events.type == pygame.QUIT:
pygame.quit()
return False, "X"
if events.type == pygame.KEYDOWN:
if events.key == pygame.K_0:
detail = {
"x": random.randint(0, displayInfo.current_w),
"y": random.randint(0, displayInfo.current_h),
"health": 1000,
"speed": 6,
}
summon_enemy = Enemy(detail)
self.enemy.add(summon_enemy)
self.all_sprite.add(summon_enemy)
# if events.key == pygame.K_1:
# weapon.switch_weapon(1)
#
# if events.key == pygame.K_2:
# weapon.switch_weapon(2)
#
# if events.key == pygame.K_3:
# weapon.switch_weapon(3)
#
# if events.key == pygame.K_q:
# weapon.next_weapon()
if events.key == pygame.K_m:
if self.music.getBusy():
self.music.playMusic()
data["music"] = True
else:
self.music.pauseMusic()
data["music"] = False
if events.key == pygame.K_ESCAPE:
pygame.quit()
return False, "SAVE_QUIT"
key_pressed = list(pygame.key.get_pressed())
direction = [
key_pressed[pygame.K_d] - key_pressed[pygame.K_a],
key_pressed[pygame.K_s] - key_pressed[pygame.K_w],
]
# wall
self.wall.update(self.player.speed * during_time, [direction[0], 0])
if pygame.sprite.spritecollide(self.player, self.wall, False):
self.wall.update(self.player.speed * during_time, [-direction[0], 0])
direction[0] = 0
self.wall.update(self.player.speed * during_time, [0, direction[1]])
if pygame.sprite.spritecollide(self.player, self.wall, False):
self.wall.update(self.player.speed * during_time, [0, -direction[1]])
direction[1] = 0
print(direction)
# floor
for sprite in self.floor:
# new_x = None
# new_y = None
if sprite.rect.x < -SCREEN_WIDTH:
sprite.rect.x += SCREEN_WIDTH * 2
if sprite.rect.x > SCREEN_WIDTH:
sprite.rect.x -= SCREEN_WIDTH * 2
if sprite.rect.y < -SCREEN_HEIGHT:
sprite.rect.y += SCREEN_HEIGHT * 2
if sprite.rect.y > SCREEN_HEIGHT:
sprite.rect.y -= SCREEN_HEIGHT * 2
self.floor.update(self.player.speed * during_time, direction)
# player
if key_pressed[pygame.K_SPACE]:
if time.time() - self.player.speedup_cd > 4:
self.player.speedup_cd = time.time()
self.player.speedup = 8
self.player.update(direction, during_time)
# print(direction)
# enemy
self.enemy.update(
self.player.rect, direction, self.player.speed * during_time
)
# bullet
if pygame.mouse.get_pressed()[0]:
if time.time() - self.bullet_cd > 1 / self.weapon.detail["frequency"]:
# print('summon')
self.bullet_cd = time.time()
mouse_pos = pygame.mouse.get_pos()
target_direction = [
mouse_pos[0] - self.player.rect.centerx,
mouse_pos[1] - self.player.rect.centery,
]
target_direction = get_update_direction(target_direction)
detail = {
"knockback": self.weapon.detail["knockback"],
"accuracy": self.weapon.detail["accuracy"],
"damage": self.weapon.detail["damage"],
"strike": self.weapon.detail["strike"],
"speed": self.weapon.detail["speed"],
"cost": self.weapon.detail["cost"],
"size": self.weapon.detail["size"],
"kind": self.weapon.detail["kind"],
"target": "enemy",
"direction": target_direction,
}
# print(direction)
if self.player.cost_mp(detail["cost"]):
summon_bullet = Bullet(self.player, detail)
self.bullet.add(summon_bullet)
self.all_sprite.add(summon_bullet)
for entity in self.enemy:
if time.time() - entity.bullet_cd > entity.attack_cd:
entity.bullet_cd = time.time()
target = entity.target
if target is not None:
target_direction = [
target.centerx - entity.rect.centerx,
target.centery - entity.rect.centery,
]
target_direction = get_update_direction(target_direction)
detail = {
"knockback": self.weapon.detail["knockback"],
"accuracy": self.weapon.detail["accuracy"],
"damage": self.weapon.detail["damage"],
"strike": self.weapon.detail["strike"],
"speed": self.weapon.detail["speed"],
"cost": self.weapon.detail["cost"],
"size": self.weapon.detail["size"],
"kind": self.weapon.detail["kind"],
"target": "player",
"direction": target_direction,
}
summon_bullet = Bullet(entity, detail)
self.bullet.add(summon_bullet)
self.all_sprite.add(summon_bullet)
self.bullet.update(direction, self.player.speed * during_time)
# gun
# text
self.att = (
self.weapon.detail["damage"]
* (self.weapon.detail["accuracy"] * (1 - self.weapon.detail["strike"]))
+ self.weapon.detail["damage"]
* self.weapon.detail["accuracy"]
* self.weapon.detail["strike"]
* 2
) * self.weapon.detail["frequency"]
self.att_text.update("att:" + str(self.att))
for current_time in list(self.dps.keys()):
if time.time() - current_time > 1:
self.dps.pop(current_time)
self.dps_text.update("dps:" + str(round(sum(self.dps.values()) / 1, 2)))
self.enemy_left_text.update("Enemy Left:" + str(len(self.enemy)))
self.player_text.update("Player:")
self.player_hp_text.update("hp:" + str(self.player.health))
self.player_mp_text.update("mp:" + str(self.player.mp))
self.player_weapon_text.update("weapon:" + str(self.weapon.main_hand))
# if dps != {} : print(dps)
for entity in self.bullet:
if entity.target == "enemy":
collided_enemy = pygame.sprite.spritecollide(
entity, self.enemy, False
)
elif entity.target == "player":
group = pygame.sprite.Group()
group.add(self.player)
collided_enemy = pygame.sprite.spritecollide(entity, group, False)
if (
(entity.rect.x - 960) ** 2 + (entity.rect.y - 540) ** 2
) > self.weapon.detail["range"] ** 2:
# print('yes')
# print(entity.rect.x, entity.rect.y)
entity.kill()
self.bullet.remove(entity)
del entity
continue
elif pygame.sprite.spritecollide(entity, self.wall, False):
entity.kill()
self.bullet.remove(entity)
del entity
continue
elif len(collided_enemy) > 0:
detail = {
"damage": entity.damage,
"knockback": entity.knockback,
}
for collided in collided_enemy:
collided.hit(detail, entity.rect)
if entity.target == "enemy":
self.dps[time.time()] = detail["damage"]
entity.kill()
# bullet.remove(entity)
# print(collided.health)
if collided.health <= 0:
collided.kill()
self.enemy.remove(collided)
self.player.exp += 10
del collided
# print('del')
del entity
self.screen.fill((255, 255, 255))
# ui
detail = {
"hp": self.player.health,
"mp": self.player.mp,
"exp": self.player.exp,
}
self.ui_group = self.ui.update(detail)
blit_sprite = [
self.wall,
self.bullet,
self.enemy,
self.player_group,
self.situation_text,
self.ui_group,
]
for floor in self.floor:
self.screen.blit(floor.surf, floor.rect)
for group in blit_sprite:
for entity in group:
if (
displayInfo.current_w + 100 >= entity.rect.x >= 0 - 100
and displayInfo.current_h + 100 >= entity.rect.y >= 0 - 100
):
self.screen.blit(entity.surf, entity.rect)
# for entity in self.all_sprite:
# self.screen.blit(entity.surf, entity.rect)
#
# for entity in self.situation_text:
# self.screen.blit(entity.surf, entity.rect)
#
if len(self.enemy) <= 0:
return True, "SAVE" # pass
if self.player.health <= 0:
return False, "SAVE_DEAD" # loss
# self.clock.tick(self.fps)
pygame.display.flip()
pygame.event.pump()
end_time = time.time()
during_time = end_time - start_time
def gameSettings(self, lvl, data):
for i in range(lvl * 1):
index = random.choice(list(data.keys()))
detail = {
"x": random.randint(-displayInfo.current_w, displayInfo.current_w * 2)
- self.player._x,
"y": random.randint(-displayInfo.current_h, displayInfo.current_h * 2)
- self.player._y,
"health": lvl * data[index]["health"],
"speed": lvl * data[index]["speed"],
"cd": 1 / lvl,
"stay_range": data[index]["stay_range"],
"att_range": data[index]["att_range"],
}
summon_enemy = Enemy(detail)
self.enemy.add(summon_enemy)
self.all_sprite.add(summon_enemy)
# summon background