Files
beat_o_char/main.lua
Indrajith K L 6baf6c48b0 Initial Commit
* Basic Game Implementation
2024-08-07 02:48:17 +05:30

131 lines
3.7 KiB
Lua

moonshine = require("libs.moonshine")
local bpm = require("libs.lovebpm")
local r, g, b = 0,0,0
local random_characters = ""
local score = 0
local luminance = 0
local progressBarWidth = 0
local progressBarHeight = 10
local progressBarX = 0
local progressBarY = 590
local musicLength = 0
local progress = 0
local finish = false
function love.load()
progressBarWidth = love.graphics.getWidth()
main_font = love.graphics.newFont("resources/fonts/Shortcake.ttf", 50)
score_font = love.graphics.newFont("resources/fonts/dealerplate_california.otf", 30)
effect = moonshine(moonshine.effects.filmgrain)
.chain(moonshine.effects.vignette)
.chain(moonshine.effects.scanlines)
.chain(moonshine.effects.chromasep)
effect.chromasep.angle = 1
effect.chromasep.radius = 2
effect.scanlines.thickness = .5
effect.scanlines.opacity = .5
loadMusic()
end
function love.update(dt)
music:update()
currentTime = music:getTime()
if math.floor(currentTime)==math.floor(musicLength) then
finish = true
end
progress = currentTime/musicLength
end
function love.draw()
effect(function ()
if luminance > 0.5 then
love.graphics.setColor(0,0,0)
else
love.graphics.setColor(1,1,1)
end
if not finish then
draw_game_logic()
else
draw_final_score()
end
draw_progress_bar()
end)
end
function love.keypressed(key, scancode, isrepeat)
if not finish then
if key == random_characters then
score = score + 1
else
score = score - 1
end
else
if key == "space" then
reset()
end
end
end
function draw_game_logic()
local text_width = main_font:getWidth(random_characters)
local text_height = main_font:getHeight()
local width, height = love.graphics.getDimensions()
local charX = (width - text_width)/2
local charY = (height - text_height)/2
love.graphics.setFont(score_font)
love.graphics.print("Score: "..score, 685, 10)
love.graphics.setFont(main_font)
love.graphics.setBackgroundColor(r, g, b)
love.graphics.printf(random_characters, charX, charY, text_width, "center")
end
function draw_final_score()
local scoreText = "Final Score: "..score
local text_width = main_font:getWidth(scoreText)
local text_height = main_font:getHeight()
local width, height = love.graphics.getDimensions()
local scoreX = (width - text_width)/2
local scoreY = (height - text_height)/2
love.graphics.setFont(main_font)
love.graphics.printf(scoreText, scoreX, scoreY, text_width, "center")
end
function draw_progress_bar()
love.graphics.setColor(0.5, 0.5, 0.5)
love.graphics.rectangle("fill", progressBarX, progressBarY, progressBarWidth, progressBarHeight)
love.graphics.setColor(0, 1, 0)
love.graphics.rectangle("fill", progressBarX, progressBarY, progressBarWidth * progress, progressBarHeight)
end
function loadMusic()
music = bpm.newTrack()
music:load("resources/music/Goth - Sidewalks and Skeletons.mp3")
music:on("beat", function(n)
if n > 0 then
r, g, b = math.random(255)/255, math.random(255)/255, math.random(255)/255
random_characters = random_char()
luminance = get_luminance()
end
end)
music:play()
musicLength = music:getTotalTime();
end
function reset()
finish = false
score = 0
currentTime = 0
loadMusic()
end
function random_char()
local alphabet = "abcdefghijklmnopqrstuvwxyz"
local random_index = math.random(1, #alphabet)
return alphabet:sub(random_index, random_index)
end
function get_luminance()
return 0.2126 * r + 0.7152*g + 0.0722*b
end