lua code help

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
newcoder
Posts: 10
Joined: Fri Dec 22, 2006 6:28 am

lua code help

Post by newcoder »

im new to lua but ive got a lot of the basics done. im wondering what is making my code just stay at a black screen and then reset i think it is the timer and if functions i have used but am not sure if someone could help that would be great. heres my code:

Code: Select all

red = Color.new(255,0,0)
background = Image.load("b.png") 
player = Image.load("a.png")
jewel = Image.load("c.png")
screenwidth = 480 - player:width()
screenheight = 272 - player:width()
Player = {}
Player[1] = {x=10,y=10}
Background = {}
Background[1] = {x=0,y=0}
Jewel = {}
Jewel[1] = {x=50,y=50}
Jewel[2] = {x=10,y=10}
Jewel[3] = {x=70,y=35}
Jewel[4] = {x=75,y=120}
Jewel[5] = {x=100,y=25}
counter = Timer.new()
counter:start()
while true do
pad = Controls.read()
screen:clear()
end
currentTime = counter:time()
screen:blit(Background[1].x, Background[1].y, background)
screen:blit(Player[1].x,Player[1].y,player)
if pad:left() and Player[1].x > 0 then
Player[1].x = Player[1].x - 2
end

if pad&#58;right&#40;&#41; and Player&#91;1&#93;.x < screenwidth then
Player&#91;1&#93;.x = Player&#91;1&#93;.x + 2
end

if pad&#58;up&#40;&#41; and Player&#91;1&#93;.y > 0 then
Player&#91;1&#93;.y = Player&#91;1&#93;.y - 2
end

if pad&#58;down&#40;&#41; and Player&#91;1&#93;.y < screenheight then
Player&#91;1&#93;.y = Player&#91;1&#93;.y + 2
end

if currentTime > 100 then
screen&#58;blit&#40;Jewel&#91;1&#93;.x,Jewel&#91;1&#93;.y,jewel&#41;
if currentTime > 250 then 
screen&#58;blit&#40;Jewel&#91;2&#93;.x,Jewel&#91;2&#93;.y,jewel&#41;
end

if currentTime > 400 then 
screen&#58;blit&#40;Jewel&#91;3&#93;.x,Jewel&#91;3&#93;.y,jewel&#41;
end

if currentTime > 550 then 
screen&#58;blit&#40;Jewel&#91;4&#93;.x,Jewel&#91;4&#93;.y,jewel&#41;
end

if currentTime > 700 then 
screen&#58;blit&#40;Jewel&#91;5&#93;.x,Jewel&#91;5&#93;.y,jewel&#41;
end

if currentTime > 1500 then
counter&#58;reset&#40;0&#41;
counter&#58;start&#40;&#41;
end
screen.flip&#40;&#41;
screen.waitVblankStart&#40;&#41;
end
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

It gets stuck here:

Code: Select all

while true do 
pad = Controls.read&#40;&#41; 
screen&#58;clear&#40;&#41; 
end 
You put an end there where it shouldn't be. Now you close the while true do-loop and it just stays in it untill it crashes. You should remove it.
newcoder
Posts: 10
Joined: Fri Dec 22, 2006 6:28 am

Post by newcoder »

i tried tha against my better judgement and it gave me an error saying it has to have that "and". so any other suggestions?
be2003
Posts: 144
Joined: Thu Apr 20, 2006 2:46 pm

Post by be2003 »

there is no such thing as "and"...

Code: Select all

red = Color.new&#40;255,0,0&#41; 
background = Image.load&#40;"b.png"&#41; 
player = Image.load&#40;"a.png"&#41; 
jewel = Image.load&#40;"c.png"&#41; 
screenwidth = 480 - player&#58;width&#40;&#41; 
screenheight = 272 - player&#58;width&#40;&#41; 
Player = &#123;&#125; 
Player&#91;1&#93; = &#123;x=10,y=10&#125; 
Background = &#123;&#125; 
Background&#91;1&#93; = &#123;x=0,y=0&#125; 
Jewel = &#123;&#125; 
Jewel&#91;1&#93; = &#123;x=50,y=50&#125; 
Jewel&#91;2&#93; = &#123;x=10,y=10&#125; 
Jewel&#91;3&#93; = &#123;x=70,y=35&#125; 
Jewel&#91;4&#93; = &#123;x=75,y=120&#125; 
Jewel&#91;5&#93; = &#123;x=100,y=25&#125; 
counter = Timer.new&#40;&#41; 
counter&#58;start&#40;&#41;

while true do 
	pad = Controls.read&#40;&#41; 
	screen&#58;clear&#40;&#41; 
	currentTime = counter&#58;time&#40;&#41; 
	screen&#58;blit&#40;Background&#91;1&#93;.x, Background&#91;1&#93;.y, background&#41; 
	screen&#58;blit&#40;Player&#91;1&#93;.x,Player&#91;1&#93;.y,player&#41;

	if pad&#58;left&#40;&#41; then if Player&#91;1&#93;.x > 0 then 
		Player&#91;1&#93;.x = Player&#91;1&#93;.x - 2 
	end end
	if pad&#58;right&#40;&#41; then if Player&#91;1&#93;.x < screenwidth then 
		Player&#91;1&#93;.x = Player&#91;1&#93;.x + 2 
	end end
	if pad&#58;up&#40;&#41; then if Player&#91;1&#93;.y > 0 then 
		Player&#91;1&#93;.y = Player&#91;1&#93;.y - 2 
	end end
	if pad&#58;down&#40;&#41; then if Player&#91;1&#93;.y < screenheight then 
		Player&#91;1&#93;.y = Player&#91;1&#93;.y + 2 
	end end

	if currentTime > 100 then 
		screen&#58;blit&#40;Jewel&#91;1&#93;.x,Jewel&#91;1&#93;.y,jewel&#41;
	end
	if currentTime > 250 then 
		screen&#58;blit&#40;Jewel&#91;2&#93;.x,Jewel&#91;2&#93;.y,jewel&#41; 
	end 
	if currentTime > 400 then 
		screen&#58;blit&#40;Jewel&#91;3&#93;.x,Jewel&#91;3&#93;.y,jewel&#41; 
	end 
	if currentTime > 550 then 
		screen&#58;blit&#40;Jewel&#91;4&#93;.x,Jewel&#91;4&#93;.y,jewel&#41; 
	end 
	if currentTime > 700 then 
		screen&#58;blit&#40;Jewel&#91;5&#93;.x,Jewel&#91;5&#93;.y,jewel&#41; 
	end 

	if currentTime > 1500 then 
		counter&#58;reset&#40;0&#41; 
		counter&#58;start&#40;&#41; 
	end

	screen.flip&#40;&#41; 
	screen.waitVblankStart&#40;&#41; 
end 
- be2003
blog
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

Well yeah ofcourse there should be an "end", but not in the place where you put it. The loop should close after all the other stuff you want it to do, so at the end of the code, like be2003 posted.
Post Reply