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 anthonyjee, our newest member.

Who's Online:

Total Members: 522
Total Posts: 13085
Total Topics: 1515
Total Categories: 7
Total Boards: 42

Recent Posts:

Re: Hello every body... by DeniseVera
Need help please by Robbynator
Re: Hello every body... by horvathann
Re: background criminal record check by backgroundchecker
background criminal record check by backgroundchecker
Intro to Perl Part 2: What are Scalars? & Examples by Chi Kitory
Intro to Perl Part 1 About Perl and Hello World by Chi Kitory
Re: Trying To Get Rid of the Spammers by Charlie


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