-- vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 autoindent: -- Callback functions -- see http://www.love2d.org/wiki/Tutorial:Callback_Functions -- -- for key constants, see http://www.love2d.org/wiki/KeyConstant -- -- love.load() - at the game's start, used to initialise ressources -- love.update(dt) - the game loop function, where "dt" stands -- for the fraction of seconds since the function was called last -- love.draw() - graphics output loop -- love.mousepressed(x, y, button) - mouse down event callback function, -- where x, y the mouse's position, and button the number of the -- button pressed. -- love.mousereleased(x, y, button) - mouse button release event callback function, -- see mousepressed -- love.keypressed(key) - key press event callback function, where key is either -- a character, or any of the constants like "up", "down", "left", "right" -- love.keyreleased(key) - key release event callback function - see keypressed -- love.focus(f) - windows focus received/lost event callback function, where f -- is a boolean (true = focus gained, false = focus lost) -- love.quit - windows close event callback function function love.load() local init = require("init") init.load() end function turnLeft(direction) if direction == "left" then newDirection = "down" end if direction == "down" then newDirection = "right" end if direction == "right" then newDirection = "up" end if direction == "up" then newDirection = "left" end return newDirection end function turnRight(direction) if direction == "left" then newDirection = "up" end if direction == "down" then newDirection = "left" end if direction == "right" then newDirection = "down" end if direction == "up" then newDirection = "right" end return newDirection end function movePlayer(x, y, toDirection) if toDirection == "left" then x = x - 1 end if toDirection == "right" then x = x + 1 end if toDirection == "up" then y = y - 1 end if toDirection == "down" then y = y + 1 end return x, y end function love.keypressed(key) if key == p1_keyleft then p1_direction = turnLeft(p1_direction) end -- what happens if p1 presses right? if key == p1_keyright then p1_direction = turnRight(p1_direction) end -- what happens if p2 presses left? if key == p2_keyleft then p2_direction = turnLeft(p2_direction) end -- what happens if p2 presses right? if key == p2_keyright then p2_direction = turnRight(p2_direction) end end function statePlayer(currentstate) if collisionArray[x1][y1] == 0 then else p1_state = "crashed" end end function love.update(dt) if love.keyboard.isDown(game_keyquit) then love.event.quit() end -- Handle collision Array. -- TODO: Implement collision detection if (p1_state == "alive" and p2_state == "alive") then -- Player 1 x1, y1 = movePlayer(x1, y1, p1_direction) if (y1 >= maxHeight) or (y1 <= 0) or (x1 >= maxWidth) or (x1 <= 0) then p1_state = "crashed" end if not (p1_state == "crashed") then if collisionArray[x1][y1] > 0 then p1_state = "crashed" else collisionArray[x1][y1] = 1 end end -- Player 2 x2, y2 = movePlayer(x2, y2, p2_direction) if (y2 >= maxHeight) or (y2 <= 0) or (x2 >= maxWidth) or (x2 <= 0) then p2_state = "crashed" end if not (p2_state == "crashed") then if collisionArray[x2][y2] > 0 then p2_state = "crashed" else collisionArray[x2][y2] = 2 end end end end function love.draw() -- Players play in canvas ... love.graphics.setCanvas(canvas) -- Player 1 love.graphics.setColor(p1_color) love.graphics.point(x1, y1) -- Player 2 love.graphics.setColor(p2_color) love.graphics.point(x2, y2) -- Back to the screen love.graphics.setCanvas() love.graphics.setBackgroundColor(bg_color) -- Border love.graphics.setColor(border_color) love.graphics.rectangle("line", 0, 0, maxWidth - 1, maxHeight - 1) -- Draw the Player Canvas love.graphics.setColor(255, 255, 255, 255) love.graphics.draw(canvas) -- Some Text over the Canvas love.graphics.setColor(text_color) love.graphics.print ('p1:' .. p1_state .. ' p2:' .. p2_state , 25, 25) end