Untitled
unknown
plain_text
14 days ago
2.3 kB
2
Indexable
import os import time def clear_screen(): """Clears the terminal screen.""" os.system('cls' if os.name == 'nt' else 'clear') def print_grid(grid): """Prints the current state of the grid.""" for row in grid: print(' '.join(row)) def main(): # Grid dimensions: 10 rows x 20 columns rows, cols = 10, 20 # Create a grid filled with dots. grid = [['.' for _ in range(cols)] for _ in range(rows)] # Mark the sea in the bottom row with '~' for j in range(cols): grid[rows - 1][j] = '~' # Starting position of the little duck: # Place the duck (D) in the first row, in the center. duck_row, duck_col = 0, cols // 2 grid[duck_row][duck_col] = 'D' while True: clear_screen() print("=== The Little Duck's Journey to the Sea ===") print_grid(grid) # Check if the duck has reached the sea. if duck_row == rows - 1: print("\nHooray! The little duck has reached the sea!") break move = input("\nEnter your move (w=up, a=left, s=down, d=right): ").lower() if move not in ['w', 'a', 's', 'd']: print("Invalid input! Please use only 'w', 'a', 's', or 'd'.") time.sleep(1) continue # Calculate the new position based on the input new_row, new_col = duck_row, duck_col if move == 'w': new_row -= 1 elif move == 's': new_row += 1 elif move == 'a': new_col -= 1 elif move == 'd': new_col += 1 # Check if the new position is within the bounds of the grid. if new_row < 0 or new_row >= rows or new_col < 0 or new_col >= cols: print("Oops! That move is out of bounds. Try another direction.") time.sleep(1) continue # Update the grid: # Replace the duck's current cell with '.' (or with '~' if it’s the sea row) grid[duck_row][duck_col] = '.' if duck_row < rows - 1 else '~' # Update the duck's position. duck_row, duck_col = new_row, new_col grid[duck_row][duck_col] = 'D' if __name__ == '__main__': main()
Editor is loading...
Leave a Comment