Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --PROJECT CANDYLAND SAGA
- --MAIN.LUA
- --CANDY.LUA
- --Music from
- --http://opengameart.org/content/jewel-crusher
- --http://opengameart.org/content/menu-loop
- --Art from
- --http://opengameart.org/content/cursor
- --Janna http://opengameart.org/content/sleek-bars
- --http://opengameart.org/content/candy-pack-1
- --http://openclipart.org/detail/62707
- --screen size 800x600
- local class = require 'middleclass'
- Timer = require 'hump.timer'
- --Load Classes
- require('candy')
- function love.load()
- gameState = "load"
- math.randomseed(os.time()) -- seed random number gen
- --image / music / fonts loading
- background1 = love.graphics.newImage("art/background1.jpg")
- playerImg = love.graphics.newImage("art/player.png")
- loadMenu = love.graphics.newImage("art/loadMenu.png")
- mainMenu = love.graphics.newImage("art/mainMenu.png")
- mouseImg = love.graphics.newImage("art/mouse.png")
- loadMenuMusic = love.audio.newSource("audio/loadmenu.wav")
- getCandyMusic = love.audio.newSource("audio/getcandy.wav")
- playingMusic = love.audio.newSource("audio/jewel_crusher.mp3")
- menuScoreFont = love.graphics.newFont(35)
- playScoreFont = love.graphics.newFont(12)
- loadMenuMusic:play()
- loadMenuMusic:setLooping(true)
- playingMusic:setVolume(0.7)
- screenW = love.window.getWidth()
- screenH = love.window.getHeight()
- love.mouse.setVisible(false)
- candies = {}
- candiesC = 0
- player = {
- x = screenW / 2 - 50,
- y = screenH / 2 + 150,
- w = 100,
- h = 23,
- vel = 500,
- img = playerImg,
- score = 0,
- lifes = 3,
- lessRainbows = false,
- doubleScore = false,
- updateLifes = false
- };
- playButton = {
- x = 312,
- y = 273,
- w = 210,
- h = 63
- };
- exitButton = {
- x = 312,
- y = 352,
- w = 210,
- h = 63
- };
- mPlayButton = {
- x = 555,
- y = 24,
- w = 213,
- h = 66
- };
- doubleScoreButton = {
- x = 10,
- y = 200,
- w = 192,
- h = 74,
- img = love.graphics.newImage("art/doubleScoreButton_false.png")
- };
- lessRainbowsButton = {
- x = 212,
- y = 200,
- w = 192,
- h = 74,
- img = love.graphics.newImage("art/lessRainbowsButton_false.png")
- };
- heartImage = {
- x = 0,
- y = 0,
- w = 23,
- h = 25,
- img = love.graphics.newImage("art/heart.png"),
- };
- end
- function love.update(dt)
- Timer.update(dt)
- --CLASS UPDATES
- if gameState == "play" then
- for x = 0, candiesC - 1 do
- candies[x]:update(dt, player)
- end
- end
- -- MAIN UPDATE
- if gameState == "load" then
- local mX, mY = love.mouse.getPosition()
- if CheckCollision(mX, mY, 20, 20, playButton.x, playButton.y, playButton.w, playButton.h) and love.mouse.isDown("l") then
- gameState = "play"
- loadMenuMusic:stop()
- Timer.addPeriodic(0.3, function() if gameState == "play" then createCandy(2) end end)
- elseif CheckCollision(mX, mY, 20, 20, exitButton.x, exitButton.y, exitButton.w, exitButton.h) and love.mouse.isDown("l") then
- love.event.quit()
- end
- elseif gameState == "menu" then
- playingMusic:stop()
- local mX, mY = love.mouse.getPosition()
- if CheckCollision(mX, mY, 20, 20, mPlayButton.x, mPlayButton.y, mPlayButton.w, mPlayButton.h) and love.mouse.isDown("l") then
- gameState = "play"
- loadMenuMusic:stop()
- player.lifes = 3
- heartImage.draw = 3
- Timer.addPeriodic(0.3, function() if gameState == "play" then createCandy(2) end end)
- end
- if CheckCollision(mX, mY, 20, 20, doubleScoreButton.x, doubleScoreButton.y, doubleScoreButton.w, doubleScoreButton.h) and love.mouse.isDown("l") then
- if player.score >= 500 and not player.doubleScore then
- player.score = player.score - 500
- player.doubleScore = true
- doubleScoreButton.img = love.graphics.newImage("art/doubleScoreButton_true.png")
- end
- end
- if CheckCollision(mX, mY, 20, 20, lessRainbowsButton.x, lessRainbowsButton.y, lessRainbowsButton.w, lessRainbowsButton.h) and love.mouse.isDown("l") then
- if player.score >= 250 and not player.lessRainbows then
- player.score = player.score - 250
- player.lessRainbows = true
- lessRainbowsButton.img = love.graphics.newImage("art/lessRainbowsButton_true.png")
- end
- end
- elseif gameState == "play" then
- if player.lifes <= 0 then
- playingMusic:stop()
- loadMenuMusic:play()
- for k in pairs(candies) do candies[k] = nil end --clears the candies table
- candiesC = 0
- Timer.clear()
- gameState = "menu"
- else
- playingMusic:play()
- player.x = love.mouse.getX()
- for x = 0, candiesC - 1 do
- if CheckCollision(player.x, player.y, player.w, player.h, candies[x].x, candies[x].y, candies[x].h2, candies[x].h) then
- player.score = player.score + 1
- if player.doubleScore then
- player.score = player.score + 1
- end
- candies[x].x = -100
- candies[x].active = false
- getCandyMusic:play()
- end
- end
- end
- end
- end
- function love.draw()
- if gameState == "load" then
- love.graphics.draw(loadMenu)
- local x, y = love.mouse.getPosition()
- love.graphics.draw(mouseImg, x, y)
- elseif gameState == "menu" then
- love.graphics.setFont(menuScoreFont)
- love.graphics.draw(mainMenu)
- love.graphics.setColor(0,0,0) -- black
- love.graphics.print("Score: " .. player.score, 565, 115)
- love.graphics.setColor(255,255,255) -- white
- love.graphics.draw(doubleScoreButton.img, doubleScoreButton.x, doubleScoreButton.y)
- love.graphics.draw(lessRainbowsButton.img, lessRainbowsButton.x, lessRainbowsButton.y)
- local x, y = love.mouse.getPosition()
- love.graphics.draw(mouseImg, x, y)
- elseif gameState == "play" then
- love.graphics.setFont(playScoreFont)
- love.graphics.draw(background1)
- love.graphics.draw(player.img, player.x, player.y)
- love.graphics.print(player.score, player.x+(player.w/2)-5, player.y+5)
- for x = 0, candiesC - 1 do
- candies[x]:draw()
- end
- if player.lifes == 3 then
- love.graphics.draw(heartImage.img, heartImage.x, heartImage.y)
- love.graphics.draw(heartImage.img, heartImage.x + 25, heartImage.y)
- love.graphics.draw(heartImage.img, heartImage.x + 50, heartImage.y)
- elseif player.lifes == 2 then
- love.graphics.draw(heartImage.img, heartImage.x, heartImage.y)
- love.graphics.draw(heartImage.img, heartImage.x + 25, heartImage.y)
- else
- love.graphics.draw(heartImage.img, heartImage.x, heartImage.y)
- end
- end
- end
- function createCandy(amount)
- for x = 1, amount do
- candies[candiesC] = Candy:new(math.random(0,screenW - 50), 0, player)
- candiesC = candiesC + 1
- end
- end
- function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
- return x1 < x2+w2 and
- x2 < x1+w1 and
- y1 < y2+h2 and
- y2 < y1+h1
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement