Browse Source

and here are the actual files :)

Paul Klumpp 9 years ago
parent
commit
20a3b2f5f4
3 changed files with 271 additions and 0 deletions
  1. 19 0
      License.txt
  2. 65 0
      init.lua
  3. 187 0
      main.lua

+ 19 - 0
License.txt

@@ -0,0 +1,19 @@
+Copyright (c) 2015 Netdome GbR (Paul Klumpp, Nikolaus Klumpp) 
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.

+ 65 - 0
init.lua

@@ -0,0 +1,65 @@
+-- vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 autoindent:
+
+
+local init = {}
+
+function init.load()
+
+    --love.window.setMode(800, 600, {resizable=true, vsync=false, minwidth=400, minheight=300})
+
+    p1_keyleft  = "h"
+    p1_keyright = "j"
+
+    p2_keyleft  = "left"
+    p2_keyright = "right"
+
+    game_keyquit = "escape"
+
+
+    -- We expect the window's size not to change
+    maxWidth = love.graphics.getWidth( )
+    maxHeight = love.graphics.getHeight( )
+
+    
+    -- Rendering
+    love.graphics.setBlendMode("additive")
+
+    -- Canvas
+    canvas = love.graphics.newCanvas(maxWidth, maxHeight)
+    love.graphics.setCanvas(canvas)
+    canvas:clear()
+
+    -- Some colors
+    text_color = {250, 250, 250, 255}
+    bg_color = {10, 10, 29, 255}
+    border_color = {15, 15, 255, 255}
+
+    -- 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 
+    x1, y1 = maxWidth / 4, maxHeight / 2  
+    x2, y2 = maxWidth - maxWidth / 4, maxHeight / 2 
+
+
+    -- setting up collision array 
+    collisionArray = {}
+    for dummyX = 1,maxWidth do 
+        collisionArray[dummyX] = {} 
+        for dummyY = 1,maxHeight do 
+            collisionArray[dummyX][dummyY] = 0
+        end 
+    end 
+
+end
+	  
+return init

+ 187 - 0
main.lua

@@ -0,0 +1,187 @@
+-- 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
+