這是例如如何將球員在船上
但它使用Linux特殊符號重繪板之前清除終端/主機。
import time
# --- constants --- (UPPER_CASE names)
SIZE_X = 10
SIZE_Y = 5
SYMBOL_EMPTY = '()'
SYMBOL_PLAYER = '(@)'
#SYMBOL_DANGER = '(!)'
# --- functions --- (lower_case names)
def print_board(board):
print('\33c') # clear terminal on Linux
for row in board:
print("".join(row))
def move_player(new_x, new_y):
global player_x
global player_y
# remove in old place
board[player_y][player_x] = SYMBOL_EMPTY
# put in new place
player_x = new_x
player_y = new_y
board[player_y][player_x] = SYMBOL_PLAYER
# --- main --- (lower_case names)
# creat board
board = [ ]
for row in range(SIZE_Y):
board.append([SYMBOL_EMPTY] * SIZE_X)
# display empty board
print_board(board)
time.sleep(0.5)
# set player position and symbol
player_x = player_y = 0
board[player_y][player_x] = SYMBOL_PLAYER
# display board with player
print_board(board)
time.sleep(0.5)
# move player
# move left
for new_x in range(SIZE_X):
move_player(new_x, player_y)
# redraw
print_board(board)
time.sleep(0.5)
# move down
for new_y in range(SIZE_Y):
move_player(player_x, new_y)
# redraw
print_board(board)
time.sleep(0.5)
真正的程序可能會更復雜。
在控制檯中可能會出現清除屏幕的問題,以重新繪製板子並獲取鍵/鼠標事件以使其真正實現互動。也許看到urwid或ncurses
模塊。
或嘗試如PyGame圖形模塊,Pyglet
'board [y] [x] =「Hello World!」' – furas
@furas你是什麼意思? – mrzippy01
如果你需要''(x,y)''用戶,那麼使用'board [y] [x] =「user_symbol」'。 – furas