sk5s 自主學習Time

作業一

Jump to Section

成品及時展示:

思路說明

讀取操作者輸入的字母,依據不同字母執行不同指令(使用 if elif else 完成),每次輸入完字母後重新繪製地圖。

step 1

w, h = 10, 5   # 設定寬為 5,高為 3
i, j = 0, 0    # 初始化印刷位置
x, y = 2, 3  # 使用者初始位置
while i < h:     # 高度判斷條件
  j=0        # 初始化 j
  content = ""
  while j<w: # 寬度判斷條件
    content += "-"
    j+=1
  print(content)
  i+=1

step 2

w, h = 10, 5   # 設定寬為 5,高為 3
i, j = 0, 0    # 初始化印刷位置
x, y = 2, 3  # 使用者初始位置
while i < h:     # 高度判斷條件
  j=0        # 初始化 j
  content = ""
  while j<w: # 寬度判斷條件
    if i == x and j == y:
      content += "*"
    else:
      content += "-"
    j+=1
  print(content)
  i+=1

step 3

#from IPython.display import clear_output
w, h = 10, 5   # 設定寬為 5,高為 3
x, y = 2, 3  # 使用者初始位置
while True:
  #clear_output() # 清除前一步內容(若輸入條會消失請移除此行)
  i, j = 0, 0    # 初始化印刷位置
  print('當前座標: (', x, ',', y, ')')
  while i < h:     # 高度判斷條件
    j=0        # 初始化 j
    content = ""
    while j<w: # 寬度判斷條件
      if i == x and j == y:
        content += "*"
      else:
        content += "-"
      j+=1
    print(content)
    i+=1
  # 讀取使用者指令
  control = input('按 W/A/S/D 來移動星星(q 結束)!\n')
  control = control.lower()
  
  # 更新使用者位置
  if control == "w":   # w 往上
    x -= 1
  elif control == "s": # s 往下
    x += 1
  elif control == "a": # a 往左
    y -= 1
  elif control == "d": # d 往右
    y += 1
  elif control == "q": # q 結束
    break