Posted 27 May 2013 - 01:58 PM
So I watched NitrogenFingers' video on making the game breakout and I figured that the best way to learn how he did it was to make a game myself. and so I have been trying to work on this game but their are two problems, the more dominate one being that the "player" isn't moving up or down. The second problem (although not as important) is that the game doesn't draw until it receives input and I don't know why this is.
l,h = term.getSize()
running = true
player = {
xpos = math.floor(l/2);
ypos = h -2;
update = function(self, key)
if key == keys.left and self.xpos > 1 then
self.xpos = self.xpos - 1
elseif key == keys.right and self.xpos < l then
self.xpos = self.xpos + 1
elseif key == keys.up and self.ypos < 1 then
self.ypos = self.ypos - 1
elseif key == keys.down and self.ypos > h then
self.ypos = self.ypos + 1
end
end;
draw = function(self)
term.setCursorPos(self.xpos, self.ypos)
term.setBackgroundColour(colours.white)
term.write(" ")
end;
}
function gameDraw()
term.setBackgroundColour(colours.black)
term.clear()
player:draw()
end
function gameUpdate()
local id, p1 = os.pullEvent()
if id == "key" then
if p1 == keys.q then running = false end
player:update(p1)
end
end
function gameLoop()
while running do
gameUpdate()
gameDraw()
end
end
gameLoop()
term.setBackgroundColour(colours.black)
shell.run("clear")
sleep(0.01)
l,h = term.getSize()
running = true
player = {
xpos = math.floor(l/2);
ypos = h -2;
update = function(self, key)
if key == keys.left and self.xpos > 1 then
self.xpos = self.xpos - 1
elseif key == keys.right and self.xpos < l then
self.xpos = self.xpos + 1
elseif key == keys.up and self.ypos < 1 then
self.ypos = self.ypos - 1
elseif key == keys.down and self.ypos > h then
self.ypos = self.ypos + 1
end
end;
draw = function(self)
term.setCursorPos(self.xpos, self.ypos)
term.setBackgroundColour(colours.white)
term.write(" ")
end;
}
function gameDraw()
term.setBackgroundColour(colours.black)
term.clear()
player:draw()
end
function gameUpdate()
local id, p1 = os.pullEvent()
if id == "key" then
if p1 == keys.q then running = false end
player:update(p1)
end
end
function gameLoop()
while running do
gameUpdate()
gameDraw()
end
end
gameLoop()
term.setBackgroundColour(colours.black)
shell.run("clear")
sleep(0.01)