shadowprophet
July 18th, 2005, 20:38
Hi all im posting this thread. For everyone thats interested in dev/ing With lua.
I think its a nifty little program. And I also think its The perfect starters tool For aspireing developers to learn To code.
What I Submit this thread be. Is a learning center, Where coders come in and paste there script code. proven methods that have worked and ideas they wish to share. So others can learn from this. you can also post code with errors in it that you are haveing problems with and maybe somone can help with the answers. for the first entry. I will give An alter verson is shines Snake code.
That I aletred myself in a first attempt. This was just for kicks to see if the program worked. it does!. But here it is. Although I doubt somwhat it will be of any use to anyone. this script Is finished, And 100% Working :p
__________________________________________________ ______________
-- Shadowprophets Fantasy Beta
-- load images
broom2 = loadImage("broom2.png")
mm = loadImage("mm.png")
bodyT = loadImage("sprite8.png")
bodyB = loadImage("sprite1.png")
bodyL = loadImage("sprite3.png")
bodyR = loadImage("sprite6.png")
headR = loadImage("headR.png")
headT = loadImage("headT.png")
headL = loadImage("headL.png")
headB = loadImage("headB.png")
bodyLT = loadImage("bodyLT.png")
bodyLB = loadImage("bodyLB.png")
bodyRT = loadImage("bodyRT.png")
bodyRB = loadImage("bodyRB.png")
bodyLR = loadImage("bodyLR.png")
bodyTB = loadImage("bodyTB.png")
-- define globals
bodys = {}
heads = {}
bodies = {}
RIGHT = 0
TOP = 1
LEFT = 2
BOTTOM = 3
bodys[RIGHT] = bodyR
bodys[TOP] = bodyT
bodys[LEFT] = bodyL
bodys[BOTTOM] = bodyB
heads[RIGHT] = headR
heads[TOP] = headT
heads[LEFT] = headL
heads[BOTTOM] = headB
bodies[RIGHT] = bodyLR
bodies[TOP] = bodyTB
bodies[LEFT] = bodyLR
bodies[BOTTOM] = bodyTB
score = 0
high = 0
target = {}
targetImage = 0
dx = 0
dy = 0
cellHead = {}
cellBody = {}
function createRandomTarget()
target["x"] = math.random(21)
target["y"] = math.random(15)
if math.random(2) == 1 then
targetImage = broom2
else
targetImage = mm
end
end
function newGame()
flipDrawFrame()
fillBackground(0, 0, 480, 270)
dx = 0
dy = 0
x = 12
y = 8
cellBody["x"] = x
cellBody["y"] = y
cellBody["direction"] = RIGHT
x = x+1
cellHead["x"] = x
cellHead["y"] = y
cellHead["direction"] = RIGHT
cellBody.next = cellHead
createRandomTarget()
blitImage(16*cellBody["x"], 16*cellBody["y"], bodyR);
blitImage(16*cellHead["x"], 16*cellHead["y"], headR);
score = 0
flipShowFrameV()
end
function keyboardControl()
waitVblankStart();
pad = ctrlRead()
if isCtrlUp(pad) then
dx = 0
dy = -1
elseif isCtrlDown(pad) then
dx = 0
dy = 1
elseif isCtrlLeft(pad) then
dx = -1
dy = 0
elseif isCtrlRight(pad) then
dx = 1
dy = 0
elseif isCtrlCircle(pad) then
-- screenshot()
end
end
function move()
-- do nothing, if no movement vector defined
if dx == 0 and dy == 0 then
return
end
-- save last head direction and position
lastX = cellHead["x"]
lastY = cellHead["y"]
lastDirection = cellHead["direction"]
-- add new head
cellHead.next = {}
cellHead = cellHead.next
cellHead["x"] = lastX + dx;
cellHead["y"] = lastY + dy;
-- set direction for new head
direction = 0
if dx == 1 then
direction = RIGHT
elseif dy == -1 then
direction = TOP
elseif dx == -1 then
direction = LEFT
elseif dy == 1 then
direction = BOTTOM
end
cellHead["direction"] = direction
-- check which body to draw at the place of the old head
if lastDirection == RIGHT and direction == TOP then
blitImage(16*lastX, 16*lastY, bodyLT)
elseif lastDirection == TOP and direction == LEFT then
blitImage(16*lastX, 16*lastY, bodyLB)
elseif lastDirection == LEFT and direction == BOTTOM then
blitImage(16*lastX, 16*lastY, bodyRB)
elseif lastDirection == BOTTOM and direction == RIGHT then
blitImage(16*lastX, 16*lastY, bodyRT)
elseif lastDirection == RIGHT and direction == BOTTOM then
blitImage(16*lastX, 16*lastY, bodyLB)
elseif lastDirection == TOP and direction == RIGHT then
blitImage(16*lastX, 16*lastY, bodyRB)
elseif lastDirection == LEFT and direction == TOP then
blitImage(16*lastX, 16*lastY, bodyRT)
elseif lastDirection == BOTTOM and direction == LEFT then
blitImage(16*lastX, 16*lastY, bodyLT)
else
blitImage(16*lastX, 16*lastY, bodies[direction])
end
-- draw new head
blitImage(16*cellHead["x"], 16*cellHead["y"], heads[cellHead["direction"]])
-- check for target
if cellHead["x"] == target["x"] and cellHead["y"] == target["y"] then
createRandomTarget()
score = score + 1
else
-- remove body
fillBackground(16*cellBody["x"], 16*cellBody["y"], 16, 16)
cellTBody = cellBody.next
-- draw new Body image
if cellBody["direction"] ~= cellBody.next["direction"] then
cellBody["direction"] = cellBody.next["direction"]
end
blitImage(16*cellBody["x"], 16*cellBody["y"], bodys[cellBody["direction"]])
end
end
function isGameOver()
lastX = cellHead["x"]
lastY = cellHead["y"]
gameOver = false
if lastX >= 22 then
gameOver = true
end
if lastX < 1 then
gameOver = true
end
if lastY >= 16 then
gameOver = true
end
if lastY < 1 then
gameOver = true
end
cell = cellBody
while cell ~= cellHead do
if cell["x"] == lastX and cell["y"] == lastY then
gameOver = true
break
end
cell = cell.next
end
return gameOver
end
-- init
math.randomseed(os.time())
screenFrame(1, 1)
screenFrame(1, 0)
newGame()
-- game loop
while true do
for i=0,4 do
keyboardControl()
end
move()
blitImage(16 * target["x"], 16 * target["y"], targetImage)
if score > high then
high = score
end
if isGameOver() then
for i=0,50 do
waitVblankStart()
end
newGame()
end
printDecimal(410, 81, score, 0);
printDecimal(429, 129, high, 0);
end
__________________________________________________ ________________
If you have any script that works with lua please post it here. Im sure there are a lot of people who could learn from it:D
"Shadowprophet" Cause he thinks lua is just the complete bomb-digity
This thread could really help the aspireing developers comunity:D
So please by all means contrubute!! :cool:
I think its a nifty little program. And I also think its The perfect starters tool For aspireing developers to learn To code.
What I Submit this thread be. Is a learning center, Where coders come in and paste there script code. proven methods that have worked and ideas they wish to share. So others can learn from this. you can also post code with errors in it that you are haveing problems with and maybe somone can help with the answers. for the first entry. I will give An alter verson is shines Snake code.
That I aletred myself in a first attempt. This was just for kicks to see if the program worked. it does!. But here it is. Although I doubt somwhat it will be of any use to anyone. this script Is finished, And 100% Working :p
__________________________________________________ ______________
-- Shadowprophets Fantasy Beta
-- load images
broom2 = loadImage("broom2.png")
mm = loadImage("mm.png")
bodyT = loadImage("sprite8.png")
bodyB = loadImage("sprite1.png")
bodyL = loadImage("sprite3.png")
bodyR = loadImage("sprite6.png")
headR = loadImage("headR.png")
headT = loadImage("headT.png")
headL = loadImage("headL.png")
headB = loadImage("headB.png")
bodyLT = loadImage("bodyLT.png")
bodyLB = loadImage("bodyLB.png")
bodyRT = loadImage("bodyRT.png")
bodyRB = loadImage("bodyRB.png")
bodyLR = loadImage("bodyLR.png")
bodyTB = loadImage("bodyTB.png")
-- define globals
bodys = {}
heads = {}
bodies = {}
RIGHT = 0
TOP = 1
LEFT = 2
BOTTOM = 3
bodys[RIGHT] = bodyR
bodys[TOP] = bodyT
bodys[LEFT] = bodyL
bodys[BOTTOM] = bodyB
heads[RIGHT] = headR
heads[TOP] = headT
heads[LEFT] = headL
heads[BOTTOM] = headB
bodies[RIGHT] = bodyLR
bodies[TOP] = bodyTB
bodies[LEFT] = bodyLR
bodies[BOTTOM] = bodyTB
score = 0
high = 0
target = {}
targetImage = 0
dx = 0
dy = 0
cellHead = {}
cellBody = {}
function createRandomTarget()
target["x"] = math.random(21)
target["y"] = math.random(15)
if math.random(2) == 1 then
targetImage = broom2
else
targetImage = mm
end
end
function newGame()
flipDrawFrame()
fillBackground(0, 0, 480, 270)
dx = 0
dy = 0
x = 12
y = 8
cellBody["x"] = x
cellBody["y"] = y
cellBody["direction"] = RIGHT
x = x+1
cellHead["x"] = x
cellHead["y"] = y
cellHead["direction"] = RIGHT
cellBody.next = cellHead
createRandomTarget()
blitImage(16*cellBody["x"], 16*cellBody["y"], bodyR);
blitImage(16*cellHead["x"], 16*cellHead["y"], headR);
score = 0
flipShowFrameV()
end
function keyboardControl()
waitVblankStart();
pad = ctrlRead()
if isCtrlUp(pad) then
dx = 0
dy = -1
elseif isCtrlDown(pad) then
dx = 0
dy = 1
elseif isCtrlLeft(pad) then
dx = -1
dy = 0
elseif isCtrlRight(pad) then
dx = 1
dy = 0
elseif isCtrlCircle(pad) then
-- screenshot()
end
end
function move()
-- do nothing, if no movement vector defined
if dx == 0 and dy == 0 then
return
end
-- save last head direction and position
lastX = cellHead["x"]
lastY = cellHead["y"]
lastDirection = cellHead["direction"]
-- add new head
cellHead.next = {}
cellHead = cellHead.next
cellHead["x"] = lastX + dx;
cellHead["y"] = lastY + dy;
-- set direction for new head
direction = 0
if dx == 1 then
direction = RIGHT
elseif dy == -1 then
direction = TOP
elseif dx == -1 then
direction = LEFT
elseif dy == 1 then
direction = BOTTOM
end
cellHead["direction"] = direction
-- check which body to draw at the place of the old head
if lastDirection == RIGHT and direction == TOP then
blitImage(16*lastX, 16*lastY, bodyLT)
elseif lastDirection == TOP and direction == LEFT then
blitImage(16*lastX, 16*lastY, bodyLB)
elseif lastDirection == LEFT and direction == BOTTOM then
blitImage(16*lastX, 16*lastY, bodyRB)
elseif lastDirection == BOTTOM and direction == RIGHT then
blitImage(16*lastX, 16*lastY, bodyRT)
elseif lastDirection == RIGHT and direction == BOTTOM then
blitImage(16*lastX, 16*lastY, bodyLB)
elseif lastDirection == TOP and direction == RIGHT then
blitImage(16*lastX, 16*lastY, bodyRB)
elseif lastDirection == LEFT and direction == TOP then
blitImage(16*lastX, 16*lastY, bodyRT)
elseif lastDirection == BOTTOM and direction == LEFT then
blitImage(16*lastX, 16*lastY, bodyLT)
else
blitImage(16*lastX, 16*lastY, bodies[direction])
end
-- draw new head
blitImage(16*cellHead["x"], 16*cellHead["y"], heads[cellHead["direction"]])
-- check for target
if cellHead["x"] == target["x"] and cellHead["y"] == target["y"] then
createRandomTarget()
score = score + 1
else
-- remove body
fillBackground(16*cellBody["x"], 16*cellBody["y"], 16, 16)
cellTBody = cellBody.next
-- draw new Body image
if cellBody["direction"] ~= cellBody.next["direction"] then
cellBody["direction"] = cellBody.next["direction"]
end
blitImage(16*cellBody["x"], 16*cellBody["y"], bodys[cellBody["direction"]])
end
end
function isGameOver()
lastX = cellHead["x"]
lastY = cellHead["y"]
gameOver = false
if lastX >= 22 then
gameOver = true
end
if lastX < 1 then
gameOver = true
end
if lastY >= 16 then
gameOver = true
end
if lastY < 1 then
gameOver = true
end
cell = cellBody
while cell ~= cellHead do
if cell["x"] == lastX and cell["y"] == lastY then
gameOver = true
break
end
cell = cell.next
end
return gameOver
end
-- init
math.randomseed(os.time())
screenFrame(1, 1)
screenFrame(1, 0)
newGame()
-- game loop
while true do
for i=0,4 do
keyboardControl()
end
move()
blitImage(16 * target["x"], 16 * target["y"], targetImage)
if score > high then
high = score
end
if isGameOver() then
for i=0,50 do
waitVblankStart()
end
newGame()
end
printDecimal(410, 81, score, 0);
printDecimal(429, 129, high, 0);
end
__________________________________________________ ________________
If you have any script that works with lua please post it here. Im sure there are a lot of people who could learn from it:D
"Shadowprophet" Cause he thinks lua is just the complete bomb-digity
This thread could really help the aspireing developers comunity:D
So please by all means contrubute!! :cool: