6
6
import adafruit_esp32spi .adafruit_esp32spi_socket as socket
7
7
import adafruit_requests as requests
8
8
import adafruit_touchscreen
9
+ from adafruit_bitmap_font .bitmap_font import load_font
10
+ from adafruit_display_text .bitmap_label import Label
11
+ from adafruit_display_text import wrap_text_to_pixels
9
12
import board
10
13
import displayio
11
14
import supervisor
26
29
# Place the key in your settings.toml file
27
30
openai_api_key = os .getenv ("OPENAI_API_KEY" )
28
31
32
+ nice_font = load_font ("helvR10.pcf" )
33
+ line_spacing = 0.75
34
+
29
35
# Customize this prompt as you see fit to create a different experience
30
36
base_prompt = """
31
37
You are an AI helping the player play an endless text adventure game. You will stay in character as the GM.
@@ -99,23 +105,13 @@ def terminal_palette(fg=0xffffff, bg=0):
99
105
return p
100
106
101
107
def print_wrapped (text ):
102
- print (text )
103
- maxwidth = main_text .width
104
- for line in text .split ("\n " ):
105
- col = 0
106
- sp = ''
107
- for word in line .split ():
108
- newcol = col + len (sp ) + len (word )
109
- if newcol < maxwidth :
110
- terminal .write (sp + word )
111
- col = newcol
112
- else :
113
- terminal .write ('\r \n ' )
114
- terminal .write (word )
115
- col = len (word )
116
- sp = ' '
117
- if sp or not line :
118
- terminal .write ('\r \n ' )
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
119
115
board .DISPLAY .refresh ()
120
116
121
117
def make_full_prompt (action ):
@@ -131,8 +127,8 @@ def record_game_step(action, response):
131
127
132
128
def get_one_completion (full_prompt ):
133
129
if not use_openai :
134
- return f"""This is a canned response in offline mode. The player's last
135
- choice was as follows: { full_prompt [- 1 ]['content' ]} """ .strip ()
130
+ return f"""This is a canned response in offline mode. The player's last choice was as follows:
131
+ { full_prompt [- 1 ]['content' ]} """ .strip ()
136
132
try :
137
133
response = requests .post (
138
134
"https://api.openai.com/v1/chat/completions" ,
@@ -181,7 +177,7 @@ def run_game_step(forced_choice=None):
181
177
choice = forced_choice
182
178
else :
183
179
choice = get_touchscreen_choice ()
184
- print_wrapped (f"\n \n PLAYER : { choice } " )
180
+ print_wrapped (f"PLAYER : { choice } " )
185
181
prompt = make_full_prompt (choice )
186
182
for _ in range (3 ):
187
183
result = get_one_completion (prompt )
@@ -190,7 +186,6 @@ def run_game_step(forced_choice=None):
190
186
else :
191
187
raise ValueError ("Error getting completion from OpenAI" )
192
188
print (result )
193
- terminal .write (clear )
194
189
print_wrapped (result )
195
190
196
191
record_game_step (choice , result )
@@ -218,29 +213,23 @@ def run_game_step(forced_choice=None):
218
213
219
214
# Determine the size of everything
220
215
glyph_width , glyph_height = terminalio .FONT .get_bounding_box ()
221
- use_height = board .DISPLAY .height - 8
222
- use_width = board .DISPLAY .width - 8
223
- terminal_width = use_width // glyph_width
224
- terminal_height = use_height // glyph_height - 4
216
+ use_height = board .DISPLAY .height - 4
217
+ use_width = board .DISPLAY .width - 4
225
218
226
219
# Game text is displayed on this wdget
227
- main_text = displayio .TileGrid (terminalio .FONT .bitmap , pixel_shader = terminal_palette (),
228
- width = terminal_width , height = terminal_height , tile_width = glyph_width ,
229
- tile_height = glyph_height )
230
- main_text .x = 4
231
- main_text .y = 4 + glyph_height
232
- terminal = terminalio .Terminal (main_text , terminalio .FONT )
233
- main_group .append (main_text )
220
+ terminal = Label (font = nice_font , color = 0xffffff , background_color = 0 , line_spacing = line_spacing , anchor_point = (0 ,0 ), anchored_position = (0 ,glyph_height + 1 ))
221
+ max_lines = (use_height - 2 * glyph_height ) // int (nice_font .get_bounding_box ()[1 ] * terminal .line_spacing )
222
+ main_group .append (terminal )
234
223
235
224
# Indicate what each quadrant of the screen does when tapped
236
225
label_width = use_width // (glyph_width * 2 )
237
226
main_group .append (terminal_label ('1' , label_width , terminal_palette (0 , 0xffff00 ), 0 , 0 ))
238
227
main_group .append (terminal_label ('2' , label_width , terminal_palette (0 , 0x00ffff ),
239
228
use_width - label_width * glyph_width , 0 ))
240
229
main_group .append (terminal_label ('3' , label_width , terminal_palette (0 , 0xff00ff ),
241
- 0 , use_height - 2 * glyph_height ))
230
+ 0 , use_height - glyph_height ))
242
231
main_group .append (terminal_label ('4' , label_width , terminal_palette (0 , 0x00ff00 ),
243
- use_width - label_width * glyph_width , use_height - 2 * glyph_height ))
232
+ use_width - label_width * glyph_width , use_height - glyph_height ))
244
233
245
234
# Show our stuff on the screen
246
235
board .DISPLAY .auto_refresh = False
@@ -258,7 +247,7 @@ def run_game_step(forced_choice=None):
258
247
run_game_step ()
259
248
except Exception as e : # pylint: disable=broad-except
260
249
traceback .print_exception (e )
261
- terminal . write (f"{ clear } An error occurred (more details on REPL).\r \n Touch the screen to re-load" )
250
+ print_wrapped (f"An error occurred (more details on REPL).\n Touch the screen to re-load" )
262
251
board .DISPLAY .refresh ()
263
252
get_touchscreen_choice ()
264
253
supervisor .reload ()
0 commit comments