-
Notifications
You must be signed in to change notification settings - Fork 1
/
fasa.py
322 lines (250 loc) · 8.06 KB
/
fasa.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
"""
FASA Tactical Starship Combat Simulator (TSCS)
Requires:
Python 3.10
pygame 2.1
pip install pygame
https://www.pygame.org/docs/index.html
pygame_gui
pip install pygame_gui -U
https://pygame-gui.readthedocs.io/en/latest/modules.html
"""
import sys, time, random, math, pygame, pygame_gui
from pygame.locals import *
from MyLibrary import *
from hexmap import Hexmap
from ship import Ship
from pygame.locals import (
K_UP,K_DOWN,K_LEFT,K_RIGHT,K_ESCAPE,K_RETURN,K_SPACE,KEYDOWN,QUIT,
K_a,K_s,K_d,K_f
)
#
# all globals must be pre-defined
#
SCREENW=0
SCREENH=0
running=False
screen=None
fontl=None
fonts=None
hexmap=None
ship=None
C_GRAY=(200,200,200)
gui=None
guiwin_debug=None
guiwin_ship=None
guiimg_ship=None
guitxt_debug=None
guibtn_debug_circles=None
guibtn_debug_labels=None
#
# Initialization (be sure to call get_video_info() first)
#
def init_game():
global screen, backbuffer, fontl, fonts, timer
global hexmap, ship
pygame.init()
get_video_info()
title = "Starship Tactical Combat Simulator"
pygame.display.set_caption(title + " (" + str(SCREENW) + "x" + str(SCREENH)+ ")")
screen = pygame.display.set_mode(size=(SCREENW,SCREENH))
#pygame.display.toggle_fullscreen()
backbuffer = pygame.Surface((SCREENW,SCREENH))
timer = pygame.time.Clock()
#this avoids slow font scaling (add more if needed)
fontl = pygame.font.SysFont('arial', size=24, bold=True)
fonts = pygame.font.SysFont('arial', size=16, bold=False)
pygame.mouse.set_visible(True)
hexmap = Hexmap()
hexmap.init(80)
#
# Initialize the GUI
#
def init_gui():
global gui
global guiwin_debug
global guiwin_ship
global guiimg_ship
global guitxt_debug
global guibtn_debug_circles
global guibtn_debug_labels
gui = pygame_gui.UIManager((SCREENW,SCREENH))
#build the debug window
w,h = 500,300
x = SCREENW-w
y = SCREENH-h
guiwin_debug = pygame_gui.elements.ui_window.UIWindow(
rect=pygame.Rect((x,y),(w,h)),
window_display_title="DEBUG OUTPUT",
element_id="guiwin_debug",
manager=gui
)
guitxt_debug = pygame_gui.elements.ui_text_box.UITextBox(
relative_rect=pygame.Rect((0,0),(w-10,200)),
html_text="",
container=guiwin_debug,
manager=gui
)
guibtn_debug_circles = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect((0, 200), (120, 40)),
text='Circles On',
container=guiwin_debug,
manager=gui
)
guibtn_debug_labels = pygame_gui.elements.UIButton(
relative_rect=pygame.Rect((125, 200), (120, 40)),
text='Labels On',
container=guiwin_debug,
manager=gui
)
#
# Get display settings
#
def get_video_info():
global SCREENW
global SCREENH
info=pygame.display.Info()
print("Display: " + str(info.current_w) + "," + str(info.current_h) + "," + str(info.bitsize))
print("Desktop modes:")
sizes=pygame.display.get_desktop_sizes()
t=""
for s in sizes:
t += str(s) + ", "
print(t)
print("Display modes:")
modes=pygame.display.list_modes(depth=0, flags=pygame.FULLSCREEN, display=0)
t=""
for m in modes:
#ignore oversize modes > desktop
if (m[0] <= info.current_w and m[1] <= info.current_h):
t += "("+ str(m[0]) + "x" + str(m[1]) + "), "
print(t)
SCREENW=1280
SCREENH=1024
#
#
#
def print_debug_info(target):
global fonts
debugx=SCREENW-200
debugy=SCREENH-100
nl=16
if guibtn_debug_circles.text == "Circles On":
hexmap.draw_circles(target)
if guibtn_debug_labels.text == "Labels On":
hexmap.draw_labels(target, fonts)
s = "Ship: " + str(ship.rect.left) + "," + str(ship.rect.top) + \
" (" + str(ship.rect.centerx) + "," + str(ship.rect.centery) + ")"
print_text(backbuffer, fonts, debugx, debugy, s, C_GRAY)
s += "<br>"
mouse = pygame.mouse.get_pos()
x,y=mouse
s += "Mouse: " + str(x) + "," + str(y)
s += "<br>"
print_text(backbuffer, fonts, debugx, debugy, s, C_GRAY)
index,center = hexmap.get_hex_at(mouse)
posx,posy = center
indx,indy = index
#hex-grid 'Y' starts at 1 instead of 0 (based on the FASA hex map)
s += "Hex: " + str(indx)+"," + str(indy+1) + " at " + str((posx,posy))
s += "<br>"
guitxt_debug.html_text = s
guitxt_debug.rebuild()
if indx>-1:
hexmap.draw_hex(target, (255,0,0), radius=80, position=center, width=8)
"""
Ship data for now will include starbases, etc.
must define the xml structure first!
"""
def load_ship(filename):
global ship
ship = Ship()
ship.load_specs(filename)
print(str(ship.specs))
ship.load_top("fed_enterprise_b_512.png")
ship.load_side("fed_enterprise_b_side_512.png")
ship.rect.centerx = 170
ship.rect.centery = 159
ship.build_details_window(gui)
"""
"""
def load_hexmap(filename):
#define the xml structure first!
load_ship("ship_excelsior.json")
"""
main engine start
"""
init_game()
init_gui()
load_hexmap("map.xml")
game_over = False
last_time = 0
inputdelay = 0
clock = pygame.time.Clock()
#main loop
while True:
timer.tick(30)
ticks = pygame.time.get_ticks()
time_delta = clock.tick(60)/1000.0
for event in pygame.event.get():
if event.type == QUIT: sys.exit()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE: sys.exit()
#send events to GUI
gui.process_events(event)
#handle all gui buttons
if event.type == pygame_gui.UI_BUTTON_PRESSED:
#debug circles toggle
if event.ui_element == guibtn_debug_circles:
if guibtn_debug_circles.text=='Circles On':
guibtn_debug_circles.set_text('Circles Off')
else:
guibtn_debug_circles.set_text('Circles On')
#debug labels toggle
if event.ui_element == guibtn_debug_labels:
if guibtn_debug_labels.text=='Labels On':
guibtn_debug_labels.set_text('Labels Off')
else:
guibtn_debug_labels.set_text('Labels On')
#handle input events
pressed_keys = pygame.key.get_pressed()
if pygame.time.get_ticks() > inputdelay + 100:
inputdelay = pygame.time.get_ticks()
if pressed_keys[K_UP]:
ship.rect.centery -= hexmap.hexsize*2
if pressed_keys[K_DOWN]:
ship.rect.centery += hexmap.hexsize*2
if pressed_keys[K_LEFT]:
hexmap.jumptoggle = hexmap.jumptoggle * -1
ship.rect.centerx -= hexmap.hexsize
ship.rect.centery -= hexmap.hexsize * hexmap.jumptoggle
if pressed_keys[K_RIGHT]:
hexmap.jumptoggle = hexmap.jumptoggle * -1
ship.rect.centerx += hexmap.hexsize
ship.rect.centery += hexmap.hexsize * hexmap.jumptoggle
if pressed_keys[K_d]:
ship.dir = ship.dir + 1
if ship.dir > 5: ship.dir = 0
if pressed_keys[K_a]:
ship.dir = ship.dir - 1
if ship.dir < 0: ship.dir = 5
#let the GUI perform updates
gui.update(time_delta)
#clear the background
backbuffer.fill((20,20,20))
hexmap.draw(backbuffer, (200,200,200))
ship.draw(backbuffer)
print_debug_info(backbuffer)
gui.draw_ui(backbuffer)
#draw the back buffer
screen.blit(backbuffer, (0,0))
pygame.display.update()
pygame.quit()
"""
msgbox = pygame_gui.windows.ui_message_window.UIMessageWindow(
rect=pygame.Rect((500,500),(200,160)),
html_message="Debug circles toggled",
manager=gui,
window_title="Debug Toggle"
)
"""