Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Create a game in a day

Create a game in a day

Use LUA and LOVE to create a game in a day

NPC Unlimited

May 25, 2013
Tweet

Other Decks in Technology

Transcript

  1. Choosing a Language • Easy to use • Cross Platform

    • Open Source • LUA & LÖVE 2 Saturday, May 25, 13
  2. Choose a game • Simple • Easy Graphics & sounds

    • Expandable • Whack a Mole 3 Saturday, May 25, 13
  3. Hello World Love • Love executable • Code • Run:

    drag folder to Love shortcut function love.draw() love.graphics.print('Hello Love!', 400, 300) end 7 Saturday, May 25, 13
  4. Graphics • Assets (Smiley and Blue) • Code smileyImage =

    love.graphics.newImage(smiley.png) smileyWidth = smileyImage.getWidth() smileyHeight = smileyImage.getHeight() 9 Saturday, May 25, 13
  5. Putting it together local smileyImage local smileyX, smileyY local smileyWidth,

    smileyHeight function love.load() smileyImage = love.graphics.newImage("Happy.png") smileyWidth = smileyImage:getWidth() smileyHeight = smileyImage:getHeight() smileyX = 100 smileyY = 100 love.graphics.setBackgroundColor(0,0,0,255) end function love.draw() love.graphics.draw(smileyImage,smileyX,smileyY) end 12 Saturday, May 25, 13
  6. Mouse function love.mousereleased(x, y, button) if button == "l" then

    smileyX = x smileyY = y end end 13 Saturday, May 25, 13
  7. Mouse, what’s wrong? function love.mousereleased(x, y, button) ! if button

    == "l" then ! ! smileyX = x - (smileyWidth/2) ! ! smileyY = y - (smileyHeight/2) ! end end 14 Saturday, May 25, 13
  8. Sounds Local Variable: local squishSound Load: squishSound = love.sound.newSoundData("sounds/squish.wav") squishSfx

    = love.audio.newSource(squishSound) Mouse Release: love.audio.play(squishSfx) 15 Saturday, May 25, 13
  9. Score Local Variable: local score Load: score = 0 Mouse

    Release: score = score + 10 Draw: love.graphics.print('Score: ' .. score, 700, 10) 16 Saturday, May 25, 13
  10. Blues Local Variable: local blueImage local faceType Load: blueImage =

    love.graphics.newImage("images/Blue.png") faceType = 0 Draw: if(smiley.getFaceType() == 1) then love.graphics.draw(smileyImage,smiley.getX(),smiley.getY()) else love.graphics.draw(blueImage,smiley.getX(),smiley.getY()) end Mouse Release: function love.mousereleased(x, y, button) if button == "l" then if(faceType == 0) then hitFace = inRect(x,y,smileyX,smileyY,smileyWidth,smileyHeight) love.audio.play(squishSfx) score = score + 10 faceType = 1 end end end 17 Saturday, May 25, 13
  11. inRect function inRect(x,y,smileyX,smileyY,smileyWidth,smileyHeight) isInRect = true if x < smileyX

    then isInRect = false end if x > (smileyX + smileyWidth) then isInRect = false end if y < smileyY then isInRect = false end if y > (smileyY + smileyHeight) then isInRect = false end return isInRect end 18 Saturday, May 25, 13
  12. Random Local Variable: local gameWidth local gameHeight Load: gameWidth =

    love.graphics.getWidth() gameHeight = love.graphics.getHeight() setSmileyToRandomLocation() New Function: function setSmileyToRandomLocation() smileyX = math.random(gameWidth) smileyY = math.random(gameHeight) end 19 Saturday, May 25, 13
  13. Movement Local Variable: local smileyXVelocity local smileyYVelocity Load: smileyXVelocity =

    1 smileyYVelocity = 1 New Function: function love.update(dt) smileyX = smileyX + smileyXVelocity smileyY = smileyY + smileyYVelocity if smileyX < 0 or smileyX + smileyWidth > gameWidth then smileyXVelocity = -smileyXVelocity end if smileyY < 0 or smileyY + smileyHeight > gameHeight then smileyYVelocity = - smileyYVelocity end end 20 Saturday, May 25, 13
  14. Another File? Smiley = {} Smiley.new = function(imageWidth,imageHeight) -- #PRIVATE

    VARIABLES local self = {} local width = imageWidth or 64 local height = imageHeight or 64 local faceType = 0 local x = math.random(love.graphics.getWidth() - width) local y = math.random(love.graphics.getHeight() - height) local maxVelocity = 50 local xVelocity = (math.random(maxVelocity)-(maxVelocity/2))/100 local yVelocity = (math.random(maxVelocity)-(maxVelocity/2))/100 21 Saturday, May 25, 13
  15. Another File, cont... -- #GETTERS self.getWidth = function() return width

    end self.getHeight = function() return height end self.getFaceType = function() return faceType end self.getX = function() return x end self.getY = function() return y end self.getXVelocity = function() return xVelocity end self.getYVelocity = function() return yVelocity end -- #SETTERS self.setFaceType = function(newFaceType) faceType = newFaceType end 22 Saturday, May 25, 13
  16. Another File, cont... -- # OTHER METHODS self.moveSmiley = function

    (dt) x = x + xVelocity y = y + yVelocity if ((x < 0) or ((x + width) > love.graphics.getWidth())) then xVelocity = -xVelocity end if ((y < 0) or ((y + height) > love.graphics.getHeight())) then yVelocity = -yVelocity end end self.inSmiley = function(xLoc,yLoc) local isInRect = true if xLoc < x then isInRect = false end if xLoc > (x + width) then isInRect = false end if yLoc < y then isInRect = false end if yLoc > (y + height) then isInRect = false end return isInRect end return self end 23 Saturday, May 25, 13
  17. Now main must change require 'socket' require("smiley") local smileyImage, blueImage

    local squishSound local squishSfx local score local gameWidth, gameHeight local smileys local newSmileys 24 Saturday, May 25, 13
  18. Main must change... function love.load() smileyImage = love.graphics.newImage("images/Happy.png") blueImage =

    love.graphics.newImage("images/Blue.png") squishSound = love.sound.newSoundData("sounds/squish.wav") squishSfx = love.audio.newSource(squishSound) love.graphics.setBackgroundColor(0,0,0,255) score = 0 gameWidth = love.graphics.getWidth() gameHeight = love.graphics.getHeight() smileys = {} newSmileys = {} seed = socket.gettime()*10000 math.randomseed(seed) table.insert(smileys,Smiley.new(blueImage:getWidth(), blueImage:getHeight())) end 25 Saturday, May 25, 13
  19. Main must change... function love.draw() for i,smiley in ipairs(smileys) do

    if(smiley.getFaceType() == 1) then love.graphics.draw(smileyImage,smiley.getX(),smiley.getY()) else love.graphics.draw(blueImage,smiley.getX(),smiley.getY()) end end love.graphics.print('Score: ' .. score, 700, 10) end 26 Saturday, May 25, 13
  20. Main must change... function love.mousereleased(x, y, button) if button ==

    "l" then hitOne = false for i,smiley in ipairs(smileys) do hit = checkSmiley(x,y,smiley) if hit == true then hitOne = true end end if hitOne == true then score = score + 10 else score = score - 1 end if #newSmileys > 0 then for i,smiley in ipairs(newSmileys) do table.insert(smileys,smiley) end newSmileys = {} end end end 27 Saturday, May 25, 13
  21. Main must change... function checkSmiley(x,y,smiley) hit = false if(smiley.getFaceType() ==

    0) then hitFace = smiley.inSmiley(x,y) if(hitFace) then love.audio.play(squishSfx) smiley.setFaceType(1) hit = true table.insert(newSmileys,Smiley.new(blueImage:getWidth(), blueImage:getHeight())) end end return hit end function love.update(dt) for i,smiley in ipairs(smileys) do smiley.moveSmiley(dt) end end 28 Saturday, May 25, 13