forked from JauriaStudios/soulmaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
199 lines (148 loc) · 5.87 KB
/
game.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
# -*- coding: utf-8 -*-
from sdl2 import SDL_GetTicks, \
SDL_KEYUP, \
SDL_KEYDOWN, \
SDL_QUIT, \
SDL_Delay,\
SDLK_ESCAPE, \
SDLK_RIGHT, \
SDLK_UP, \
SDLK_DOWN, \
SDLK_LEFT, \
SDLK_SPACE, \
SDLK_i
from sdl2.ext import Resources, \
get_events
from const import WindowSize
from input import Input
from db import DataBase
from map import Map
from player import Player, Facing, MotionType
from npc import NPC
from enemy import Enemy
FPS = 30 # units.FPS
MAX_FRAME_TIME = int(5 * (1000 / FPS))
RESOURCES = Resources(__file__, 'resources')
MAPS = Resources(__file__, 'resources', 'maps')
class Game:
def __init__(self, world, window, renderer, factory):
self.db = DataBase()
self.running = False
self.world = world
self.window = window
self.renderer = renderer
self.factory = factory
self.background_tiles = []
self.behind_tiles = []
self.front_tiles = []
self.sprites = []
map_file = MAPS.get_path("map.tmx")
self.map_bg_sprite = Map(map_file)
self.player = Player(self.renderer, self.factory)
self.all_npc = []
self.init_npc("Debug Room")
self.all_enemies = [Enemy(self.renderer, self.factory, "doombat")]
def init_npc(self, map_name):
map_data = self.db.get_map_npc(map_name)
map_npc = []
for data in map_data:
map_npc.append(data["npc"])
for npc in map_npc:
self.all_npc.append(NPC(self.renderer, self.factory, npc))
def get_sprites(self):
self.sprites.append(self.map_bg_sprite)
for sprite in self.player.get_sprites():
self.sprites.append(sprite)
for npc in self.all_npc:
for sprite in npc.get_sprites():
self.sprites.append(sprite)
for enemy in self.all_enemies:
for sprite in enemy.get_sprites():
self.sprites.append(sprite)
def update(self, position, motion_type, facing, elapsed_time):
self.map_bg_sprite.position = position
self.player.update(motion_type, facing, elapsed_time)
for npc in self.all_npc:
npc.update(position, elapsed_time)
for enemy in self.all_enemies:
enemy.update(position, elapsed_time)
def run(self):
game_input = Input()
speed_x, speed_y = 2, 1
player_pos = [0, 0]
motion_type = self.player.motion_type
facing = self.player.facing
running = True
last_update_time = SDL_GetTicks() # units.MS
while running:
start_time = SDL_GetTicks() # units.MS
game_input.begin_new_frame()
game_events = get_events()
for event in game_events:
if event.type == SDL_KEYDOWN:
game_input.key_down_event(event)
elif event.type == SDL_KEYUP:
game_input.key_up_event(event)
elif event.type == SDL_QUIT:
running = False
break
# Exit
if game_input.was_key_pressed(SDLK_ESCAPE):
running = False
# Player movement
if game_input.is_key_held(SDLK_RIGHT) and game_input.is_key_held(SDLK_UP):
player_pos[0] -= speed_x
player_pos[1] += speed_y
motion_type = MotionType.WALKING
facing = Facing.RIGHT_UP
elif game_input.is_key_held(SDLK_RIGHT) and game_input.is_key_held(SDLK_DOWN):
player_pos[0] -= speed_x
player_pos[1] -= speed_y
motion_type = MotionType.WALKING
facing = Facing.RIGHT_DOWN
elif game_input.is_key_held(SDLK_LEFT) and game_input.is_key_held(SDLK_UP):
player_pos[0] += speed_x
player_pos[1] += speed_y
motion_type = MotionType.WALKING
facing = Facing.LEFT_UP
elif game_input.is_key_held(SDLK_LEFT) and game_input.is_key_held(SDLK_DOWN):
player_pos[0] += speed_x
player_pos[1] -= speed_y
motion_type = MotionType.WALKING
facing = Facing.LEFT_DOWN
elif game_input.is_key_held(SDLK_LEFT):
player_pos[0] += speed_x
motion_type = MotionType.WALKING
facing = Facing.LEFT
elif game_input.is_key_held(SDLK_RIGHT):
player_pos[0] -= speed_x
motion_type = MotionType.WALKING
facing = Facing.RIGHT
elif game_input.is_key_held(SDLK_UP):
player_pos[1] += speed_y
motion_type = MotionType.WALKING
facing = Facing.UP
elif game_input.is_key_held(SDLK_DOWN):
player_pos[1] -= speed_y
motion_type = MotionType.WALKING
facing = Facing.DOWN
elif game_input.was_key_pressed(SDLK_i):
self.player.toggle_inventory()
# Player Attack
elif game_input.is_key_held(SDLK_SPACE):
motion_type = MotionType.CASTING
# Nothing
else:
motion_type = MotionType.STANDING
current_time = SDL_GetTicks() # units.MS
elapsed_time = current_time - last_update_time # units.MS
self.update(player_pos, motion_type, facing, min(elapsed_time, MAX_FRAME_TIME))
last_update_time = current_time
self.get_sprites()
self.renderer.process(self.world, self.sprites)
self.sprites.clear()
# This loop lasts 1/60th of a second, or 1000/60th ms
ms_per_frame = 1000 // FPS # units.MS
elapsed_time = SDL_GetTicks() - start_time # units.MS
if elapsed_time < ms_per_frame:
SDL_Delay(ms_per_frame - elapsed_time)