A textbox with a teleprinter text effect as in old adventure games
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2023-08-15 13:12:17 +02:00
src/teletype Adapt to latest version of Cooldown 2023-08-15 13:12:17 +02:00
.flake8 Initial checkin 2023-03-11 04:59:57 +01:00
.gitignore Initial commit 2023-03-11 04:01:26 +01:00
demo.gif Screen recording of demo 2023-03-11 05:32:24 +01:00
LICENSE Initial commit 2023-03-11 04:01:26 +01:00
MANIFEST.in Added a multi-window demo 2023-03-11 22:35:52 +01:00
multidemo.gif Added a multi-window demo 2023-03-11 22:35:52 +01:00
pyproject.toml Rework project structure 2023-08-15 13:11:49 +02:00
README.md Update README.md 2023-03-21 19:57:16 +01:00

pygame-teletype

A textbox with a teletype text effect as in old adventure games.

Screen recording of demo

Installation

Never do this without a virtual env!

pip install git+https://github.com/dickerdackel/pygame-teletype
teletype-demo

Usage

Teletype()

tt = Teletype(pos=screen.get_rect().center,
	  text='Lorem ipsum dolor',
	  margin=10,
	  ticker_speed=0.05,
	  font=pygame.font.SysFont('couriernew', 16),
	  font_color=pygame.Color('black'),
	  sound=pygame.mixer.Sound('beep.wav'),
	  random_delay=0,
	  pause_after_sentence=True)

...

tt.update(dt)
tt.draw(screen)

Parameters:

pos: Vector2 - *center* position of the text box
backdrop: pygame.Surface - background to draw the text on.
text: the text to teletype
margin: int - margin around the text within the text box
ticket_speed: float - delay in seconds between letters
backdrop: pygame.Surface - optional.  e.g. a paper texture
font: pygame.font.SysFont - Falls back to couriernew, 16pt
font_color: pygame.Color - defaults to white
random_delay: float = 0: randomly add pauses, faking transmission delays
pause_after_sentence: bool = True - Make a longer pause after ./!/?/;

The rect of the background also gives the size of the box. If you don't need a background, just pass a transparent surface.

The process shouldn't be too resource intensive, since every letter is blitted onto a surface that's kept in the class. Line breaks as well as scrolling are implemented.

Run and look into teletype-demo for an example.

How it's done

The class works on 3 surfaces.

1st backdrop. You hand the main surface, e.g. a paper texture, with proper dimensions to the constructor.

The class itself has a main surface the same size as the backdrop.

Additionally, there's the text canvas. This is filled letter by letter after each cooldown delay.

Then first the backdrop is blitted onto the main surface, followed by the text canvas.

TODO

Proper word wrapping and text justification would be nice. Right now, words simply break in the middle, when the border is reached. But this requires a form of lookahead and this was implemented in about 2 hours, with 2 more for documentation and making a github project.

Licensing stuff

This lib is under the MIT license.

Beep sound, used in demo from https://opengameart.org/content/beep-tone-sound-sfx It's CC0, but still

Screen recording of a multi instance demo