Skip to content

Flappy Guitar Scales

This example shows

  • How to track a sequence of guitar notes.
  • How to make simple animation of 2D sprites.

Lets build a Flappy Bird clone. But to make it musical, lets only flap as a scale of notes are played on the guitar, in different scales and tempos.


FlappyGuitarScales.lua

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
local scale = {0,7,0,4, 0, 5, 7}
local numSteps = 4
local scaleNames = {"C","C#","D","D#","E","F","F#", "G", "G#", "A", "A#", "B"}
local nextStepInScale = 0
local y = 0
local dy = 0
local pipes = {{-10,0}}
local speed = 0.2
local score = 0
local displayedScore = 0
local scaleTextures = {}
local scaleTexturesHighlight = {}
local crashBeat = 0
local timeSinceFlap = 1000

function UpdateNumStepsTex()
  Color(0,0,0,1)
  Rect(numStepsTex, 0,0,1,1)
  Color(0.4,0.4,0.4,1)
  Text(numStepsTex, 0,0,1,1, tostring(numSteps))
end

function UpdateScaleTex(step)
  Color(0,0,0,1)
  Rect(scaleTextures[step], 0,0,1,1)
  Color(0.4,0.4,0.4,1)
  Text(scaleTextures[step], 0,0,1,1, scaleNames[1+scale[step]])
  Color(0.9,0.7,.2,1)
  Rect(scaleTexturesHighlight[step], 0,0,1,1)
  Color(1,1,1,1)
  Text(scaleTexturesHighlight[step], 0,0,1,1, scaleNames[1+scale[step]])
end

function UpdateScoreTex()
  Color(0,0,0,1)
  Rect(pipe, 0,0.1,1,0.2)
  Color(1,1,1,1)
  Text(pipe, 0,0.1,1,0.2, tostring(score))
end

function IsNextStepInScale(pitch)
  return Round(pitch)%12 == scale[nextStepInScale+1]
end


function Initialize()
  p1Tex = Texture(knobShader, "SPEED")
  bird1 = Texture(nil, "https://jam.live/developers/examples/flappybird.png/0:3")
  bird2 = Texture(nil, "https://jam.live/developers/examples/flappybird.png/1:3")
  bird3 = Texture(nil, "https://jam.live/developers/examples/flappybird.png/2:3")
  pipe  = Texture(nil, "https://jam.live/developers/examples/pipe.png")
  instruction = Texture(nil,"Press Play to Play");
  bpm = Texture(nil,"Set BPM to adjust difficulty");
  numStepsTex = Texture(nil, nil);
  UpdateNumStepsTex()

  for i=1, 7 do
    scaleTextures[i] = Texture(nil, nil)
    scaleTexturesHighlight[i] = Texture(nil, nil)
    UpdateScaleTex(i)
  end
end  

function Process(time, beat, inputNotes)

  -- infinite track, so create pipes along the way
  local futureBeat = beat+4
  if crashBeat == 0 and math.abs(futureBeat-Round(futureBeat)) < 0.01 and Round(futureBeat) > Round(pipes[Round(#pipes)][1]) and Round(futureBeat)%4 == 0 then
    pipes[#pipes+1] = {futureBeat, math.max(0.8-beat/10, math.random())}
    score = score + 1
  end

  local flap = false
  for nid, n in pairs(inputNotes) do
    if n.age == 1 and IsNextStepInScale(n.pitch) then
      flap = true
      nextStepInScale = (nextStepInScale + 1) % numSteps
    end
  end

  if not flap then
    timeSinceFlap = timeSinceFlap + 1
  else
    timeSinceFlap = 0
  end

  -- crash test the last 4 pipes
  local crashing = false
  for i=math.max(1, #pipes-4), #pipes do
    if crashBeat == 0 and math.abs(pipes[i][1]- beat) < 0.2 and math.abs(pipes[i][2]-y) > 0.2 then
      crashing = true
      crashBeat = beat
    end
  end

  if crashing then
    y = y * 0.98 + 0.02
  end

  if dy < 0 then
    dy = dy *0.5
  end

  -- flaps and gravity
  if flap then 
    dy = -0.1/2
  else
    dy = dy+0.00001/2
    y = y + dy
  end

  if y > 0.8 then
    y = 0.8
    dy = 0
  elseif y < 0 then
    y = 0
    dy = 0
  end

  return inputNotes;
end


function Render(time, beat, mouse, keys, notes)

  if crashBeat > 0 then
    if beat == 0 then
      crashBeat = 0
      pipes = {{-10,0}}
      score = 0
    else
       beat = crashBeat
    end
  end

  -- draw score on texture
  if score ~= displayedScore then 
    UpdateScoreTex()
    displayedScore = score
  end


  Draw(numStepsTex, 0,0,0.1,0.1)
  if Clicked(mouse) then
    numSteps = numSteps + 1
    if numSteps == 8 then
      numSteps = 2
    end
    nextStepInScale = nextStepInScale%numSteps
    UpdateNumStepsTex();
  end


  -- draw scale
  for i=1, numSteps do
    if nextStepInScale == i-1 then
      Draw(scaleTexturesHighlight[i], 0.1+i/10, 0, 0.1,0.1)
    else
      Draw(scaleTextures[i], 0.1+i/10, 0, 0.1,0.1)
    end

    if Clicked(mouse) then
      scale[i] = (scale[i] + 1) % 12
      UpdateScaleTex(i)
    end
  end

--  Draw(p1Tex, 0.8,0.77,0.2,0.22) -- parameter 1

  -- draw pipes
  for pid, p in pairs(pipes) do
    local pipeY = p[2]+0.2
    Draw(pipe, (p[1]-beat)*speed+0.2, pipeY, 0.1, 1-pipeY)
    Draw(pipe, (p[1]-beat)*speed+0.2, 0, 0.1, pipeY-0.3)
  end

  -- draw instructions
  if beat == 0 or crashBeat > 0 then
    Draw(instruction, 0,0.4,1,0.1)
    Draw(bpm, 0,0.5,1,0.1)
  end

  -- draw bird
  local bird = 0
  if timeSinceFlap < 10 then
    bird = bird1
  elseif timeSinceFlap < 50 then
    bird = bird2
  else
    bird = bird3
  end
  Draw(bird, 0.1, y, 0.2, 0.2)
end