2017-05-28 67 views
0

只是做簡單的地圖,其中包括到地圖使用功能的播放器的位置,但球員沒有出現(映射爲空)簡單的地圖功能

這裏是我的代碼。請幫忙解決。由於

import random 

CELLS = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), 
     (0, 1), (1, 1), (2, 1), (3, 1), (4, 1), 
     (0, 2), (1, 2), (2, 2), (3, 2), (4, 2), 
     (0, 3), (1, 3), (2, 3), (3, 3), (4, 3), 
     (0, 4), (1, 4), (2, 4), (3, 4), (4, 4)] 

def get_locations(): 

    return random.sample(CELLS, 1) 

player = get_locations() 

def draw_map(player): 

    print(" _" * 5) 
    tile = "|{}" 


    for cell in CELLS: 
     x, y = cell 

     if x < 4: 
      line_end = "" 
      if cell == player: 
       output = tile.format("X") 
      else: 
       output = tile.format("_") 
     else: 
      line_end = "\n" 
      if cell == player: 
       output = tile.format("X|") 
      else: 
       output = tile.format("_|") 
     print(output, end=line_end) 


draw_map(player) 

回答

2

你需要random.choice(CELLS),不random.sample(CELLS,1)

random.choice(seq)返回一個隨機元素進行seq

(1, 3) 

random.sample(seq, 1)回報和一個隨機元素seq一個子表:

[(1, 3)] 

有了這個小小的變化,你的程序輸出:

_ _ _ _ _ 
|_|_|_|_|_| 
|_|_|_|_|_| 
|_|_|_|_|_| 
|_|X|_|_|_| 
|_|_|_|_|_| 
+0

非常感謝,注意到。非常感謝一次 –