Enemy Movement
👉 BlitzCoder will be building a new platform and other plans to preserve and continue the Blitz legacy.
To be able to achieve this goal, we need your support by becoming a Patreon Paid Member 👈
Tweet artificial-intelligence monkey-x code-archives tutorials
Simple enemy movement in Monkey-X
Enemies are quite similar to players. You update the player by checking which way he wants to move based on keys pressed or whatever. You do the same with enemies based on what they want to do - in this case patrolling between x=0 and x=400, switching direction when he touches either. I would write something like the following - it's slightly longer than it could be, but it's more systematic than updating in OnRender etc
I made the base image global in case you want to have lots of enemies using the same image. Never (in small simple programs, anyway) load images more than once.
Author: Gerry Quinn
Global enemyImage:Image = LoadImage( "enemy.png" )
Class enemy
Field enemy1Sprite:Image
Field ex:Int
Field ey:Int
Field exspeed:Int
Field xLeft:Int
Field xRight:Int
' Called when enemy is created (could be in New())
Method initialise()
enem1Sprite = enemyImage
ex = 100
ey = 100
xLeft = 0 + enem1sprite.Width() / 2
xRight = 400 - enem1sprite.Width() / 2
exspeed = 20
End
' Called every update
Method update()
ex += exspeed
If ex < xLeft
ex += ( xleft - ex ) ' improves accuracy of 'bounce'
exspeed = -exspeed
Elseif ex > xRight
ex -= ( ex - xRight )
exspeed = -exspeed
End
End
' Called every render
Method render()
DrawImage enemy1Sprite, ex, ey
End
End
Reply To Topic (minimum 10 characters)
Please log in to reply