Posted 07 June 2013 - 12:29 AM
I think it's because once the lasers hit the end of the screen they nil out and then it doesn't know how to update, how do i fix :3
–Edit–
I did just realize that this is a slightly older version, so I don't think the error is showning… let me fix it real quick….
l, h = term.getSize()
refreshRate = 0.15
gtID = os.startTimer(refreshRate)
starTime = os.startTimer(0.01)
enemyTime = os.startTimer(.5)
running = true
--Background
function background()
stars = {
xpos = math.random(l);
ypos = math.random(h);
xvel = 1;
draw = function(self)
term.setCursorPos(self.xpos, self.ypos)
term.setBackgroundColour(colours.black)
term.setTextColour(colours.white)
term.write("*")
end;
update = function(self)
self.xpos = self.xpos - self.xvel
if self.xpos <= 0 then
self.xpos = l
end
end;
}
return stars
end
--player
player = {
xpos = 5;
ypos = math.floor(h/2);
update = function(self, key)
if key == keys.up then
self.ypos = self.ypos - 1
elseif key == keys.down then
self.ypos = self.ypos + 1
elseif key == keys.right then
table.insert(lAmount, laser())
end
if self.ypos >= h -1 then
self.ypos = h - 1
elseif self.ypos <= 1 then
self.ypos = 1
end
end;
draw = function(self)
term.setTextColour(colours.green)
term.setCursorPos(self.xpos, self.ypos)
term.setBackgroundColour(colours.black)
term.write("\\")
term.setCursorPos(self.xpos, self.ypos + 1)
term.setBackgroundColour(colours.black)
term.write("/")
end
}
--Enemies
function spawnEnemy()
enemy = {
xpos = l;
ypos = math.random(h);
draw = function(self)
term.setCursorPos(self.xpos, self.ypos)
term.setBackgroundColour(colours.black)
term.setTextColour(colours.red)
term.write("<")
end;
update = function(self)
self.xpos = self.xpos - 1
if self.xpos <= 0 then
self.xpos = l
end
end;
}
return enemy
end
--Lasers
function laser()
shoot = {
xpos = player.xpos + 1;
ypos = player.ypos;
update = function(self)
self.xpos = self.xpos + 1
if self.xpos >= l then
table.remove(lAmount)
end
end;
draw = function(self)
term.setCursorPos(self.xpos, self.ypos)
term.setBackgroundColour(colours.black)
term.setTextColour(colours.yellow)
term.write("-")
term.setCursorPos(self.xpos, self.ypos -1)
term.write("-")
end;
}
return shoot
end
sAmount = {}
for i=1,100 do
table.insert(sAmount, background())
end
eAmount = {}
for i=1,5 do
table.insert(eAmount, spawnEnemy())
end
lAmount = {}
for i=1,1 do
table.insert(lAmount, laser())
end
function drawGame()
term.clear()
term.setBackgroundColour(colours.black)
for i,v in pairs(sAmount) do
v:draw()
end
player:draw()
for i,v in pairs(eAmount) do
v:draw()
end
for i,v in pairs(lAmount) do
v:draw()
end
end
function updateGame()
id, p1 = os.pullEvent()
if id == "key" and p1 == keys.q then running = false end
if id == "timer" then
if p1 == starTime then
for i,v in pairs(sAmount) do
v:update()
end
for i,v in pairs(lAmount) do
v:update()
end
starTime = os.startTimer(0.01)
elseif p1 == enemyTime then
for i,v in pairs(eAmount) do
v:update()
end
enemyTime = os.startTimer(.5)
end
end
if id == "key" then
player:update(p1)
end
end
function gameLoop()
while running do
drawGame()
updateGame()
end
end
gameLoop()
shell.run("clear")
–Edit–
I did just realize that this is a slightly older version, so I don't think the error is showning… let me fix it real quick….