Skip to content

Commit 413e0da

Browse files
committed
Add scrolling of over-long text
1 parent 1c8a86f commit 413e0da

File tree

1 file changed

+58
-11
lines changed
  • CircuitPython_Zorque_Text_Game_openai

1 file changed

+58
-11
lines changed

CircuitPython_Zorque_Text_Game_openai/code.py

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
77
import adafruit_requests as requests
88
import adafruit_touchscreen
9+
from adafruit_ticks import ticks_ms, ticks_add, ticks_less
910
from adafruit_bitmap_font.bitmap_font import load_font
1011
from adafruit_display_text.bitmap_label import Label
1112
from adafruit_display_text import wrap_text_to_pixels
@@ -104,15 +105,44 @@ def terminal_palette(fg=0xffffff, bg=0):
104105
p[1] = fg
105106
return p
106107

108+
class WrappedTextDisplay:
109+
def __init__(self):
110+
self.line_offset = 0
111+
self.lines = []
112+
113+
def add_text(self, text):
114+
self.lines.extend(wrap_text_to_pixels(text, use_width, nice_font))
115+
116+
def set_text(self, text):
117+
self.lines = wrap_text_to_pixels(text, use_width, nice_font)
118+
self.line_offset = 0
119+
120+
def scroll_to_end(self):
121+
self.line_offset = self.max_offset()
122+
123+
def scroll_next_line(self):
124+
max_offset = self.max_offset()
125+
if max_offset > 0:
126+
line_offset = self.line_offset + 1
127+
self.line_offset = line_offset % (max_offset + 1)
128+
129+
def max_offset(self):
130+
return max(0, len(self.lines) - max_lines)
131+
132+
def on_last_line(self):
133+
return self.line_offset == self.max_offset()
134+
135+
def refresh(self):
136+
text = '\n'.join(self.lines[self.line_offset : self.line_offset + max_lines])
137+
while '\n\n' in text:
138+
text = text.replace('\n\n', '\n \n')
139+
terminal.text = text
140+
board.DISPLAY.refresh()
141+
wrapped_text_display = WrappedTextDisplay()
142+
107143
def print_wrapped(text):
108-
lines = wrap_text_to_pixels(text, use_width, nice_font)
109-
del lines[max_lines:]
110-
#del lines[:-max_lines]
111-
text = '\n'.join(lines)
112-
while '\n\n' in text:
113-
text = text.replace('\n\n', '\n \n')
114-
terminal.text = text
115-
board.DISPLAY.refresh()
144+
wrapped_text_display.set_text(text)
145+
wrapped_text_display.refresh()
116146

117147
def make_full_prompt(action):
118148
return session + [{"role": "user", "content": f"PLAYER: {action}"}]
@@ -128,7 +158,15 @@ def record_game_step(action, response):
128158
def get_one_completion(full_prompt):
129159
if not use_openai:
130160
return f"""This is a canned response in offline mode. The player's last choice was as follows:
131-
{full_prompt[-1]['content']}""".strip()
161+
{full_prompt[-1]['content']}
162+
163+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Nulla aliquet enim tortor at auctor urna. Arcu ac tortor dignissim convallis aenean et tortor at. Dapibus ultrices in iaculis nunc sed augue. Enim nec dui nunc mattis enim ut tellus elementum sagittis. Sit amet mattis vulputate enim nulla. Ultrices in iaculis nunc sed augue lacus. Pulvinar neque laoreet suspendisse interdum consectetur libero id faucibus nisl. Aenean pharetra magna ac placerat vestibulum lectus mauris ultrices eros. Imperdiet nulla malesuada pellentesque elit eget. Tellus at urna condimentum mattis pellentesque id nibh tortor. Velit dignissim sodales ut eu sem integer vitae. Id ornare arcu odio ut sem nulla pharetra diam sit.
164+
165+
1: Stand in the place where you live
166+
2: Now face West
167+
3: Think about the place where you live
168+
4: Wonder why you haven't before
169+
""".strip()
132170
try:
133171
response = requests.post(
134172
"https://api.openai.com/v1/chat/completions",
@@ -154,6 +192,7 @@ def get_touchscreen_choice():
154192

155193
# Wait for screen to be pressed
156194
touch_count = 0
195+
deadline = ticks_add(ticks_ms(), 1000)
157196
while True:
158197
t = ts.touch_point
159198
if t is not None:
@@ -162,6 +201,10 @@ def get_touchscreen_choice():
162201
break
163202
else:
164203
touch_count = 0
204+
if wrapped_text_display.max_offset() > 0 and ticks_less(deadline, ticks_ms()):
205+
wrapped_text_display.scroll_next_line()
206+
wrapped_text_display.refresh()
207+
deadline = ticks_add(deadline, 5000 if wrapped_text_display.on_last_line() else 1000)
165208

166209
# Depending on the quadrant of the screen, make a choice
167210
x, y, _ = t
@@ -177,7 +220,10 @@ def run_game_step(forced_choice=None):
177220
choice = forced_choice
178221
else:
179222
choice = get_touchscreen_choice()
180-
print_wrapped(f"PLAYER: {choice}")
223+
wrapped_text_display.add_text(f"\nPLAYER: {choice}")
224+
wrapped_text_display.scroll_to_end()
225+
wrapped_text_display.refresh()
226+
181227
prompt = make_full_prompt(choice)
182228
for _ in range(3):
183229
result = get_one_completion(prompt)
@@ -186,7 +232,8 @@ def run_game_step(forced_choice=None):
186232
else:
187233
raise ValueError("Error getting completion from OpenAI")
188234
print(result)
189-
print_wrapped(result)
235+
wrapped_text_display.set_text(result)
236+
wrapped_text_display.refresh()
190237

191238
record_game_step(choice, result)
192239

0 commit comments

Comments
 (0)