|
Submit
Your Own Code
PSP Lua CodeBase : Chase PlayerDescription:
Simple beginner AI. Enemy will chase a moveable player.
-- EVILMANA.COM PSP LUA CODEBASE
-- www.evilmana.com/tutorials/codebase
-- Chase Player
-- SUBMITTED BY: Charlie
blue = Color.new(0,0,255)
green = Color.new(0,255,0)
math.randomseed(os.time()) --/// prevent same random numbers from occuring each run of the game///
Player = { x = 300, y = 240, img = Image.createEmpty(32,32) } --/// players position and image info///
Player.img:clear(blue) -- clear players image to color blue
Enemy = { x = 50, y = 50, img = Image.createEmpty(32,32) } --///enemy's info///
Enemy.img:clear(green) --/// clear enemy image to green///
function movePlayer() --/// function to move our player when directionals are pressed///
pad = Controls.read()
if pad:left() then
Player.x = Player.x - 3
end
if pad:right() then
Player.x = Player.x + 3
end
if pad:up() then
Player.y = Player.y - 3
end
if pad:down() then
Player.y = Player.y + 3
end
end
function chasePlayer() --/// function to make the enemy chase the player///
stallchase = math.random(2) --/// stallchase variable set to random number. this variable prevents the enemy from being too perfect while chasing you.///
if stallchase == 1 then --/// if stallchase resulted to 1 then move x value towards players x value///
if Enemy.x > Player.x then
Enemy.x = Enemy.x - 2
elseif Enemy.x < Player.x then
Enemy.x = Enemy.x + 2
end
end
stallchase = math.random(2)
if stallchase == 1 then --/// if stallchase resulted to 1 then move y value towards players y value///
if Enemy.y > Player.y then
Enemy.y = Enemy.y - 2
elseif Enemy.y < Player.y then
Enemy.y = Enemy.y + 2
end
end
end --///end the function///
--///loop///
while true do
screen:clear()
movePlayer() --///call movePlayer function///
chasePlayer() --///call chasePlayer function////
--/// paste player and enemy to screen///
screen:blit(Player.x,Player.y,Player.img)
screen:blit(Enemy.x,Enemy.y,Enemy.img)
screen.waitVblankStart()
screen.flip()
end
Back to CodeBase
|