2222import tcod .bsp
2323import tcod .cffi
2424import tcod .console
25+ import tcod .constants
2526import tcod .context
2627import tcod .event
2728import tcod .image
@@ -76,15 +77,18 @@ def _get_elapsed_time() -> float:
7677
7778
7879class Sample :
80+ """Samples base class."""
81+
7982 name : str = "???"
8083
8184 def on_enter (self ) -> None :
82- pass
85+ """Called when entering a sample."""
8386
8487 def on_draw (self ) -> None :
85- pass
88+ """Called every frame."""
8689
8790 def on_event (self , event : tcod .event .Event ) -> None :
91+ """Called for each event."""
8892 global cur_sample
8993 match event :
9094 case tcod .event .Quit () | tcod .event .KeyDown (sym = KeySym .ESCAPE ):
@@ -114,16 +118,20 @@ def on_event(self, event: tcod.event.Event) -> None:
114118
115119
116120class TrueColorSample (Sample ):
121+ """Simple performance benchmark."""
122+
117123 name = "True colors"
118124
119125 def __init__ (self ) -> None :
126+ """Initialize random generators."""
120127 self .noise = tcod .noise .Noise (2 , tcod .noise .Algorithm .SIMPLEX )
121128 """Noise for generating color."""
122129
123130 self .generator = np .random .default_rng ()
124131 """Numpy generator for random text."""
125132
126133 def on_draw (self ) -> None :
134+ """Draw this sample."""
127135 self .interpolate_corner_colors ()
128136 self .darken_background_characters ()
129137 self .randomize_sample_console ()
@@ -178,9 +186,15 @@ def randomize_sample_console(self) -> None:
178186
179187
180188class OffscreenConsoleSample (Sample ):
189+ """Console blit example."""
190+
181191 name = "Offscreen console"
182192
193+ CONSOLE_MOVE_RATE = 1 / 2
194+ CONSOLE_MOVE_MARGIN = 5
195+
183196 def __init__ (self ) -> None :
197+ """Initialize the offscreen console."""
184198 self .secondary = tcod .console .Console (sample_console .width // 2 , sample_console .height // 2 )
185199 self .screenshot = tcod .console .Console (sample_console .width , sample_console .height )
186200 self .counter = 0.0
@@ -189,57 +203,51 @@ def __init__(self) -> None:
189203 self .x_dir = 1
190204 self .y_dir = 1
191205
192- self .secondary .draw_frame (
206+ self .secondary .draw_frame (0 , 0 , self .secondary .width , self .secondary .height , clear = False , fg = WHITE , bg = BLACK )
207+ self .secondary .print (
193208 0 ,
194209 0 ,
195- sample_console . width // 2 ,
196- sample_console . height // 2 ,
197- " Offscreen console" ,
198- clear = False ,
199- fg = WHITE ,
200- bg = BLACK ,
210+ width = self . secondary . width ,
211+ height = self . secondary . height ,
212+ text = " Offscreen console " ,
213+ fg = BLACK ,
214+ bg = WHITE ,
215+ alignment = tcod . constants . CENTER ,
201216 )
202217
203- self .secondary .print_box (
204- 1 ,
205- 2 ,
206- sample_console .width // 2 - 2 ,
207- sample_console .height // 2 ,
208- "You can render to an offscreen console and blit in on another one, simulating alpha transparency." ,
218+ self .secondary .print (
219+ x = 1 ,
220+ y = 2 ,
221+ width = sample_console .width // 2 - 2 ,
222+ height = sample_console .height // 2 ,
223+ text = "You can render to an offscreen console and blit in on another one, simulating alpha transparency." ,
209224 fg = WHITE ,
210225 bg = None ,
211226 alignment = libtcodpy .CENTER ,
212227 )
213228
214229 def on_enter (self ) -> None :
230+ """Capture the previous sample screen as this samples background."""
215231 self .counter = _get_elapsed_time ()
216- # get a "screenshot" of the current sample screen
217- sample_console .blit (dest = self .screenshot )
232+ sample_console .blit (dest = self .screenshot ) # get a "screenshot" of the current sample screen
218233
219234 def on_draw (self ) -> None :
220- if _get_elapsed_time () - self .counter >= 1 :
235+ """Draw and animate the offscreen console."""
236+ if _get_elapsed_time () - self .counter >= self .CONSOLE_MOVE_RATE :
221237 self .counter = _get_elapsed_time ()
222238 self .x += self .x_dir
223239 self .y += self .y_dir
224- if self .x == sample_console .width / 2 + 5 :
240+ if self .x == sample_console .width / 2 + self . CONSOLE_MOVE_MARGIN :
225241 self .x_dir = - 1
226- elif self .x == - 5 :
242+ elif self .x == - self . CONSOLE_MOVE_MARGIN :
227243 self .x_dir = 1
228- if self .y == sample_console .height / 2 + 5 :
244+ if self .y == sample_console .height / 2 + self . CONSOLE_MOVE_MARGIN :
229245 self .y_dir = - 1
230- elif self .y == - 5 :
246+ elif self .y == - self . CONSOLE_MOVE_MARGIN :
231247 self .y_dir = 1
232248 self .screenshot .blit (sample_console )
233249 self .secondary .blit (
234- sample_console ,
235- self .x ,
236- self .y ,
237- 0 ,
238- 0 ,
239- sample_console .width // 2 ,
240- sample_console .height // 2 ,
241- 1.0 ,
242- 0.75 ,
250+ sample_console , self .x , self .y , 0 , 0 , sample_console .width // 2 , sample_console .height // 2 , 1.0 , 0.75
243251 )
244252
245253
@@ -298,9 +306,7 @@ def on_draw(self) -> None:
298306 for x in range (sample_console .width ):
299307 value = x * 255 // sample_console .width
300308 col = (value , value , value )
301- libtcodpy .console_set_char_background (sample_console , x , rect_y , col , self .bk_flag )
302- libtcodpy .console_set_char_background (sample_console , x , rect_y + 1 , col , self .bk_flag )
303- libtcodpy .console_set_char_background (sample_console , x , rect_y + 2 , col , self .bk_flag )
309+ sample_console .draw_rect (x = x , y = rect_y , width = 1 , height = 3 , ch = 0 , fg = None , bg = col , bg_blend = self .bk_flag )
304310 angle = time .time () * 2.0
305311 cos_angle = math .cos (angle )
306312 sin_angle = math .sin (angle )
@@ -312,14 +318,8 @@ def on_draw(self) -> None:
312318 # in python the easiest way is to use the line iterator
313319 for x , y in tcod .los .bresenham ((xo , yo ), (xd , yd )).tolist ():
314320 if 0 <= x < sample_console .width and 0 <= y < sample_console .height :
315- libtcodpy .console_set_char_background (sample_console , x , y , LIGHT_BLUE , self .bk_flag )
316- sample_console .print (
317- 2 ,
318- 2 ,
319- f"{ self .FLAG_NAMES [self .bk_flag & 0xFF ]} (ENTER to change)" ,
320- fg = WHITE ,
321- bg = None ,
322- )
321+ sample_console .draw_rect (x , y , width = 1 , height = 1 , ch = 0 , fg = None , bg = LIGHT_BLUE , bg_blend = self .bk_flag )
322+ sample_console .print (2 , 2 , f"{ self .FLAG_NAMES [self .bk_flag & 0xFF ]} (ENTER to change)" , fg = WHITE , bg = None )
323323
324324
325325class NoiseSample (Sample ):
0 commit comments