「網易官方」極客戰記(codecombat)攻略-沙漠-掠奪者

「網易官方」極客戰記(codecombat)攻略-沙漠-掠奪者

這裡有好多錢有沒有人要呀? 沒有人我就要啦~

簡介

那個前行的部隊有很多金幣。為了拿到手,你要打破他們的陣營!

首先,用 while 語句收集所有存在的金幣。

coin = hero.findNearest(hero.findItems())

while coin:

# 收集金幣

coin = hero.findNearest(hero.findItems())

然後, 當(while) 最近敵人的 health 大於 0 時一直攻擊。

默認代碼

# 打幾下泡泡人撿走掉出的幣

while True:

coin = hero.findNearestItem()

# 當存在金幣時:

# 移動到金幣處。

# ‘coin’應該是最近的那枚 撿到手以後要另找一枚最近的

enemy = hero.findNearestEnemy()

if enemy:

# 如果敵人還會動

# 就打它

pass

概覽

while 表達式是一種有用的結構, 例如,它允許您在存在某些內容時執行一些操作。 例如,您可以在活著時觀察敵人的健康狀況。

while robot.health > 0:

hero.say("The robot's alive")

while-expression 允許檢查一些 thangs 的存在:

weakOgre = hero.findNearestEnemy()

while weakOgre:

hero.attack(weakOgre)

hero.say("One hit, one frag! Next!")

weakOgre = hero.findNearestEnemy() #重新分配(更新)變量

掠奪者解法

# 打幾下泡泡人撿走掉出的幣

while True:

coin = hero.findNearestItem()

# 當存在金幣時:

while coin:

# 移動到金幣處。

hero.moveXY(coin.pos.x, coin.pos.y)

# ‘coin’應該是最近的那枚 撿到手以後要另找一枚最近的

coin = hero.findNearest(hero.findItems())

enemy = hero.findNearestEnemy()

if enemy:

# 如果敵人還會動

while enemy.health > 0:

# 就打它

hero.attack(enemy)


本攻略發於極客戰記官方教學欄目,原文地址為:

https://codecombat.163.com/news/jikezhanji-lueduozhe

極客戰記——學編程,用玩的!


分享到:


相關文章: