123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- -- 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
|