This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Joe3000's profile picture

Particle not moving up? (game)

Started by Joe3000, 27 May 2013 - 11:58 AM
Joe3000 #1
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)
nutcase84 #2
Posted 27 May 2013 - 03: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.
Bubba #3
Posted 27 May 2013 - 04:00 PM
Why use the parallel API when it's unnecessary? Just have a game timer.

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