-- EVILMANA.COM PSP LUA CODEBASE
-- www.evilmana.com/tutorials/codebase
-- Random Movement
-- SUBMITTED BY: Charlie
math.randomseed(os.time())
-- direction /// 1 = left /// 2 = right /// 3 = up /// 4 = down
character = {x=200, y=100, speed = -2, direction = 1,
image = Image.createEmpty(32,32)}
character.image:clear(Color.new(255,0,0))
counter = Timer.new()
counter:start()
counterMax = math.random(1000,4000)
while true do
screen:clear()
timeElapsed = counter:time()
screen:print(10,10,"Elapsed Time: "..timeElapsed,
Color.new(255,255,255))
screen:print(20,20,"Direction: "..character.direction,
Color.new(255,255,255))
if timeElapsed >= counterMax then
counterMax = math.random(1000,4000)
counter:reset(0)
counter:start()
character.direction = math.random(1,4)
if character.direction == 1 or
character.direction == 3 then
character.speed = -2
else
character.speed = 2
end
end
screen:blit(character.x,character.y,character.image)
if (character.direction == 1) or (character.direction == 2) then
character.x = character.x + character.speed
end
if (character.direction == 3) or (character.direction == 4) then
character.y = character.y + character.speed
end
if (character.x + 32) > 480 then
character.speed = -2
character.direction = 1
end
if (character.y + 32) > 272 then
character.speed = -2
character.direction = 3
end
if character.x < 0 then
character.speed = 2
character.direction = 2
end
if character.y < 0 then
character.speed = 2
character.direction = 4
end
screen.waitVblankStart()
screen.flip()
end