我们需要一些东西来抵抗食人魔。
简介
在吃豆人街机游戏系列的第四级,我们将增加能量提升 让英雄暂时击败敌人。
它们将会是有时间限制的加速器,短时间内翻转 "hunter-and-prey" 机制 - 当加电启动时,敌人将从玩家处运行。
这是准备测试游戏的最后一步,所以它会比前一个更长。 使用基于时间的函数, "collide" 和 "collect" 事件和抽象来使代码更清晰。
使用键盘或鼠标进行播放器控制:
- W -> 向上
- S -> 向下
- A -> 左边
- D -> 右边
- 鼠标点击 -> 移动到该点
默认代码
# 创建Pac-Man风格街机游戏的第4步。
# 游戏布局和项目。 向下滚动。
game.spawnXY("forest", 16, 16)
game.spawnXY("forest", 32, 16)
game.spawnXY("forest", 48, 16)
game.spawnXY("forest", 64, 16)
game.spawnXY("forest", 16, 32)
game.spawnXY("forest", 32, 32)
game.spawnXY("forest", 48, 32)
game.spawnXY("forest", 64, 32)
game.spawnXY("forest", 16, 48)
game.spawnXY("forest", 32, 48)
game.spawnXY("forest", 48, 48)
game.spawnXY("forest", 64, 48)
game.spawnXY("bronze-coin", 16, 8)
game.spawnXY("bronze-coin", 24, 8)
game.spawnXY("bronze-coin", 32, 8)
game.spawnXY("bronze-coin", 48, 8)
game.spawnXY("bronze-coin", 56, 8)
game.spawnXY("bronze-coin", 64, 8)
game.spawnXY("bronze-coin", 72, 8)
game.spawnXY("bronze-coin", 8, 16)
game.spawnXY("bronze-coin", 24, 16)
game.spawnXY("bronze-coin", 40, 16)
game.spawnXY("bronze-coin", 56, 16)
game.spawnXY("bronze-coin", 72, 16)
game.spawnXY("bronze-coin", 8, 24)
game.spawnXY("bronze-coin", 16, 24)
game.spawnXY("bronze-coin", 24, 24)
game.spawnXY("bronze-coin", 32, 24)
game.spawnXY("bronze-coin", 40, 24)
game.spawnXY("bronze-coin", 48, 24)
game.spawnXY("bronze-coin", 56, 24)
game.spawnXY("bronze-coin", 64, 24)
game.spawnXY("bronze-coin", 72, 24)
game.spawnXY("bronze-coin", 24, 32)
game.spawnXY("bronze-coin", 56, 32)
game.spawnXY("bronze-coin", 8, 40)
game.spawnXY("bronze-coin", 16, 40)
game.spawnXY("bronze-coin", 24, 40)
game.spawnXY("bronze-coin", 32, 40)
game.spawnXY("bronze-coin", 40, 40)
game.spawnXY("bronze-coin", 48, 40)
game.spawnXY("bronze-coin", 56, 40)
game.spawnXY("bronze-coin", 64, 40)
game.spawnXY("bronze-coin", 72, 40)
game.spawnXY("bronze-coin", 8, 48)
game.spawnXY("bronze-coin", 24, 48)
game.spawnXY("bronze-coin", 40, 48)
game.spawnXY("bronze-coin", 56, 48)
game.spawnXY("bronze-coin", 72, 48)
game.spawnXY("bronze-coin", 8, 56)
game.spawnXY("bronze-coin", 16, 56)
game.spawnXY("bronze-coin", 24, 56)
game.spawnXY("bronze-coin", 32, 56)
game.spawnXY("bronze-coin", 48, 56)
game.spawnXY("bronze-coin", 56, 56)
game.spawnXY("bronze-coin", 64, 56)
game.spawnXY("bronze-coin", 72, 56)
game.spawnXY("mushroom", 40, 8)
game.spawnXY("mushroom", 8, 32)
game.spawnXY("mushroom", 72, 32)
game.spawnXY("mushroom", 40, 56)
game.score = 1000
# 加电持续时间。
game.powerDuration = 6
# 时间随强化流逝。用于UI。
game.powerTime = 0
# 加电时间到了。主动使用。
game.powerEndTime = 0
ui.track(game, "time")
ui.track(game, "score")
# 为game.powerTime添加ui:
ui.track(game, "powerTime")
game.addCollectGoal()
game.addSurviveGoal();
player = game.spawnPlayerXY("knight", 8, 8)
player.maxSpeed = 30
# 这个功能使玩家强大。
def powerPlayerUp():
player.scale = 2
player.attackDamage = 100
game.powerEndTime = game.time + game.powerDuration
# 函数返回玩家在正常状态。
def powerPlayerDown():
player.scale = 1
player.attackDamage = 1
def onCollect(event):
player = event.target
item = event.other
if item.type == "bronze-coin":
game.score += 1
if item.type == "mushroom":
game.score += 5
# 使用powerPlayerUp来强化玩家:
def onCollide(event):
player = event.target
other = event.other
# 如果其他是"scout",玩家的比例是2:
# 用可取胜得方法击败对方:
player.on("collect", onCollect)
# 为玩家的"collide"事件分配事件处理程序:
generator = game.spawnXY("generator", 41, 31)
generator.spawnType = "scout"
generator.spawnDelay = 6;
def onSpawn(event):
unit = event.target
unit.maxSpeed = 8
unit.attackDamage = player.maxHealth
while True:
# 敌人逃离了强大得玩家。
if player.scale == 2:
unit.behavior = "RunsAway"
else:
unit.behavior = "AttacksNearest"
def onDefeat(event):
# 为被击败的敌人增加game.score:
game.score += 10
game.setActionFor("scout", "spawn", onSpawn)
game.setActionFor("scout", "defeat", onDefeat)
def checkTimeScore():
game.score -= 0.5
if game.score < 0:
game.score = 0
def checkPowerTimer():
# 剩余强化时间。
game.powerTime = game.powerEndTime - game.time
if game.powerTime <= 0:
game.powerTime = 0
if player.scale == 2:
powerPlayerDown()
# 让我们结合所有基于时间的函数。
def checkTimers():
checkTimeScore()
checkPowerTimer()
while True:
checkTimers()
# 赢得游戏胜利。
概览
玩家不喜欢总是遇到敌人就逃跑...... 他们需要某种方式来反击!
它不一定是真正的战斗。 它可能会受到时间的限制,陷阱,棘手的行为(如跳跃),等等。
加电通常是有时间限制的,否则,它们不是加电,而是技能升级。 改变加电时间可以让你修改游戏的难度。
硬币游戏第 4 步:上电 解法
# 创建Pac-Man风格街机游戏的第4步。
# 游戏布局和项目。 向下滚动。
game.spawnXY("forest", 16, 16)
game.spawnXY("forest", 32, 16)
game.spawnXY("forest", 48, 16)
game.spawnXY("forest", 64, 16)
game.spawnXY("forest", 16, 32)
game.spawnXY("forest", 32, 32)
game.spawnXY("forest", 48, 32)
game.spawnXY("forest", 64, 32)
game.spawnXY("forest", 16, 48)
game.spawnXY("forest", 32, 48)
game.spawnXY("forest", 48, 48)
game.spawnXY("forest", 64, 48)
game.spawnXY("bronze-coin", 16, 8)
game.spawnXY("bronze-coin", 24, 8)
game.spawnXY("bronze-coin", 32, 8)
game.spawnXY("bronze-coin", 48, 8)
game.spawnXY("bronze-coin", 56, 8)
game.spawnXY("bronze-coin", 64, 8)
game.spawnXY("bronze-coin", 72, 8)
game.spawnXY("bronze-coin", 8, 16)
game.spawnXY("bronze-coin", 24, 16)
game.spawnXY("bronze-coin", 40, 16)
game.spawnXY("bronze-coin", 56, 16)
game.spawnXY("bronze-coin", 72, 16)
game.spawnXY("bronze-coin", 8, 24)
game.spawnXY("bronze-coin", 16, 24)
game.spawnXY("bronze-coin", 24, 24)
game.spawnXY("bronze-coin", 32, 24)
game.spawnXY("bronze-coin", 40, 24)
game.spawnXY("bronze-coin", 48, 24)
game.spawnXY("bronze-coin", 56, 24)
game.spawnXY("bronze-coin", 64, 24)
game.spawnXY("bronze-coin", 72, 24)
game.spawnXY("bronze-coin", 24, 32)
game.spawnXY("bronze-coin", 56, 32)
game.spawnXY("bronze-coin", 8, 40)
game.spawnXY("bronze-coin", 16, 40)
game.spawnXY("bronze-coin", 24, 40)
game.spawnXY("bronze-coin", 32, 40)
game.spawnXY("bronze-coin", 40, 40)
game.spawnXY("bronze-coin", 48, 40)
game.spawnXY("bronze-coin", 56, 40)
game.spawnXY("bronze-coin", 64, 40)
game.spawnXY("bronze-coin", 72, 40)
game.spawnXY("bronze-coin", 8, 48)
game.spawnXY("bronze-coin", 24, 48)
game.spawnXY("bronze-coin", 40, 48)
game.spawnXY("bronze-coin", 56, 48)
game.spawnXY("bronze-coin", 72, 48)
game.spawnXY("bronze-coin", 8, 56)
game.spawnXY("bronze-coin", 16, 56)
game.spawnXY("bronze-coin", 24, 56)
game.spawnXY("bronze-coin", 32, 56)
game.spawnXY("bronze-coin", 48, 56)
game.spawnXY("bronze-coin", 56, 56)
game.spawnXY("bronze-coin", 64, 56)
game.spawnXY("bronze-coin", 72, 56)
game.spawnXY("mushroom", 40, 8)
game.spawnXY("mushroom", 8, 32)
game.spawnXY("mushroom", 72, 32)
game.spawnXY("mushroom", 40, 56)
game.score = 1000
# 加电持续时间。
game.powerDuration = 6
# 时间随强化流逝。用于UI。
game.powerTime = 0
# 加电时间到了。主动使用。
game.powerEndTime = 0
ui.track(game, "time")
ui.track(game, "score")
# 为game.powerTime添加ui:
ui.track(game, "powerTime")
game.addCollectGoal()
game.addSurviveGoal();
player = game.spawnPlayerXY("knight", 8, 8)
player.maxSpeed = 30
# 这个功能使玩家强大。
def powerPlayerUp():
player.scale = 2
player.attackDamage = 100
game.powerEndTime = game.time + game.powerDuration
# 函数返回玩家在正常状态。
def powerPlayerDown():
player.scale = 1
player.attackDamage = 1
def onCollect(event):
player = event.target
item = event.other
if item.type == "bronze-coin":
game.score += 1
if item.type == "mushroom":
game.score += 5
# 使用powerPlayerUp来强化玩家:
powerPlayerUp()
def onCollide(event):
player = event.target
other = event.other
# 如果其他是"scout",玩家的比例是2:
if other.type == "scout" and player.scale == 2:
# 用可取胜得方法击败对方:
other.defeat()
player.on("collect", onCollect)
# 为玩家的"collide"事件分配事件处理程序:
generator = game.spawnXY("generator", 41, 31)
generator.spawnType = "scout"
generator.spawnDelay = 6;
def onSpawn(event):
unit = event.target
unit.maxSpeed = 8
unit.attackDamage = player.maxHealth
while True:
# 敌人逃离了强大得玩家。
if player.scale == 2:
unit.behavior = "RunsAway"
else:
unit.behavior = "AttacksNearest"
def onDefeat(event):
# 为被击败的敌人增加game.score:
game.score += 10
game.setActionFor("scout", "spawn", onSpawn)
game.setActionFor("scout", "defeat", onDefeat)
def checkTimeScore():
game.score -= 0.5
if game.score < 0:
game.score = 0
def checkPowerTimer():
# 剩余强化时间。
game.powerTime = game.powerEndTime - game.time
if game.powerTime <= 0:
game.powerTime = 0
if player.scale == 2:
powerPlayerDown()
# 让我们结合所有基于时间的函数。
def checkTimers():
checkTimeScore()
checkPowerTimer()
while True:
checkTimers()
# 赢得游戏胜利。
本攻略发于极客战记官方教学栏目,原文地址为:
https://codecombat.163.com/news/jikezhanji-yingbiyouxishangdian