Guitar Hero like issues implemented in Love2d

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Roguitar88
Prole
Posts: 2
Joined: Sun Aug 14, 2016 7:46 pm

Guitar Hero like issues implemented in Love2d

Post by Roguitar88 »

This time I'm trying to do a game like Guitar Hero. But instead of a guitar I'm using drums (that is, a key-based drumming game). We would have two targets (squares) named HitterTouched1 and HitterTouched2 through which the keys drawn on the screen must pass (collision part). That collision part was already solved, once the player has to press the key with the respective character, when the latter hits the target. For example, we have several letters (maybe you don't know anything about drums or music, but I think you'll understand); if I push the "k" key, a sound file will be loaded and executed, corresponding to the snare of the drum. But the sound will be played only if the key drawn on the screen hits the target. This way, I created a png image file for each key, each key being a part of the drum (we have eleven parts: for the right hand - snare (k), bass (n), tom 1(i), tom 2 (o), floor tom(p), rim(l) / now for the left hand - closed hihat(r), open hihat(w), trash cymbal(a), ride cymbal(g), crash cymbal(h)). Thus, we have misses and hits counters. If I push the key at the wrong time, at the wrong place, I will have misses. Now if I push the key at the right time, in the right place, I will have hits. There is a limit of 100 misses, or game over. You win, when you have 1000 hits. Remember that it consists of a rythm game. The objective is to make the player play a rythm, that is, a drums groove. What is really not working is the sequence of the keys going to the targets. The way the code was written, it only works with the first key, but not with the others that are coming after. So much that I created two signaling variables, like a dial on the screen that says when the key touches the target. The program interprets this sequence (row) of keys like one. I've tried to use vectors, but I've been learning how to program with lua and love2d of late and I'm not experienced. I'm not good with vectors, matrices and loops, but I know I'll have to use them a little bit. There follows all the code divided into files:

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
keyhit.lua

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
keys.lua

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
Well, that's part of the code, but I believe it's sufficient. If you need something else, or further explanation, please let me know. I need this done by 08/12, because it's an essay for Computer Science college here in Brazil. Thanks a lot and sorry for my bad English!! ;)
Last edited by Roguitar88 on Mon Aug 15, 2016 3:24 pm, edited 1 time in total.
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Guitar Hero like issues implemented in Love2d

Post by pgimeno »

Without a complete .love file, it's hard to know what may be going on. I notice, however, that you're using a lot of globals that seem unnecessary, to the point that I wonder if your problem is a clash between global variables.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Guitar Hero like issues implemented in Love2d

Post by zorg »

Just wanted to mention that, to me, your gameplay logic description is a bit off (bolding was my emphasis):
Roguitar88 wrote:Thus, we have misses and hits counters. If I push the key at the wrong time, at the wrong place, I will have misses. Now if I push the key at the right time, in the wrong place, I will have hits.
Usually, for one to score in these kinds of games, you need to be on-time AND you also, at the same time, need to hit the proper buttons, or whatever you wish to call them.
You made it sound like i can win by spamming whatever as long as my timing's spot-on.

Code: Select all

nothit = false --I really don't know what it is for
It's your code isn't it? I'd suggest actually understanding what you write before you go over your head, but hey, that's just my opinion. :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Roguitar88
Prole
Posts: 2
Joined: Sun Aug 14, 2016 7:46 pm

Re: Guitar Hero like issues implemented in Love2d

Post by Roguitar88 »

zorg wrote:Just wanted to mention that, to me, your gameplay logic description is a bit off (bolding was my emphasis):
Roguitar88 wrote:Thus, we have misses and hits counters. If I push the key at the wrong time, at the wrong place, I will have misses. Now if I push the key at the right time, in the wrong place, I will have hits.
Usually, for one to score in these kinds of games, you need to be on-time AND you also, at the same time, need to hit the proper buttons, or whatever you wish to call them.
You made it sound like i can win by spamming whatever as long as my timing's spot-on.

Code: Select all

nothit = false --I really don't know what it is for
It's your code isn't it? I'd suggest actually understanding what you write before you go over your head, but hey, that's just my opinion. :3
Haha, I typed so fast, that I didn't realized the mistake I made. It's "right" instead. As a matter of fact, this code is an adaptation by me from another code I found here in the forum. I will add the whole folder for you to analyze it better. I've even solved the problem today, but I think the code could be simply more concise.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 1 guest