Code:
--light cycle clone by luke ryan
System.setHigh()
math.randomseed(os.time())
gamestate=0
option_selected=1
onewin=Image.load("images/p1win.png")
twowin=Image.load("images/p2win.png")
splash=Image.load("images/splash.png")
menumn=Image.load("images/menu.png")
meselcol=Color.new(196, 0, 72)
menoselcol=Color.new(0, 189, 89)
white=Color.new(255,255,255)
gray=Color.new(128,128,128)
speed=1
checkcollision=false
player={x=225,y=240, direction=1, colour=Color.new(255,20,147),width=5,height=5,life=5}
playerblock=Image.createEmpty(3,3)
playerblock:clear(player.colour)
player.line=1
player[player.line]={x=225,y=240,width=5,height=5}
computer={x=250,y=240, direction=1, colour=Color.new(0,255,127),width=5,height=5,life=5}
computerblock=Image.createEmpty(3,3)
computerblock:clear(computer.colour)
computer.line=1
computer[computer.line]={x=250,y=240,width=5,height=5,direction=1}
comthinktime=1
----------------------------------------------------------------------------------------
function menu()
fader = Image.createEmpty(480,272)
alphaValue = 255
faderColor = Color.new(0,0,0,alphaValue)
fader:clear(faderColor)
while true do
screen:clear()
screen:blit(0,0,splash)
screen:blit(0,0,fader)
if alphaValue > 0 then
alphaValue = alphaValue - 5
else
break
end
faderColor = Color.new(0,0,0,alphaValue)
fader:clear(faderColor)
screen.waitVblankStart()
screen.flip()
end
screen.waitVblankStart(200)
while true do
screen:clear()
screen:blit(0,0,splash)
screen:blit(0,0,fader)
if alphaValue < 255 then
alphaValue = alphaValue + 5
else
gamestate=4
break
end
faderColor = Color.new(0,0,0,alphaValue)
fader:clear(faderColor)
screen.waitVblankStart()
screen.flip()
end
end
function mainmenu()
while true do
pad=Controls.read()
if option_selected==1 then
screen:blit(0,0,menumn)
screen:print(273, 115, "ONE PLAYER", meselcol)
screen:print(273, 154, "TWO PLAYER", menoselcol)
screen:print(267, 193, "VIEW CREDITS", menoselcol)
end
if option_selected==2 then
screen:blit(0,0,menumn)
screen:print(273, 115, "ONE PLAYER", menoselcol)
screen:print(273, 154, "TWO PLAYER", meselcol)
screen:print(267, 193, "VIEW CREDITS", menoselcol)
end
if option_selected==3 then
screen:blit(0,0,menumn)
screen:print(273, 115, "ONE PLAYER", menoselcol)
screen:print(273, 154, "TWO PLAYER", menoselcol)
screen:print(267, 193, "VIEW CREDITS", meselcol)
end
if pad:up() and oldpad:up() ~= pad:up() and option_selected>1 then
option_selected=option_selected-1
end
if pad:down() and oldpad:down() ~= pad:down() and option_selected<3 then
option_selected=option_selected+1
end
if pad:cross() then
oldpad=pad
if option_selected==1 then
gamestate=1
break
end
if option_selected==2 then
gamestate=2
break
end
if option_selected==3 then
gamestate=3
break
end
end
screen.waitVblankStart()
screen:flip()
oldpad=pad
end
end
--------------------------------------------------------------------------------------------------------------
function oneplayer()
while true do
pad=Controls.read()
screen:clear()
CustomTracks()
musiccontrol()
if checkcollision==false then
playermovement()
for n=1, player.line do
computerai(player[n])
end
if player.line> 2 then
for n=1,player.line-2 do
playercollision(player[n])
end
end
if computer.line> 2 then
for n=1,computer.line-2 do
computerai(computer[n])
computercollision(computer[n])
end
end
computeraimovement()
computeraiwalls()
if checkcollision==false and computer.y>272 or computer.y<0 or computer.x<0 or computer.x>480 then
computer.life=computer.life-1
checkcollision=true
end
if checkcollision==false and player.y>272 or player.y<0 or player.x<0 or player.x>480 then
player.life=player.life-1
checkcollision=true
end
for n=1,computer.line do
collision(player,computer[n])
end
for n=1,player.line do
collision(computer,player[n])
end
display()
if computer.line+player.line>5 then
speed=(computer.line+player.line)/2
end
screen.waitVblankStart()
screen:flip()
n=1
oldpad=pad
else
display()
screen:print(170,50,"Press START to Continue",Color.new(255,255,255))
screen:print(170,60," Press SELECT for menu",Color.new(255,255,255))
if pad:select() then
player.life=5
computer.life=5
resetvariables()
checkcollision=false
gamestate=4
break
end
if pad:start() then
if player.life==0 or computer.life==0 then
player.life=5
computer.life=5
end
resetvariables()
checkcollision=false
end
if player.life==0 then
screen:blit(0,0,twowin)
end
if computer.life==0 then
screen:blit(0,0,onewin)
end
screen.waitVblankStart()
screen:flip()
n=1
oldpad=pad
end
end
end
--------------------------------------------------------------------------------------------------------------------
function twoplayer()
while true do
pad=Controls.read()
screen:clear()
CustomTracks()
musiccontrol()
if checkcollision==false then
display()
playermovement()
computermovement()
if player.line> 2 then
for n=1,player.line-2 do
playercollision(player[n])
end
end
if computer.line> 2 then
for n=1,computer.line-2 do
computercollision(computer[n])
end
end
if checkcollision==false and computer.y>272 or computer.y<0 or computer.x<0 or computer.x>480 then
computer.life=computer.life-1
checkcollision=true
end
if checkcollision==false and player.y>272 or player.y<0 or player.x<0 or player.x>480 then
player.life=player.life-1
checkcollision=true
end
for n=1,computer.line do
collision(player,computer[n])
end
for n=1,player.line do
collision(computer,player[n])
end
if computer.line+player.line>5 then
speed=(computer.line+player.line)/2
end
screen.waitVblankStart()
screen:flip()
n=1
oldpad=pad
else
display()
screen:print(170,50,"Press START to Continue",Color.new(255,255,255))
screen:print(170,60," Press SELECT for menu",Color.new(255,255,255))
if pad:select() then
player.life=5
computer.life=5
resetvariables()
checkcollision=false
gamestate=4
break
end
if pad:start() then
if player.life==0 or computer.life==0 then
player.life=5
computer.life=5
end
resetvariables()
checkcollision=false
end
if player.life==0 then
screen:blit(0,0,twowin)
end
if computer.life==0 then
screen:blit(0,0,onewin)
end
screen.waitVblankStart()
screen:flip()
n=1
oldpad=pad
end
end
end
--------------------------------------------------------------------------------------------------------------
function playermovement()
if pad:up() and oldpad:up()~=pad:up() and player.direction~=3 and player.direction~=1 then
player.direction=1
player.line=player.line+1
player[player.line]={x=player.x,y=player.y,width=5,height=5}
end
if pad:down() and oldpad:down()~=pad:down() and player.direction~=1 and player.direction~=3 then
player.direction=3
player.line=player.line+1
player[player.line]={x=player.x,y=player.y,width=5,height=5}
end
if pad:right() and oldpad:right()~=pad:right() and player.direction~=4 and player.direction~=2 then
player.direction=2
player.line=player.line+1
player[player.line]={x=player.x,y=player.y,width=5,height=5}
end
if pad:left() and oldpad:left()~=pad:left() and player.direction~=2 and player.direction~=4 then
player.direction=4
player.line=player.line+1
player[player.line]={x=player.x,y=player.y,width=5,height=5}
end
if player.direction==1 then
player.y=player.y-speed
player[player.line].height=player[player.line].height+speed
player[player.line].y=player[player.line].y-speed
end
if player.direction==3 then
player.y=player.y+speed
player[player.line].height=player[player.line].height+speed
end
if player.direction==2 then
player.x=player.x+speed
player[player.line].width=player[player.line].width+speed
end
if player.direction==4 then
player.x=player.x-speed
player[player.line].width=player[player.line].width+speed
player[player.line].x=player[player.line].x-speed
end
end
---------------------------------------------------------------------------------------------------------------------------------
function computermovement()
if pad:triangle() and oldpad:triangle()~=pad:triangle() and computer.direction~=3 and computer.direction~=1 then
computer.direction=1
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5}
end
if pad:cross() and oldpad:cross()~=pad:cross() and computer.direction~=1 and computer.direction~=3 then
computer.direction=3
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5}
end
if pad:circle() and oldpad:circle()~=pad:circle() and computer.direction~=4 and computer.direction~=2 then
computer.direction=2
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5}
end
if pad:square() and oldpad:square()~=pad:square() and computer.direction~=2 and computer.direction~=4 then
computer.direction=4
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5}
end
if computer.direction==1 then
computer.y=computer.y-speed
computer[computer.line].height=computer[computer.line].height+speed
computer[computer.line].y=computer[computer.line].y-speed
end
if computer.direction==3 then
computer.y=computer.y+speed
computer[computer.line].height=computer[computer.line].height+speed
end
if computer.direction==2 then
computer.x=computer.x+speed
computer[computer.line].width=computer[computer.line].width+speed
end
if computer.direction==4 then
computer.x=computer.x-speed
computer[computer.line].width=computer[computer.line].width+speed
computer[computer.line].x=computer[computer.line].x-speed
end
end
-------------------------------------------------------------------------------------------------------------------------------------------
function playercollision(object)
if checkcollision==false and (player.x + player.width > object.x) and (player.x < object.x + object.width) and (player.y + player.height > object.y) and (player.y < object.y + object.height) then
player.life=player.life-1
checkcollision=true
end
end
-----------------------------------------------------------------------------------------------------------------------------------------------------
function computercollision(object)
if checkcollision==false and (computer.x + computer.width > object.x) and (computer.x < object.x + object.width) and (computer.y + computer.height > object.y) and (computer.y < object.y + object.height) then
computer.life=computer.life-1
checkcollision=true
end
end
----------------------------------------------------------------------------------------------------------------------------------------------------------
function collision(objectone,objecttwo)
if checkcollision==false and (objectone.x + objectone.width > objecttwo.x) and (objectone.x < objecttwo.x + objecttwo.width) and (objectone.y + objectone.height > objecttwo.y) and (objectone.y < objecttwo.y + objecttwo.height) then
objectone.life=objectone.life-1
checkcollision=true
end
end
-------------------------------------------------------------------------------------------------------------------------------------------------------------
function resetvariables()
for n=1, player.line do
player[player.line]=nil
end
for n=1, computer.line do
computer[computer.line]=nil
end
System.memclean()
speed=1
player={x=225,y=240, direction=1, colour=Color.new(255,20,147),width=5,height=5,life=player.life}
playerblock=Image.createEmpty(5,5)
playerblock:clear(player.colour)
player.line=1
player[player.line]={x=225,y=240,width=5,height=5}
computer={x=250,y=240, direction=1, colour=Color.new(0,255,127),width=5,height=5,life=computer.life}
computerblock=Image.createEmpty(5,5)
computerblock:clear(computer.colour)
computer.line=1
computer[computer.line]={x=250,y=240,width=5,height=5}
comthinktime=1
end
------------------------------------------------------------------------------------------------------------------------------------------
function display()
screen:drawLine(15, 0, 15, 272, gray)
screen:drawLine(49, 0, 49, 272, gray)
screen:drawLine(83, 0, 83, 272, gray)
screen:drawLine(117, 0, 117, 272, gray)
screen:drawLine(151, 0, 151, 272, gray)
screen:drawLine(185, 0, 185, 272, gray)
screen:drawLine(219, 0, 219, 272, gray)
screen:drawLine(253, 0, 253, 272, gray)
screen:drawLine(287, 0, 287, 272, gray)
screen:drawLine(321, 0, 321, 272, gray)
screen:drawLine(355, 0, 355, 272, gray)
screen:drawLine(389, 0, 389, 272, gray)
screen:drawLine(423, 0, 423, 272, gray)
screen:drawLine(457, 0, 457, 272, gray)
screen:drawLine(0, 34, 480, 34, gray)
screen:drawLine(0, 68, 480, 68, gray)
screen:drawLine(0, 102, 480, 102, gray)
screen:drawLine(0, 136, 480, 136, gray)
screen:drawLine(0, 170, 480, 170, gray)
screen:drawLine(0, 204, 480, 204, gray)
screen:drawLine(0, 238, 480, 238, gray)
screen:blit(player.x,player.y,playerblock)
screen:blit(computer.x,computer.y,computerblock)
for n=1, player.line do
image=Image.createEmpty(player[n].width,player[n].height)
image:clear(player.colour)
screen:blit(player[n].x,player[n].y,image)
end
for n=1, computer.line do
image=Image.createEmpty(computer[n].width,computer[n].height)
image:clear(computer.colour)
screen:blit(computer[n].x,computer[n].y,image)
end
screen:print(5,5,"player 1 life:"..player.life,player.colour)
screen:print(300,5,"player 2 life:"..computer.life,computer.colour)
end
--------------------------------------------------------------------------------------------------------------------------------------------------
function computerai(object)
comthinktime=comthinktime+1
if computer.line>2 then
if computer.direction==1 and comthinktime>speed and (computer.y-speed < object.y + object.height) and (computer.x < object.x + object.width) and (computer.x + computer.width > object.x) then
comthinktime=1
if computer[computer.line-1].direction==2 then
computer.direction=4
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
else
computer.direction=2
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
end
n=1
end
if computer.direction==3 and comthinktime>speed and (computer.y + computer.height+speed> object.y) and (computer.x < object.x + object.width) and (computer.x + computer.width > object.x) then
comthinktime=1
if computer[computer.line-1].direction==2 then
computer.direction=4
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
else
computer.direction=2
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
end
n=1
end
if computer.direction==2 and comthinktime>speed and (computer.x +computer.width +speed> object.x) and (computer.y + computer.height > object.y) and (computer.y < object.y + object.height) then
comthinktime=1
if computer[computer.line-1].direction==1 then
computer.direction=3
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
else
computer.direction=1
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
end
n=1
end
if computer.direction==4 and comthinktime>speed and (computer.x-speed < object.x + object.width) and (computer.y + computer.height > object.y) and (computer.y < object.y + object.height) then
comthinktime=1
if computer[computer.line-1].direction==1 then
computer.direction=3
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
else
computer.direction=1
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
end
n=1
end
end
if computer.direction==1 and computer.x<10 and computer.y<10 then
comthinktime=1
computer.direction=2
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
end
if computer.direction==4 and computer.x<10 and computer.y<10 then
comthinktime=1
computer.direction=3
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
end
if computer.direction==1 and computer.x>470 and computer.y<10 then
comthinktime=1
computer.direction=4
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
end
if computer.direction==2 and computer.x>470 and computer.y<10 then
comthinktime=1
computer.direction=3
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
end
if computer.direction==3 and computer.x<10 and computer.y>262 then
comthinktime=1
computer.direction=2
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
end
if computer.direction==4 and computer.x<8 and computer.y>262 then
comthinktime=1
computer.direction=1
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
end
if computer.direction==2 and computer.x>470 and computer.y>262 then
comthinktime=1
computer.direction=1
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
end
if computer.direction==3 and computer.x>470 and computer.y>262 then
comthinktime=1
computer.direction=4
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
end
end
-----------------------------------------------------------------------------------------------------------------------------------------------------------
function computeraimovement()
if computer.direction==1 then
computer.y=computer.y-speed
computer[computer.line].height=computer[computer.line].height+speed
computer[computer.line].y=computer[computer.line].y-speed
end
if computer.direction==3 then
computer.y=computer.y+speed
computer[computer.line].height=computer[computer.line].height+speed
end
if computer.direction==2 then
computer.x=computer.x+speed
computer[computer.line].width=computer[computer.line].width+speed
end
if computer.direction==4 then
computer.x=computer.x-speed
computer[computer.line].width=computer[computer.line].width+speed
computer[computer.line].x=computer[computer.line].x-speed
end
end
---------------------------------------------------------------------------------------------------------------------------------------------------------
function computeraiwalls()
if computer.direction==1 and computer.x<10 and computer.y<10 then
comthinktime=1
computer.direction=2
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
end
if computer.direction==4 and computer.x<10 and computer.y<10 then
comthinktime=1
computer.direction=3
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
end
if computer.direction==1 and computer.x>470 and computer.y<10 then
comthinktime=1
computer.direction=4
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
end
if computer.direction==2 and computer.x>470 and computer.y<10 then
comthinktime=1
computer.direction=3
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
end
if computer.direction==3 and computer.x<10 and computer.y>262 then
comthinktime=1
computer.direction=2
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
end
if computer.direction==4 and computer.x<10 and computer.y>262 then
comthinktime=1
computer.direction=1
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
end
if computer.direction==2 and computer.x>470 and computer.y>262 then
comthinktime=1
computer.direction=1
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
end
if computer.direction==3 and computer.x>470 and computer.y>262 then
comthinktime=1
computer.direction=4
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
end
if (computer.direction==1 and computer.y<10) or (computer.direction==3 and computer.y>262) then
rannumber=math.random(1,2)
comthinktime=1
if rannumber==1 then
computer.direction=2
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
else
computer.direction=4
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
end
end
if (computer.direction==4 and computer.x<10) or (computer.direction==2 and computer.x> 470) then
rannumber=math.random(1,2)
comthinktime=1
if rannumber==1 then
computer.direction=1
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
else
computer.direction=3
computer.line=computer.line+1
computer[computer.line]={x=computer.x,y=computer.y,width=5,height=5,direction=computer.direction}
end
end
end
-----------------------------------------------------------------------------------------------------------------
function credits()
screen:clear()
tmv = 1
credy = 272
while true do
pad=Controls.read()
screen:clear()
tmv = tmv + 1
pad = Controls.read()
screen:blit(0,0,menumn)
if credy > 80 then
credy = credy - 0.5
end
screen:print(180, credy, " Thank you for playing,", menoselcol)
screen:print(187, credy + 10, " Light Cycles Clone!", meselcol)
screen:print(194, credy + 30, "Thanks to Homemister and PickDat for:", menoselcol)
screen:print(186, credy + 40, " LUA Player HM6", meselcol)
screen:print(200, credy + 60, " Developed by:", menoselcol)
screen:print(215, credy + 70, " Luke Ryan", meselcol)
screen:print(185, credy + 90, " Additional programing by:", menoselcol)
screen:print(165, credy + 100, " Code Red", meselcol)
screen:print(165, credy + 120, " Original release:", menoselcol)
screen:print(173, credy + 130, " www.psp-hacks.com", meselcol)
screen:print(165, credy + 150, " Created for:", menoselcol)
screen:print(196, credy + 160, " PSP-HACKS Homebrew Compo 2008", meselcol)
screen:print(173, credy + 180, "Press START to return", white)
if pad:start() then
gamestate=4
break
end
screen.waitVblankStart()
screen:flip()
n=1
oldpad=pad
end
end
----------------------------------------------------------------------------------------
-------custom soundtrack
function CustomTracks()
if numberofsongs==nil then
track=3
loadsong=1
song = System.listDirectory("ms0:/PSP/MUSIC")
numberofsongs=table.getn(song)
end
if numberofsongs>2 then
if loadsong==1 then
System.currentDirectory("ms0:/")
Mp3me.load("PSP/MUSIC/"..song[track].name)
Mp3me.play()
loadsong=0
end
if Mp3me.eos()==true then
if track<numberofsongs then
track=track+1
else
track=3
end
Mp3me.stop()
loadsong=1
end
else
if numberofsongs<3 or Mp3me.eos()==true then
Mp3me.load("song.mp3")
Mp3me.play()
end
end
end
-----------------------------------------------------------------------------------------------------------
--music controls
function musiccontrol()
if pad:r() and oldpad:r()~=pad:r() then
Mp3me.stop()
if track<numberofsongs then
track=track+1
else
track=3
end
loadsong=1
end
if pad:l() and oldpad:l()~=pad:l() then
Mp3me.stop()
if track>3 then
track=track-1
else
track=3
end
loadsong=1
end
end
---------------------------------------------------------------------------------
--main loop
while true do
screen:clear()
CustomTracks()
if gamestate==0 then
menu()
end
if gamestate==1 then
oneplayer()
end
if gamestate==2 then
twoplayer()
end
if gamestate==3 then
credits()
end
if gamestate==4 then
mainmenu()
end
screen.waitVblankStart()
screen:flip()
end