Submit Your Own Code

PSP Lua CodeBase : Random Movement

Description:
This code will randomly move an object in 4 directions, without letting the object move outside of the PSP's screen.

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

Back to CodeBase
 

Please welcome jramada, our newest member.

Who's Online: 5 Guests, 0 Users

Total Members: 555
Total Posts: 13059
Total Topics: 1480
Total Categories: 8
Total Boards: 35

Recent Posts:

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
Lua Loader? by acer5050


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