Browse Source

made it compatible with Löve 0.9.x

Paul Klumpp 9 years ago
parent
commit
9109ea37ed
4 changed files with 167 additions and 152 deletions
  1. BIN
      SourceCodePro-Regular.ttf
  2. 9 33
      init.lua
  3. 27 119
      main.lua
  4. 131 0
      players.lua

BIN
SourceCodePro-Regular.ttf


+ 9 - 33
init.lua

@@ -1,54 +1,31 @@
--- vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 autoindent:
+-- vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 autoindent:
+-- kate: space-indent on; indent-width 4; mixedindent off;
 
-
-local init = {}
+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"
-
+    -- a game key
     game_keyquit = "escape"
 
-
-    -- We expect the window's size not to change
+    -- Window (we expect the window's size not to change)
+    love.window.setMode(1280, 1024, {resizable=false, vsync=true, minwidth=400, minheight=300})
     maxWidth = love.graphics.getWidth( )
     maxHeight = love.graphics.getHeight( )
-
     
     -- Rendering
-    love.graphics.setBlendMode("additive")
+    love.graphics.setBlendMode("screen")
 
     -- Canvas
     canvas = love.graphics.newCanvas(maxWidth, maxHeight)
     love.graphics.setCanvas(canvas)
-    canvas:clear()
+    --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 
@@ -61,5 +38,4 @@ function init.load()
     end 
 
 end
-	  
-return init
+	 

+ 27 - 119
main.lua

@@ -1,4 +1,5 @@
--- vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 autoindent:
+-- vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 autoindent:
+-- kate: space-indent on; indent-width 4; mixedindent off;
 
 -- Callback functions
 -- see http://www.love2d.org/wiki/Tutorial:Callback_Functions
@@ -22,150 +23,60 @@
 -- love.quit - windows close event callback function
 
 
+require("init")
+require("players")
+
 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
+    players.load()
 end
 
 
 function love.keypressed(key)
-    if key == p1_keyleft then
-        p1_direction = turnLeft(p1_direction)
+    if key == p1.keyleft then
+        p1.direction = players.turnLeft(p1.direction)
     end
 
     -- what happens if p1 presses right?
-    if key == p1_keyright then
-        p1_direction = turnRight(p1_direction)
+    if key == p1.keyright then
+        p1.direction = players.turnRight(p1.direction)
     end
 
     -- what happens if p2 presses left?
-    if key == p2_keyleft then
-        p2_direction = turnLeft(p2_direction)
+    if key == p2.keyleft then
+        p2.direction = players.turnLeft(p2.direction)
     end
 
     -- what happens if p2 presses right?
-    if key == p2_keyright then
-        p2_direction = turnRight(p2_direction)
+    if key == p2.keyright then
+        p2.direction = players.turnRight(p2.direction)
     end
 end
 
-function statePlayer(currentstate)
-
-
-    if collisionArray[x1][y1] == 0 then
-    else
-        p1_state = "crashed"
-    end
-
-
-end
 
 function love.update(dt)
+    update_players(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)
+function love.draw()
 
-    -- Player 2
-    love.graphics.setColor(p2_color)
-    love.graphics.point(x2, y2)
+    -- Call our player drawing function
+    draw_players()
 
+    -- Some text over the Canvas
+    love.graphics.setCanvas(canvas)
+    love.graphics.setColor(text_color)
+    font = love.graphics.newFont('SourceCodePro-Regular.ttf', 28)
+    font:setFilter('linear')
+    -- Set font before drawing text .. and something is wrong here...
+    love.graphics.setFont(font);
+    love.graphics.print ('p1:' .. p1.state .. ' p2:' .. p2.state , 25, 25, 0, 1, 1)
+    
     -- Back to the screen
     love.graphics.setCanvas()
 
@@ -179,9 +90,6 @@ function love.draw()
     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
 

+ 131 - 0
players.lua

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