-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.py
More file actions
53 lines (42 loc) · 1.56 KB
/
example.py
File metadata and controls
53 lines (42 loc) · 1.56 KB
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
"""Example game using PyTerminal."""
from pyterminal import PyTerminal,time
class Game:
"""A simple demo game that animates a name in the terminal."""
def __init__(self):
"""Initialize the game variables."""
self.index = 0
self.forward = True
self.display_text = ""
self.name = None
self.elapsed = 0
self.engine = PyTerminal(init_func=self.init, end_func=self.end)
def init(self, engine):
name = engine.get_input("/cRANDWhat is your name? ")
self.name = name or "Person"
def update(self, _delta):
if self.forward:
if self.index < len(self.name):
self.index += 1
self.display_text = self.name[:self.index]
else:
self.forward = False
else:
if self.index > 0:
self.index -= 1
self.display_text = self.name[:self.index]
else:
self.forward = True
def draw(self, delta):
self.elapsed += delta
self.engine.draw(f"/cRANDHello, {self.display_text}!")
if self.elapsed >= 10:
self.engine.quit()
def end(self):
"""End of game message."""
self.engine.draw("/cYELLOWExample ended")
self.engine.draw("/cGREENThanks for checking it out")
def run_game(self): # call once at game loop begin
"""Start the game loop."""
self.engine.run_loop(self.update, self.draw, fps=5)
game = Game()
game.run_game()