-
Notifications
You must be signed in to change notification settings - Fork 3
/
menu.py
141 lines (106 loc) · 4.24 KB
/
menu.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
# -*- coding: utf-8 -*-
from sdl2 import SDL_Delay, \
SDL_GetTicks, \
SDL_KEYDOWN, \
SDL_KEYUP, \
SDL_QUIT, \
SDL_Rect, \
SDL_RenderCopy, \
SDLK_ESCAPE, \
SDLK_UP, \
SDLK_DOWN, \
SDLK_RETURN, \
SDL_Quit
from sdl2.ext import Resources, \
get_events
from const import WindowSize, Colors
from input import Input
from ui import Dialog
from game import Game
FPS = 60 # units.FPS
MAX_FRAME_TIME = int(5 * (1000 / FPS))
RESOURCES = Resources(__file__, 'resources')
class Menu:
def __init__(self, window, world, renderer, factory):
self.window = window
self.renderer = renderer
self.world = world
self.factory = factory
self.rsystem = factory.create_sprite_render_system(window)
self.menu_bg = RESOURCES.get_path("menu_bg.png")
self.menu_cursor = RESOURCES.get_path("menu_cursor.png")
self.running = True
self.position = 460, 340
self.cursor_start_position = 370, 330
self.cursor_position = 0
self.cursor_sprite_size = 32
self.background_sprite = self.factory.from_image(self.menu_bg)
self.cursor_sprite = self.factory.from_image(self.menu_cursor)
self.text = {0: "START",
1: "OPTIONS",
2: "EXIT"}
self.dialog = Dialog(self.factory,
font_size=32,
fg_color=Colors.WHITE,
bg_color=Colors.BLACK,
font_name="04B_20__.TTF",
text=self.text,
position=self.position,
renderer=self.renderer)
self.sprites = [self.background_sprite]
dialog_decoration_sprites = self.dialog.get_decoration_sprites()
self.sprites.append(dialog_decoration_sprites)
text_sprites = self.dialog.get_text_sprites()
for line in text_sprites:
self.sprites.append(line)
self.sprites.append(self.cursor_sprite)
def __del__(self):
SDL_Quit()
def update(self, elapsed_time):
self.cursor_sprite.position = self.cursor_start_position[0],\
self.cursor_start_position[1] + self.cursor_position * self.cursor_sprite_size
def run(self):
menu_input = Input()
last_update_time = SDL_GetTicks() # units.MS
while self.running:
start_time = SDL_GetTicks() # units.MS
menu_input.begin_new_frame()
menu_events = get_events()
for event in menu_events:
if event.type == SDL_KEYDOWN:
menu_input.key_down_event(event)
elif event.type == SDL_KEYUP:
menu_input.key_up_event(event)
elif event.type == SDL_QUIT:
self.running = False
break
# Exit
if menu_input.was_key_pressed(SDLK_ESCAPE):
self.running = False
# Move the cursor
elif menu_input.was_key_pressed(SDLK_UP):
if self.cursor_position != 0:
self.cursor_position -= 1
elif menu_input.was_key_pressed(SDLK_DOWN):
if self.cursor_position != 2:
self.cursor_position += 1
# Select option
elif menu_input.was_key_pressed(SDLK_RETURN):
self.running = False
if self.cursor_position == 0:
self.launch_game()
current_time = SDL_GetTicks() # units.MS
elapsed_time = current_time - last_update_time # units.MS
self.update(min(elapsed_time, MAX_FRAME_TIME))
last_update_time = current_time
self.renderer.process(self.world, self.sprites)
# 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)
def launch_game(self):
game = Game(self.world, self.window, self.renderer, self.factory)
game.run()
self.running = True
self.run()