Posted 31 December 2012 - 05:24 AM
Hey guys, I've been really bored lately so I'm just messing around making random things.
At the moment I'm making a spaceship game or whatever just for fun (yes I know, there are already some but I need to extend my lua knowledge)..
Ok, so now my problem when I try remove a table within a table (for the shots that you shoot) it does remove it (i think but then errors out).
The way I thought (the only way I knew of how to do it) of how to update the shots separately and properly was using tables,
by adding the X and Y position of the shot in a table.
If I haven't made myself clear, just copy the code and test it and look at the number at the bottom right, that is the count of how many shots have been fired.
Now eventually that number will go into the thousands and I need a way to remove the shot once it's off the screen.
I have only tried this but it errors out on this line: "term.setCursorPos(t_shots.curX, t_shots.curY)"
Also, now and then I've been getting a Java error. It keeps disappearing before I can take a screenshot of it and I have no clue what is causing it.
At the moment I'm making a spaceship game or whatever just for fun (yes I know, there are already some but I need to extend my lua knowledge)..
Ok, so now my problem when I try remove a table within a table (for the shots that you shoot) it does remove it (i think but then errors out).
The way I thought (the only way I knew of how to do it) of how to update the shots separately and properly was using tables,
by adding the X and Y position of the shot in a table.
If I haven't made myself clear, just copy the code and test it and look at the number at the bottom right, that is the count of how many shots have been fired.
Now eventually that number will go into the thousands and I need a way to remove the shot once it's off the screen.
Spoiler
running = true
screenX, screenY = term.getSize()
posX, posY = math.floor(screenX/2), screenY
curX, curY = posX, posY
term.clear()
shot = false
t_shots = {}
function drawSpaceShip()
term.setCursorPos(posX, posY-4)
term.clearLine()
write(" _ ")
term.setCursorPos(posX, posY-3)
term.clearLine()
write(" / \\")
term.setCursorPos(posX, posY-2)
term.clearLine()
write("_| |_")
term.setCursorPos(posX, posY-1)
term.clearLine()
write("|____|")
end
function shoot()
term.setCursorPos(posX+2, posY - 5)
write("o")
shot = true
table.insert(t_shots, {curX = posX + 2, curY = posY - 5})
end
function update()
if #t_shots > 0 then
for i = 1, #t_shots do
if t_shots[i].curX and t_shots[i].curY then
term.setCursorPos(t_shots[i].curX, t_shots[i].curY)
write(" ")
t_shots[i].curY = t_shots[i].curY - 1
term.setCursorPos(t_shots[i].curX, t_shots[i].curY)
write("o")
if t_shots[i].curY == 0 then
t_shots[i].curY = nil
t_shots[i].curX = nil
end
end
end
else
shot = false
end
end
timer1 = os.startTimer(0.1)
while running do
drawSpaceShip()
term.setCursorPos(screenX - 15, screenY) term.clearLine() write(tostring(shot) .. " - " .. #t_shots)
e = {os.pullEvent()}
if e[1] == "key" then
if e[2] == keys.right and posX + 5 < screenX then
posX = posX + 1
elseif e[2] == keys.left and posX > 1 then
posX = posX - 1
--[[elseif e[2] == keys.up and posY > 5 then
posY = posY - 1
elseif e[2] == keys.down and posY - 1 < screenY then
posY = posY + 1]]
elseif e[2] == keys.space then
shoot()
end
elseif e[1] == "char" then
if e[2] == "x" then
running = false
end
elseif e[1] == "timer" and e[2] == timer1 then
update()
timer1 = os.startTimer(0.1)
end
end
I have only tried this but it errors out on this line: "term.setCursorPos(t_shots.curX, t_shots.curY)"
function update()
if #t_shots > 0 then
for i = 1, #t_shots do
if t_shots[i].curX and t_shots[i].curY then
term.setCursorPos(t_shots[i].curX, t_shots[i].curY)
write(" ")
t_shots[i].curY = t_shots[i].curY - 1
term.setCursorPos(t_shots[i].curX, t_shots[i].curY)
write("o")
if t_shots[i].curY == 0 then
table.remove(t_shots, i) -- I think it does remove the table, but then setting the cursor position errors out
end
end
end
else
shot = false
end
end
Also, now and then I've been getting a Java error. It keeps disappearing before I can take a screenshot of it and I have no clue what is causing it.