123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- -- vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 autoindent:
- -- kate: space-indent on; indent-width 4; mixedindent off;
- players = {}
- p1 = {}
- p2 = {}
- function players.load()
- -- Keys for player control
- p1.keyleft = "h"
- p1.keyright = "j"
-
- p2.keyleft = "left"
- p2.keyright = "right"
-
- -- Player's colors
- p1.color = {255, 20, 20, 255}
- p2.color = {20, 255, 20, 255}
-
- -- Player's states
- p1.state = "alive"
- p2.state = "alive"
-
- -- Player's starting directions
- p1.direction = "right"
- p2.direction = "left"
-
- -- Player's starting points
- p1.x, p1.y = maxWidth / 4, maxHeight / 2
- p2.x, p2.y = maxWidth - maxWidth / 4, maxHeight / 2
- end
- function players.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 players.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 players.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 players.statePlayer(x, y, oldState)
- -- Handle collision Array.
- state = oldState
- if (y >= maxHeight) or (y <= 0) or (x >= maxWidth) or (x <= 0) then
- state = "crashed"
- end
- if not (state == "crashed") then
- if collisionArray[x][y] > 0 then
- state = "crashed"
- else
- collisionArray[x][y] = 1
- end
- end
-
- return state
- end
- -- Those functions for the Löve 2D game loop
- function update_players(dt)
- if (p1.state == "alive" and p2.state == "alive") then
- p1.x, p1.y = players.movePlayer(p1.x, p1.y, p1.direction)
- p1.state = players.statePlayer(p1.x, p1.y, p1.state)
-
- p2.x, p2.y = players.movePlayer(p2.x, p2.y, p2.direction)
- p2.state = players.statePlayer(p2.x, p2.y, p2.state)
- end
- end
- -- Those functions for the Löve 2D drawing loop
- function draw_players()
- -- Players play in canvas ...
- love.graphics.setCanvas(canvas)
-
- -- Player 1
- love.graphics.setColor(p1.color)
- love.graphics.point(p1.x, p1.y)
-
- -- Player 2
- love.graphics.setColor(p2.color)
- love.graphics.point(p2.x, p2.y)
- end
|