collisions.lua
Code: Select all
collision = {}
function collision:detect(x1,y1,w1,h1,x2,y2,w2,h2)
return x1 < x2+w2 and
x2 < x1+w1
end
return collision
Code: Select all
-- The code below represents the targets of our game (you hit the note or beat, only when you hit the key on the target at the right time, forming a rythm)
--target 1
keyhit = {}
keyhit.x = 690 --258
keyhit.y = 483
keyhit.w = 45
keyhit.h = 38
--target 2
keyhit2 = {}
keyhit2.x = 690
keyhit2.y = 533
keyhit2.w = 45
keyhit2.h = 38
--Drawing the targets
function DrawHitter()
love.graphics.setLineWidth(3)
love.graphics.rectangle('line', keyhit.x, keyhit.y, keyhit.w, keyhit.h)
love.graphics.rectangle('line', keyhit2.x, keyhit2.y, keyhit2.w, keyhit2.h)
end
Code: Select all
require 'collisions'
require 'keyhit'
key3 = {}
key3.x = 100
key3.y = 485
key3.w = 32
key3.h = 32
--The key1 variable will store the keys that go running to the right towards the first target. The key2 variable will store the keys that go running to the right towards the second target.
key1 = {}
--closedhihatKey, openhihatKey2, trashKey2, rideKey2, crashKey2, drumsticksKey2
key1.x = 200 --690
key1.y = 485
key1.w = 32
key1.h = 32
key1.speed = 0.05 --speed of the keys race
key1.hits = 0
key1.misses = 0
key1.TouchingHitter = 0
key2 = {}
--{snareKey2, rimKey2, bassKey2, tom1Key2, tom2Key2, floortomKey2}
key2.x = 200
key2.y = 535
key2.w = 32
key2.h = 32
key2.TouchingHitter = 0
nothit = false --I really don't know what it is for
--We print on the screen the Points counter (misses counter and hits counter, yep, somethin'like that)
--This part will print all the images and background that shows on Game mode. The virtual keys are also printed in here
function KeyDraw()
love.graphics.print("This is Game mode. Press Y to exit and go back to Practice mode", 20, 450)
love.graphics.print("Hits: "..key1.hits, 20, 465)
love.graphics.print("Misses: "..key1.misses, 120, 465)
love.graphics.print("HitterTouched 1: "..key1.TouchingHitter, 220, 465)
love.graphics.print("HitterTouched 2: "..key2.TouchingHitter, 400, 465)
-- I think it is a little confusing even to myself
key3[1] = love.graphics.draw(closedhihatKey, key3.x, key3.y)
key1[2] = love.graphics.draw(closedhihatKey, key1.x, key1.y)
key2[1] = love.graphics.draw(bassKey, key2.x - 100, key2.y)
key2[2] = love.graphics.draw(snareKey, key2.x, key2.y)
DrawHitter()
love.graphics.draw(drummer_sprite, activeFrame, 15, 480)
love.graphics.draw(lefthand, 150, 490)
love.graphics.draw(righthand, 150, 535)
end
--Collision teller: "yes" will show whenever a collision between a key and the square target occurs (HitterTouched1 and HitterTouched2)
--it's valid for both targets
function AntiSpamHitter()
if collision:detect(key3.x, key3.y, key3.w, key3.h, keyhit.x, keyhit.y, keyhit.w, keyhit.h) then
key1.TouchingHitter = "yes"
else
key1.TouchingHitter = "no"
end
-- I think this part above, involving key3 is not necessary. It would be more practical if that stuff was in a vector, I guess, like, for example, key1[1], key1[1], key2[1], key2[2] and so on and on...
if collision:detect(key1.x, key1.y, key1.w, key1.h, keyhit.x, keyhit.y, keyhit.w, keyhit.h) then
key1.TouchingHitter = "yes"
else
key1.TouchingHitter = "no"
end
if collision:detect(key2.x, key2.y, key2.w, key2.h, keyhit2.x, keyhit2.y, keyhit2.w, keyhit2.h) then
key2.TouchingHitter = "yes"
else
key2.TouchingHitter = "no"
end
end
--Keys moving, the screen width is 759 pixels, but, a priori, I set 1000 pixels (more space) for the keys race.
function KeyMove()
repeat
key1.x = key1.x + key1.speed
until nothit == false
if key1.x > 1000 then
key1.x = 200
end
--------------------------------
repeat
key2.x = key2.x + key1.speed
until nothit == false
if key2.x > 1000 then
key2.x = 200
end
--------------------------------
repeat
key3.x = key3.x + key1.speed
until nothit == false
if key3.x > 1000 then
key3.x = 200
end
end
--When a specific key is pushed, when the virtual key on the screen touches the target, a sound corresponding to a drum part is played. For example, if I press "k" at the time when the virtual key draw on the screen touches the target (square), the player will hear a snare sound.
function PlayingKeys(key)
if key2.TouchingHitter == "no" and key == "k" then
key1.misses = key1.misses + 1
end
if key2.TouchingHitter == "yes" and key == "k" then
--key2.x = 200
key1.hits = key1.hits + 1
love.audio.stop(snare)
love.audio.play(snare)
arrow1 = true
end
if key2.TouchingHitter == "no" and key == "n" then
key1.misses = key1.misses + 1
end
if key2.TouchingHitter == "yes" and key == "n" then
--key2.x = 100
key1.hits = key1.hits + 1
love.audio.stop(bass)
love.audio.play(bass)
arrow3 = true
end
if key1.TouchingHitter == "no" and key == "r" then
key1.misses = key1.misses + 1
end
if key1.TouchingHitter == "yes" and key == "r" then
--key1.x = 200
key1.hits = key1.hits + 1
love.audio.stop(hihat)
love.audio.play(hihat)
arrow4 = true
end
end
--increasing speed each time the player has two hits -- this is just an example. Of course I will set a limit of 100 hits for increasing speed, to make things more thrilling a little bit.
function increaseSpeed()
if key1.hits > 2 then
key1.speed = 0.1
end
if key1.hits > 4 then
key1.speed = 0.2
end
if key1.hits > 6 then
key1.speed = 0.3
end
if key1.hits > 8 then
key1.speed = 0.4
end
end