[/size]
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)
Particle not moving up? (game)
Started by Joe3000, May 27 2013 12:58 PM
2 replies to this topic
#1
Posted 27 May 2013 - 12: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.[size=4]
#2
Posted 27 May 2013 - 02:14 PM
The 2nd problem is caused by os.pullEvent(). os.pullEvent() waits until the user does something. You need to use the parallels api.
#3
Posted 27 May 2013 - 03:00 PM
Why use the parallel API when it's unnecessary? Just have a game timer.
Your second problem is caused by these bits of code:
You need to reverse the lesser than/greater than signs like this:
local function main()
local game_timer = os.startTimer(0.05)
while true do
local e = {os.pullEvent()}
if e[1] == "key" then
--Handle key stuff
elseif e[1] == "timer" and e[2] == game_timer then
--Handle drawing here
game_timer = os.startTimer(0.05)
end
end
end
Your second problem is caused by these bits of code:
elseif key == keys.up and self.ypos < 1 then --and this one elseif key == keys.down and self.ypos > h then
You need to reverse the lesser than/greater than signs like this:
elseif key == keys.up and self.ypos > 1 then elseif key == keys.down and self.ypos < h then
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











