-1
我創建了一個功能齊全的網格。有一名玩家可以四處移動並收集寶箱和地精。雖然在我產卵的地精和地精中有問題。他們擴大了我不想發生的網格大小。任何人都可以幫助解決這個問題嗎?需要修復的部分是ChestsandGoblins的功能。這是我的代碼:Python使用列表操作
from random import *
# Set up Initial Variables
Money = "0"
grid = []
character = "X"
# player_loc will hold the x, y location of the player
player_loc = (0, 0)
# These are a mapping of direction
treasure_loc = (0, 0)
NORTH = "N"
SOUTH = "S"
EAST = "E"
WEST = "W" #All variables used for Later on
Treasure = "T"
Goblin = "G"
def setupGrid():
global grid
global row
global N
N = input("How big would you like the grid to be?")
for x in range(0, (int(N))):
row = []
for y in range(0, (int(N))):
if x == player_loc[0] and y == player_loc[1]:
row.append(character)
else:
row.append('O')
grid.append(row)
def Chests_and_Goblins():
global grid
global row
global N
print("How many chests would you like in the grid?")
B = input("The amount of chests you like is given by the amount of C's")
for each in B:
grid[randint(0, (int(N)))].insert(randint(0, (int(N))), Treasure)
grid[randint(0, (int(N)))].insert(randint(0, (int(N))), Goblin)
def moveSouth(n):
global player_loc
grid[player_loc[0]][player_loc[1]] = "O"
grid[player_loc[0] + n][player_loc[1]] = character
player_loc = (player_loc[0] + n, player_loc[1])
def moveNorth(n):
global player_loc
grid[player_loc[0]][player_loc[1]] = "O"
grid[player_loc[0] - n][player_loc[1]] = character
player_loc = (player_loc[0] - n, player_loc[1])
def moveEast(n):
global player_loc
grid[player_loc[0]][player_loc[1]] = "O"
grid[player_loc[0]][player_loc[1] + n] = character
player_loc = (player_loc[0], player_loc[1] + n)
def moveWest(n):
global player_loc
grid[player_loc[0]][player_loc[1]] = "O"
grid[player_loc[0]][player_loc[1] - n] = character
player_loc = (player_loc[0], player_loc[1] - n)
def gridRunner():
while True:
for row in grid:
print (row)
switch = {NORTH : moveNorth,
SOUTH : moveSouth,
EAST : moveEast,
WEST : moveWest }
P = input("What direction would you like to move in? North (N), South(S), East(E) or West(W)?").upper()
if P not in switch:
print ("invalid move")
continue
distance = int(input("How far would you like to move in this direction? (blocks are the units)"))
switch[P](distance)
setupGrid()
Chests_and_Goblins()
gridRunner()
謝謝!儘管我不明白你編寫的其他代碼的作用,但代碼的最高位幫助不大。沒有它,我的代碼似乎可以正常工作。再次感謝 –
啊,我現在明白了。雖然我不知道把它放在代碼中。你可以幫我嗎? –