Advertisement
10100

main.lua

Jan 25th, 2014
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.49 KB | None | 0 0
  1. --PROJECT CANDYLAND SAGA
  2. --MAIN.LUA
  3. --CANDY.LUA
  4.  
  5. --Music from
  6. --http://opengameart.org/content/jewel-crusher
  7. --http://opengameart.org/content/menu-loop
  8. --Art from
  9. --http://opengameart.org/content/cursor
  10. --Janna http://opengameart.org/content/sleek-bars
  11. --http://opengameart.org/content/candy-pack-1
  12. --http://openclipart.org/detail/62707
  13.  
  14. --screen size 800x600
  15.  
  16. local class = require 'middleclass'
  17. Timer = require 'hump.timer'
  18.  
  19.  
  20. --Load Classes
  21. require('candy')
  22.  
  23. function love.load()
  24.     gameState = "load"
  25.    
  26.     math.randomseed(os.time()) -- seed random number gen
  27.    
  28.     --image / music / fonts loading
  29.     background1 = love.graphics.newImage("art/background1.jpg")
  30.     playerImg = love.graphics.newImage("art/player.png")
  31.     loadMenu = love.graphics.newImage("art/loadMenu.png")
  32.     mainMenu = love.graphics.newImage("art/mainMenu.png")
  33.     mouseImg = love.graphics.newImage("art/mouse.png")
  34.     loadMenuMusic = love.audio.newSource("audio/loadmenu.wav")
  35.     getCandyMusic = love.audio.newSource("audio/getcandy.wav")
  36.     playingMusic = love.audio.newSource("audio/jewel_crusher.mp3")
  37.     menuScoreFont = love.graphics.newFont(35)
  38.     playScoreFont = love.graphics.newFont(12)
  39.    
  40.     loadMenuMusic:play()
  41.     loadMenuMusic:setLooping(true)
  42.     playingMusic:setVolume(0.7)
  43.    
  44.     screenW = love.window.getWidth()
  45.     screenH = love.window.getHeight()
  46.    
  47.     love.mouse.setVisible(false)
  48.    
  49.     candies = {}
  50.     candiesC = 0
  51.    
  52.     player = {
  53.         x = screenW / 2 - 50,
  54.         y = screenH / 2 + 150,
  55.         w = 100,
  56.         h = 23,
  57.         vel = 500,
  58.         img = playerImg,
  59.         score = 0,
  60.         lifes = 3,
  61.         lessRainbows = false,
  62.         doubleScore = false,
  63.         updateLifes = false
  64.         };
  65.        
  66.     playButton = {
  67.         x = 312,
  68.         y = 273,
  69.         w = 210,
  70.         h = 63
  71.     };
  72.     exitButton = {
  73.         x = 312,
  74.         y = 352,
  75.         w = 210,
  76.         h = 63
  77.     };
  78.    
  79.     mPlayButton = {
  80.         x = 555,
  81.         y = 24,
  82.         w = 213,
  83.         h = 66
  84.     };
  85.    
  86.     doubleScoreButton = {
  87.         x = 10,
  88.         y = 200,
  89.         w = 192,
  90.         h = 74,
  91.         img = love.graphics.newImage("art/doubleScoreButton_false.png")
  92.     };
  93.    
  94.     lessRainbowsButton = {
  95.         x = 212,
  96.         y = 200,
  97.         w = 192,
  98.         h = 74,
  99.         img = love.graphics.newImage("art/lessRainbowsButton_false.png")
  100.     };
  101.    
  102.     heartImage = {
  103.         x = 0,
  104.         y = 0,
  105.         w = 23,
  106.         h = 25,
  107.         img = love.graphics.newImage("art/heart.png"),
  108.     };
  109. end
  110.  
  111. function love.update(dt)
  112.     Timer.update(dt)
  113.  
  114.     --CLASS UPDATES
  115.     if gameState == "play" then
  116.         for x = 0, candiesC - 1 do
  117.             candies[x]:update(dt, player)
  118.         end
  119.     end
  120.  
  121.     -- MAIN UPDATE
  122.     if gameState == "load" then
  123.         local mX, mY = love.mouse.getPosition()
  124.         if CheckCollision(mX, mY, 20, 20, playButton.x, playButton.y, playButton.w, playButton.h) and love.mouse.isDown("l") then
  125.             gameState = "play"
  126.             loadMenuMusic:stop()
  127.             Timer.addPeriodic(0.3, function() if gameState == "play" then createCandy(2) end end)
  128.         elseif CheckCollision(mX, mY, 20, 20, exitButton.x, exitButton.y, exitButton.w, exitButton.h) and love.mouse.isDown("l") then
  129.             love.event.quit()
  130.         end
  131.     elseif gameState == "menu" then
  132.         playingMusic:stop()
  133.         local mX, mY = love.mouse.getPosition()
  134.         if CheckCollision(mX, mY, 20, 20, mPlayButton.x, mPlayButton.y, mPlayButton.w, mPlayButton.h) and love.mouse.isDown("l") then
  135.             gameState = "play"
  136.             loadMenuMusic:stop()
  137.             player.lifes = 3
  138.             heartImage.draw = 3
  139.             Timer.addPeriodic(0.3, function() if gameState == "play" then createCandy(2) end end)
  140.         end
  141.         if CheckCollision(mX, mY, 20, 20, doubleScoreButton.x, doubleScoreButton.y, doubleScoreButton.w, doubleScoreButton.h) and love.mouse.isDown("l") then
  142.             if player.score >= 500 and not player.doubleScore then
  143.                 player.score = player.score - 500
  144.                 player.doubleScore = true
  145.                 doubleScoreButton.img = love.graphics.newImage("art/doubleScoreButton_true.png")
  146.             end
  147.         end
  148.         if CheckCollision(mX, mY, 20, 20, lessRainbowsButton.x, lessRainbowsButton.y, lessRainbowsButton.w, lessRainbowsButton.h) and love.mouse.isDown("l") then
  149.             if player.score >= 250 and not player.lessRainbows then
  150.                 player.score = player.score - 250
  151.                 player.lessRainbows = true
  152.                 lessRainbowsButton.img = love.graphics.newImage("art/lessRainbowsButton_true.png")
  153.             end
  154.         end
  155.     elseif gameState == "play" then
  156.         if player.lifes <= 0 then
  157.             playingMusic:stop()
  158.             loadMenuMusic:play()
  159.             for k in pairs(candies) do candies[k] = nil end --clears the candies table
  160.             candiesC = 0
  161.             Timer.clear()
  162.             gameState = "menu"
  163.         else
  164.             playingMusic:play()
  165.             player.x = love.mouse.getX()
  166.             for x = 0, candiesC - 1 do
  167.                 if CheckCollision(player.x, player.y, player.w, player.h, candies[x].x, candies[x].y, candies[x].h2, candies[x].h) then
  168.                     player.score = player.score + 1
  169.                     if player.doubleScore then
  170.                         player.score = player.score + 1
  171.                     end
  172.                     candies[x].x = -100
  173.                     candies[x].active = false
  174.                     getCandyMusic:play()
  175.                 end
  176.             end
  177.         end
  178.     end
  179. end
  180.  
  181. function love.draw()
  182.     if gameState == "load" then
  183.         love.graphics.draw(loadMenu)
  184.         local x, y = love.mouse.getPosition()
  185.         love.graphics.draw(mouseImg, x, y)
  186.     elseif gameState == "menu" then
  187.         love.graphics.setFont(menuScoreFont)
  188.         love.graphics.draw(mainMenu)
  189.         love.graphics.setColor(0,0,0) -- black
  190.         love.graphics.print("Score: " .. player.score, 565, 115)
  191.         love.graphics.setColor(255,255,255) -- white
  192.         love.graphics.draw(doubleScoreButton.img, doubleScoreButton.x, doubleScoreButton.y)
  193.         love.graphics.draw(lessRainbowsButton.img, lessRainbowsButton.x, lessRainbowsButton.y)
  194.         local x, y = love.mouse.getPosition()
  195.         love.graphics.draw(mouseImg, x, y)
  196.     elseif gameState == "play" then
  197.         love.graphics.setFont(playScoreFont)
  198.         love.graphics.draw(background1)
  199.         love.graphics.draw(player.img, player.x, player.y)
  200.         love.graphics.print(player.score, player.x+(player.w/2)-5, player.y+5)
  201.         for x = 0, candiesC - 1 do
  202.             candies[x]:draw()
  203.         end
  204.         if player.lifes == 3 then
  205.             love.graphics.draw(heartImage.img, heartImage.x, heartImage.y)
  206.             love.graphics.draw(heartImage.img, heartImage.x + 25, heartImage.y)
  207.             love.graphics.draw(heartImage.img, heartImage.x + 50, heartImage.y)
  208.         elseif player.lifes == 2 then
  209.             love.graphics.draw(heartImage.img, heartImage.x, heartImage.y)
  210.             love.graphics.draw(heartImage.img, heartImage.x + 25, heartImage.y)
  211.         else
  212.             love.graphics.draw(heartImage.img, heartImage.x, heartImage.y)
  213.         end
  214.     end
  215. end
  216.  
  217. function createCandy(amount)
  218.     for x = 1, amount do
  219.         candies[candiesC] = Candy:new(math.random(0,screenW - 50), 0, player)
  220.         candiesC = candiesC + 1
  221.     end
  222. end
  223.  
  224. function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
  225.   return x1 < x2+w2 and
  226.          x2 < x1+w1 and
  227.          y1 < y2+h2 and
  228.          y2 < y1+h1
  229. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement