Submit Your Own Code

PSP Lua CodeBase : Chase Player

Description:
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
 

Please welcome daryy, our newest member.

Who's Online: 3 Guests, 0 Users

Total Members: 556
Total Posts: 13060
Total Topics: 1480
Total Categories: 8
Total Boards: 35

Recent Posts:

Re: Lua 5.1.4 help. by daryy
Re: PGELua Help - need term for a special rotation fix (mathematical issue) by yaustar
Re: Lua Loader? by Buddy4point0
Re: Lua Loader? by osgeld
Re: Lua Loader? by acer5050
Re: Lua Loader? by bumuckl
Re: Lua Loader? by acer5050
Re: Lua Loader? by bumuckl


Copyright © 2006-2007 www.EvilMana.com All rights reserved.
EvilMana Logo by emcp and Charlie