移动播放器绕板
问题描述:
所以我用下面的代码做了一个网格:移动播放器绕板
board = []
x = choice_size
y = x
for row in range(0,x):
board.append(["()"]*y)
def print_board(board):
for row in board:
print(" ".join(row))
print_board(board)
我需要给用户选择进入输入(如“(左)10最多2”),然后移动他们到板上的相应位置。 我试着把它们的数字添加到变量,然后得到列表中的x和y位置“board []”,但我似乎无法让它工作。
答
这是例如如何将球员在船上
但它使用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
模块。
答
试着想通过加上或减去一个数,具体取决于哪个方向,玩家想要移动改变X/Y值。
'board [y] [x] =“Hello World!”' – furas
@furas你是什么意思? – mrzippy01
如果你需要''(x,y)''用户,那么使用'board [y] [x] =“user_symbol”'。 – furas