Skip to content

Commit 3cddaad

Browse files
committed
Cleanup tcod samples
1 parent 56d0e60 commit 3cddaad

File tree

1 file changed

+28
-41
lines changed

1 file changed

+28
-41
lines changed

examples/samples_tcod.py

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def __init__(self) -> None:
190190
sample_console.width // 2,
191191
sample_console.height // 2,
192192
"Offscreen console",
193-
False,
193+
clear=False,
194194
fg=WHITE,
195195
bg=BLACK,
196196
)
@@ -239,7 +239,7 @@ def on_draw(self) -> None:
239239

240240

241241
class LineDrawingSample(Sample):
242-
FLAG_NAMES = [
242+
FLAG_NAMES = (
243243
"BKGND_NONE",
244244
"BKGND_SET",
245245
"BKGND_MULTIPLY",
@@ -253,7 +253,7 @@ class LineDrawingSample(Sample):
253253
"BKGND_BURN",
254254
"BKGND_OVERLAY",
255255
"BKGND_ALPHA",
256-
]
256+
)
257257

258258
def __init__(self) -> None:
259259
self.name = "Line drawing"
@@ -316,7 +316,7 @@ def on_draw(self) -> None:
316316

317317

318318
class NoiseSample(Sample):
319-
NOISE_OPTIONS = [ # (name, algorithm, implementation)
319+
NOISE_OPTIONS = ( # (name, algorithm, implementation)
320320
(
321321
"perlin noise",
322322
tcod.noise.Algorithm.PERLIN,
@@ -362,7 +362,7 @@ class NoiseSample(Sample):
362362
tcod.noise.Algorithm.WAVELET,
363363
tcod.noise.Implementation.TURBULENCE,
364364
),
365-
]
365+
)
366366

367367
def __init__(self) -> None:
368368
self.name = "Noise"
@@ -428,7 +428,7 @@ def on_draw(self) -> None:
428428
)
429429

430430
for cur_func in range(len(self.NOISE_OPTIONS)):
431-
text = "%i : %s" % (cur_func + 1, self.NOISE_OPTIONS[cur_func][0])
431+
text = f"{cur_func + 1} : {self.NOISE_OPTIONS[cur_func][0]}"
432432
if cur_func == self.func:
433433
sample_console.print(2, 2 + cur_func, text, fg=WHITE, bg=LIGHT_BLUE)
434434
else:
@@ -495,7 +495,7 @@ def ev_keydown(self, event: tcod.event.KeyDown) -> None:
495495
DARK_GROUND = (50, 50, 150)
496496
LIGHT_GROUND = (200, 180, 50)
497497

498-
SAMPLE_MAP_ = [
498+
SAMPLE_MAP_ = (
499499
"##############################################",
500500
"####################### #################",
501501
"##################### # ###############",
@@ -516,11 +516,11 @@ def ev_keydown(self, event: tcod.event.KeyDown) -> None:
516516
"######## # #### ##### #####",
517517
"######## ##### ####################",
518518
"##############################################",
519-
]
519+
)
520520

521521
SAMPLE_MAP: NDArray[Any] = np.array([list(line) for line in SAMPLE_MAP_]).transpose()
522522

523-
FOV_ALGO_NAMES = [
523+
FOV_ALGO_NAMES = (
524524
"BASIC ",
525525
"DIAMOND ",
526526
"SHADOW ",
@@ -535,7 +535,7 @@ def ev_keydown(self, event: tcod.event.KeyDown) -> None:
535535
"PERMISSIVE8",
536536
"RESTRICTIVE",
537537
"SYMMETRIC_SHADOWCAST",
538-
]
538+
)
539539

540540
TORCH_RADIUS = 10
541541
SQUARED_TORCH_RADIUS = TORCH_RADIUS * TORCH_RADIUS
@@ -635,13 +635,13 @@ def on_draw(self) -> None:
635635
sample_console.bg[...] = np.where(fov[:, :, np.newaxis], self.light_map_bg, self.dark_map_bg)
636636

637637
def ev_keydown(self, event: tcod.event.KeyDown) -> None:
638-
MOVE_KEYS = {
638+
MOVE_KEYS = { # noqa: N806
639639
tcod.event.KeySym.i: (0, -1),
640640
tcod.event.KeySym.j: (-1, 0),
641641
tcod.event.KeySym.k: (0, 1),
642642
tcod.event.KeySym.l: (1, 0),
643643
}
644-
FOV_SELECT_KEYS = {
644+
FOV_SELECT_KEYS = { # noqa: N806
645645
tcod.event.KeySym.MINUS: -1,
646646
tcod.event.KeySym.EQUALS: 1,
647647
tcod.event.KeySym.KP_MINUS: -1,
@@ -753,7 +753,7 @@ def on_draw(self) -> None:
753753
sample_console,
754754
x,
755755
y,
756-
libtcodpy.color_lerp( # type: ignore
756+
libtcodpy.color_lerp( # type: ignore[arg-type]
757757
LIGHT_GROUND,
758758
DARK_GROUND,
759759
0.9 * libtcodpy.dijkstra_get_distance(self.dijkstra, x, y) / self.dijkstra_dist,
@@ -771,11 +771,11 @@ def on_draw(self) -> None:
771771
if self.using_astar:
772772
if not libtcodpy.path_is_empty(self.path):
773773
libtcodpy.console_put_char(sample_console, self.px, self.py, " ", libtcodpy.BKGND_NONE)
774-
self.px, self.py = libtcodpy.path_walk(self.path, True) # type: ignore
774+
self.px, self.py = libtcodpy.path_walk(self.path, True) # type: ignore[assignment]
775775
libtcodpy.console_put_char(sample_console, self.px, self.py, "@", libtcodpy.BKGND_NONE)
776776
elif not libtcodpy.dijkstra_is_empty(self.dijkstra):
777777
libtcodpy.console_put_char(sample_console, self.px, self.py, " ", libtcodpy.BKGND_NONE)
778-
self.px, self.py = libtcodpy.dijkstra_path_walk(self.dijkstra) # type: ignore
778+
self.px, self.py = libtcodpy.dijkstra_path_walk(self.dijkstra) # type: ignore[assignment]
779779
libtcodpy.console_put_char(sample_console, self.px, self.py, "@", libtcodpy.BKGND_NONE)
780780
self.recalculate = True
781781

@@ -983,9 +983,9 @@ def on_draw(self) -> None:
983983
1,
984984
"ENTER : rebuild bsp\n"
985985
"SPACE : rebuild dungeon\n"
986-
"+-: bsp depth %d\n"
987-
"*/: room size %d\n"
988-
"1 : random room size %s" % (bsp_depth, bsp_min_room_size, rooms),
986+
f"+-: bsp depth {bsp_depth}\n"
987+
f"*/: room size {bsp_min_room_size}\n"
988+
f"1 : random room size {rooms}",
989989
fg=WHITE,
990990
bg=None,
991991
)
@@ -1102,23 +1102,12 @@ def on_draw(self) -> None:
11021102
sample_console.print(
11031103
1,
11041104
1,
1105-
"Pixel position : %4dx%4d\n"
1106-
"Tile position : %4dx%4d\n"
1107-
"Tile movement : %4dx%4d\n"
1108-
"Left button : %s\n"
1109-
"Right button : %s\n"
1110-
"Middle button : %s\n"
1111-
% (
1112-
self.motion.position.x,
1113-
self.motion.position.y,
1114-
self.motion.tile.x,
1115-
self.motion.tile.y,
1116-
self.motion.tile_motion.x,
1117-
self.motion.tile_motion.y,
1118-
("OFF", "ON")[self.mouse_left],
1119-
("OFF", "ON")[self.mouse_right],
1120-
("OFF", "ON")[self.mouse_middle],
1121-
),
1105+
f"Pixel position : {self.motion.position.x:4d}x{self.motion.position.y:4d}\n"
1106+
f"Tile position : {self.motion.tile.x:4d}x{self.motion.tile.y:4d}\n"
1107+
f"Tile movement : {self.motion.tile_motion.x:4d}x{self.motion.tile_motion.y:4d}\n"
1108+
f"Left button : {'ON' if self.mouse_left else 'OFF'}\n"
1109+
f"Right button : {'ON' if self.mouse_right else 'OFF'}\n"
1110+
f"Middle button : {'ON' if self.mouse_middle else 'OFF'}\n",
11221111
fg=LIGHT_YELLOW,
11231112
bg=None,
11241113
)
@@ -1406,10 +1395,8 @@ def init_context(renderer: int) -> None:
14061395
global context, console_render, sample_minimap
14071396
if "context" in globals():
14081397
context.close()
1409-
libtcod_version = "%i.%i.%i" % (
1410-
tcod.cffi.lib.TCOD_MAJOR_VERSION,
1411-
tcod.cffi.lib.TCOD_MINOR_VERSION,
1412-
tcod.cffi.lib.TCOD_PATCHLEVEL,
1398+
libtcod_version = (
1399+
f"{tcod.cffi.lib.TCOD_MAJOR_VERSION}.{tcod.cffi.lib.TCOD_MINOR_VERSION}.{tcod.cffi.lib.TCOD_PATCHLEVEL}"
14131400
)
14141401
context = tcod.context.new(
14151402
columns=root_console.width,
@@ -1535,14 +1522,14 @@ def draw_stats() -> None:
15351522
root_console.print(
15361523
root_console.width,
15371524
46,
1538-
"last frame :%5.1f ms (%4d fps)" % (frame_length[-1] * 1000.0, fps),
1525+
f"last frame :{frame_length[-1] * 1000.0:5.1f} ms ({int(fps):4d} fps)",
15391526
fg=GREY,
15401527
alignment=libtcodpy.RIGHT,
15411528
)
15421529
root_console.print(
15431530
root_console.width,
15441531
47,
1545-
"elapsed : %8d ms %5.2fs" % (time.perf_counter() * 1000, time.perf_counter()),
1532+
f"elapsed : {int(time.perf_counter() * 1000):8d} ms {time.perf_counter():5.2f}s",
15461533
fg=GREY,
15471534
alignment=libtcodpy.RIGHT,
15481535
)

0 commit comments

Comments
 (0)