使用godot制作超级马里奥1-1之敌人制作

196 阅读2分钟

首先敌人的制作会相对简单一些,主要的就是马里奥水平方向碰到敌人和y轴上碰到敌人发生的变化,还有就是击中敌人有一个得分的效果,分数会一直叠加到桨命为止。

动画.gif 敌人的本身是有多种状态,会有移动和死亡等等,那么首先需要定义一个基类来定义一个基本的状态,实现方法跟主角 的制作基本一致,敌人有一个jumpedOn的函数,就是用来实现马里奥踩在敌人上面发生的情况,由于只实现了乌龟和蘑菇那两个敌人,所以相对比较简单。

var status=constants.walking
var dir=constants.left
var spriteIndex=0 #0普通颜色 1蓝色 2灰色
var time=0
var delTime=140  #删除时间 140帧
var maxYVel=constants.enemyMaxVel
#var init=false
var isOnFloor=true #是否在地面上


func _ready():
	._ready()
	gravity=constants.enemyGravity
	
	
func walking(delta):
	if yVel<maxYVel:
		yVel+=gravity*delta			
	position.x+=xVel*delta
	if !isOnFloor:	
		position.y+=yVel*delta
	

func deathJump(delta):
	if yVel<maxYVel:
		yVel+=gravity*delta	
	position.x+=xVel*delta
	position.y+=yVel*delta
	pass	
	
func jumpedOn():
	pass	

func startDeathJump():
	status=constants.deadJump
	yVel=-350
	if dir==constants.left:
		xVel=-45
	else:
		xVel=45
#	gravity=constants.deathJumpGravity
	z_index=3
	pass

func dead(delta):
	time+=1
	if time>=delTime:
		queue_free()
		pass
	pass

func destory():
	queue_free()
	
func turnLeft():
	dir=constants.left
	xVel=-abs(xVel)
	pass	

func turnRight():
	dir=constants.right
	xVel=abs(xVel)
	pass	

需要注意的是乌龟可以变成壳然后可以被推走,这个就是单独判断是不是壳。需要注意的是乌龟可以变成壳然后可以被推走,这个就是单独判断是不是壳。

分数的累加是根据时间间隔来进行判断,这个时间不能是系统时间,应该是游戏时间,就是标题上的时间,然后设置一个分数数组,如果数组里面的分数都叠加完了,就是桨命了。

func addScore(m,_score=100,isCumulative=false,_scoreIndex=0):
	var temp=score.instance()
	temp.setPos(Vector2(m.position.x,m.position.y-45))
	if isCumulative: #分数累加
		if abs(_title.getTime()-lastScoreTime)>scoreTick:
			if _scoreIndex!=0:
				scoreIndex=_scoreIndex
			else:	
				scoreIndex=0
			print(constants.scoreList[scoreIndex])
			temp.setScore(constants.scoreList[scoreIndex])
			lastScoreTime=_title.getTime()
			_title.addScore(constants.scoreList[scoreIndex])
		else:
			if _scoreIndex!=0:
				scoreIndex=_scoreIndex
			else:	
				scoreIndex+=1
			if scoreIndex>=constants.scoreList.size():
				temp.setScore("1up")
				Game.playerData['lives']+=1
				SoundsUtil.playItem1up()
			else:
				temp.setScore(constants.scoreList[scoreIndex])	
				_title.addScore(constants.scoreList[scoreIndex])
	else:	
		temp.setScore(_score)
		_title.addScore(_score)
	_otherobjList.add_child(temp)
	pass
#游戏中分数
const scoreList=[100,200,400,500,800,1000,2000,4000,5000,8000]

分数的叠加不是很复杂,只要超过时间就是从新开始进行分数累加了。 其他想到在补充。

参考资料:
github.com/absolve/god… (项目文件夹mario1)
godotengine.org/