def test_TilePlane_conversions(): print("TESTING conversions") W = 4 H = 5 tiles_1D = [str(i) for i in range(W * H)] print("ORIGINAL:\n", tiles_1D, end='\n\n') tiles_2D = TilePlane.tilelist_1D_to_2D(W, H, tiles_1D, default='.') print("2D VERSION:\n", *tiles_2D, sep='\n', end='\n\n') converted_back_to_1D = TilePlane.tilelist_2D_to_1D(tiles_2D) print("2D TO 1D:\n", tiles_1D, end='\n\n')
def test_TilePlane_new_from_2D_padded(): print("TESTING TilePlane.new_from_2D_padded()") W, H = 5, 4 tile_list = [[1, 2, 3], [4, 5], [7, 8, 9, 0], [1, 2, 3, 4, 5, 6]] tp = TilePlane.new_from_2D_padded(W, H, tile_list, '#') tp.display() print()
def test_TilePlane_project(): print("TESTING tp.project()") print("BACKGROUND:") bg_tp = TilePlane.new_filled(20, 20, '.') bg_tp.display() print("FOREGROUND:") fg_tp = TilePlane.new_filled(2, 2, '@') fg_tp.display() print("PROJECTED:") fg_tp.project(bg_tp, 1, 1) fg_tp.project(bg_tp, -1, 4) fg_tp.project(bg_tp, 19, 8) fg_tp.project(bg_tp, 22, 11) fg_tp.project(bg_tp, 5, -1) fg_tp.project(bg_tp, 12, 19) fg_tp.project(bg_tp, 25, 25) bg_tp.display() print()
def test_TilePlane_display(): print("TESTING tp.display()") W = 4 H = 5 tp = TilePlane.new_from_1D(W, H, [str(i % W) for i in range(W * H)], default='.') tp.display() print()
def test_TilePlane_fill(): print("TESTING tp.fill()") print("BEFORE:") w, h = 6, 6 main_plane = TilePlane.new_from_1D( w, h, ["abcdefghijklmnopqrstuvwxyz"[i % 26] for i in range(w * h)]) main_plane.display() print() print("AFTER:") main_plane.fill('&') main_plane.display() print()
def test_TilePlane_subplane(): print("TESTING tp.subplane()") print("MAIN:") w, h = 20, 10 main_plane = TilePlane.new_from_1D( w, h, ["abcdefghijklmnopqrstuvwxyz"[i % 26] for i in range(w * h)]) main_plane.display() print() print("SUB:") sub = main_plane.subplane(2, 2, 4, 4) sub.display() print()
def test_TilePlane_get_set_tile(): print("TESTING tp.get_tile() and tp.set_tile()") W = 10 H = 6 tp = TilePlane.new_filled(W, H, '0') print(*tp.tilelist, sep='\n') print("ORIGINAL:") tp.display() x, y = 2, 4 val = '#' tp.set_tile(x, y, val) print(f"AFTER SETTING TILE {x},{y} to {val}:") tp.display() print(f"GET TILE {x},{y}:", tp.get_tile(x, y)) print()
def main(): # CONSTANTS SCREEN_DIM = 60, 20 HALF_SCREEN_DIM = SCREEN_DIM[0] // 2, SCREEN_DIM[1] // 2 MAP_DIM = 100, 50 BG = '|' # SETUP VALID_CHARS = r'abcdefghijklmnopqrstuvwxyz1234567890-[]\;:/?<>,.~!@#$%^&*()_={}|` ' previous_char = ' ' display = TilePlane.new_filled(*SCREEN_DIM, BG) screen_buffer = TilePlane.new_filled(*MAP_DIM, fill='') map_buffer = TilePlane.new_filled(*MAP_DIM, fill=' ') text_display = Text("Hello World!", visible=True) cursor = Cursor(25, 5, *HALF_SCREEN_DIM) # Draw the map to the screen buffer map_buffer.project(screen_buffer, 0, 0) # Draw the cursor onto the screen buffer cursor.draw(screen_buffer) # Transfer the screen buffer to the display screen_buffer.project(display, *cursor.get_camera_pos()) # Display text text_display.text = f" POS: ({cursor.x}, {cursor.y}) \n CHAR: {map_buffer.get_tile(cursor.x, cursor.y)} " text_display.draw(display, 3, 1) # Print the screen print(term.home + term.clear, end='') display.display() while True: # NEXT TASK: take keyboard input to move around screen with term.cbreak(), term.hidden_cursor(): inp = term.inkey() if inp == 'q': break elif inp in VALID_CHARS: map_buffer.set_tile(cursor.x, cursor.y, inp[0]) previous_char = inp[0] elif inp == '\t': map_buffer.set_tile(cursor.x, cursor.y, previous_char) elif repr(inp) == 'KEY_LEFT': cursor.x -= 1 elif repr(inp) == 'KEY_RIGHT': cursor.x += 1 elif repr(inp) == 'KEY_UP': cursor.y -= 1 elif repr(inp) == 'KEY_DOWN': cursor.y += 1 # UPDATE STUFF # Fill the screen display.fill(BG) # Draw the map to the screen buffer map_buffer.project(screen_buffer, 0, 0) # Draw the cursor onto the screen buffer cursor.draw(screen_buffer) # Transfer the screen buffer to the display screen_buffer.project(display, *cursor.get_camera_pos()) # Update text display text_display.text = f" POS: ({cursor.x}, {cursor.y}) \n CHAR: {map_buffer.get_tile(cursor.x, cursor.y)} " text_display.draw(display, 3, 1) # Print the screen print(term.home + term.clear, end='') display.display() print(term.home + term.clear, end='') print("ALL DONE!")
def test_TilePlane_new_filled(): print("TESTING TilePlane.new_filled()") W = 10 H = 6 tp = TilePlane.new_filled(W, H, '0') tp.display()
def main(): from random import choice, randint # CONSTANTS SCREEN_DIM = 60, 20 MAP_DIM = 100, 50 BG = '|' # SETUP display = TilePlane.new_filled(*SCREEN_DIM, BG) screen_buffer = TilePlane.new_filled(*MAP_DIM, fill='') map_buffer = TilePlane.new_filled(*MAP_DIM, fill='.') # Draw a bunch of blobs to the map buffer iterations = 500 for i in range(iterations): x = randint(0, MAP_DIM[0]) y = randint(0, MAP_DIM[1]) map_buffer.set_tile(x, y, choice(['*', '=', '%'])) # Initialize the player graphic player = TilePlane(3, 1, tiles=['{', '&', '}']) # Set player position cam_x, cam_y = 2, 2 player_x, player_y = SCREEN_DIM[0] // 2 - 2, SCREEN_DIM[1] // 2 - 1 # Draw the map to the screen buffer map_buffer.project(screen_buffer, 0, 0) # Draw the player to the screen buffer player.project(screen_buffer, -cam_x + player_x, -cam_y + player_y) # Transfer the screen buffer to the display screen_buffer.project(display, cam_x, cam_y) # Print the screen print(term.home + term.clear, end='') display.display() while True: # NEXT TASK: take keyboard input to move around screen with term.cbreak(), term.hidden_cursor(): inp = term.inkey() if inp == 'q': break elif repr(inp) == 'KEY_LEFT': cam_x += 1 elif repr(inp) == 'KEY_RIGHT': cam_x -= 1 elif repr(inp) == 'KEY_UP': cam_y += 1 elif repr(inp) == 'KEY_DOWN': cam_y -= 1 # UPDATE STUFF # Fill the screen display.fill('|') # Draw the map to the screen buffer map_buffer.project(screen_buffer, 0, 0) # Draw the cursor player.project(screen_buffer, -cam_x + player_x, -cam_y + player_y) # Transfer the screen buffer to the display screen_buffer.project(display, cam_x, cam_y) # Print the screen print(term.home + term.clear, end='') display.display() print(term.home + term.clear, end='') print("ALL DONE!")