Skip to main content

Flappy Box Game With LOVE2D

 


After learning how to get started with LOVE2D and make Hello World with LOVE2D, I wanted to make a minimal but playable game. 


I went to the official LOVE2D website and went to see some tutorials.  I chose to try any one game from the Simple Game Tutorial website. I chose the Bird game from there. This is a simple version of the Flappy Bird game. Since there is a box instead of a bird, I am calling it Flappy Box. Yeah, I know it isn't flapping either. But cut the box some slack please. :D

I skimmed through the tutorial. It was pretty neat and well explained. Instead of giving the full code at once, it guides you through each step by adding/removing/updating over previous codes. 

And yes, I too went through all the steps thoroughly. I promise! ;) 

I got confused in the first few steps when it said to add new codes. It was actually replacing some parts in the function with new codes. So, watch out for that. 

Here's an example.

In the next step:

So you can see that it's the same function. So, instead of adding the code  given above completely into the file, you are expected to update the previous function love.draw().

 Here,  -- etc.  refers to the previous lines of codes in the same function. 

So, the final code will actually be the sum of two. 

function love.draw()

    -- code for drawing background
    love.graphics.setColor(.14, .36, .46)
    love.graphics.rectangle('fill', 0, 0, 300, 388)

    -- code for drawing the bird 
   love.graphics.setColor(.87, .84, .27)     love.graphics.rectangle('fill', 62, 200, 30, 25)
 end

If you aren't patient enough to go through the whole steps, you can just paste the following code into your main.lua file. And press CTRL+B.

To revise on use of Sublime:

  • Create a folder anywhere in your PC. The folder can have any name.
  • Open the folder from Sublime.
  •  Create a new file named main.lua. (Yes, the name has to be exactly main.lua)
  • Copy the following code into main.lua.
  • Press CTRL+B.
  • VoilĂ  !
Gad Elmaleh Huge In France GIF by NETFLIX - Find & Share on GIPHY


Next: Distribute your game in Windows

Final code  

(From the same website)
function love.load()
    playingAreaWidth = 300
    playingAreaHeight = 388

    birdX = 62
    birdWidth = 30
    birdHeight = 25
    
    pipeSpaceHeight = 100
    pipeWidth = 54

    function newPipeSpaceY()
        local pipeSpaceYMin = 54
        local pipeSpaceY = love.math.random(
            pipeSpaceYMin,
            playingAreaHeight - pipeSpaceHeight - pipeSpaceYMin
        )
        return pipeSpaceY
    end
    
    function reset()
        birdY = 200
        birdYSpeed = 0

        pipe1X = playingAreaWidth
        pipe1SpaceY = newPipeSpaceY()

        pipe2X = playingAreaWidth + ((playingAreaWidth + pipeWidth) / 2)
        pipe2SpaceY = newPipeSpaceY()

        score = 0

        upcomingPipe = 1
    end

    reset()
end

function love.update(dt)
    birdYSpeed = birdYSpeed + (516 * dt)
    birdY = birdY + (birdYSpeed * dt)
    
    local function movePipe(pipeX, pipeSpaceY)
        pipeX = pipeX - (60 * dt)

        if (pipeX + pipeWidth) < 0 then
            pipeX = playingAreaWidth
            pipeSpaceY = newPipeSpaceY()
        end
        
        return pipeX, pipeSpaceY
    end

    pipe1X, pipe1SpaceY = movePipe(pipe1X, pipe1SpaceY)
    pipe2X, pipe2SpaceY = movePipe(pipe2X, pipe2SpaceY)

    function isBirdCollidingWithPipe(pipeX, pipeSpaceY)
        return
        -- Left edge of bird is to the left of the right edge of pipe
        birdX < (pipeX + pipeWidth)
        and
         -- Right edge of bird is to the right of the left edge of pipe
        (birdX + birdWidth) > pipeX
        and (
            -- Top edge of bird is above the bottom edge of first pipe segment
            birdY < pipeSpaceY
            or
            -- Bottom edge of bird is below the top edge of second pipe segment
            (birdY + birdHeight) > (pipeSpaceY + pipeSpaceHeight)
        )
    end

    if isBirdCollidingWithPipe(pipe1X, pipe1SpaceY)
    or isBirdCollidingWithPipe(pipe2X, pipe2SpaceY)
    or birdY > playingAreaHeight then
        reset()
    end

    local function updateScoreAndClosestPipe(thisPipe, pipeX, otherPipe)
        if upcomingPipe == thisPipe
        and (birdX > (pipeX + pipeWidth)) then
            score = score + 1
            upcomingPipe = otherPipe
        end
    end

    updateScoreAndClosestPipe(1, pipe1X, 2)
    updateScoreAndClosestPipe(2, pipe2X, 1)
end

function love.draw()
    love.graphics.setColor(.14, .36, .46)
    love.graphics.rectangle('fill', 0, 0, playingAreaWidth, playingAreaHeight)

    love.graphics.setColor(.87, .84, .27)
    love.graphics.rectangle('fill', birdX, birdY, birdWidth, birdHeight)
    
    local function drawPipe(pipeX, pipeSpaceY)
        love.graphics.setColor(.37, .82, .28)
        love.graphics.rectangle(
            'fill',
            pipeX,
            0,
            pipeWidth,
            pipeSpaceY
        )
        love.graphics.rectangle(
            'fill',
            pipeX,
            pipeSpaceY + pipeSpaceHeight,
            pipeWidth,
            playingAreaHeight - pipeSpaceY - pipeSpaceHeight
        )
    end

    drawPipe(pipe1X, pipe1SpaceY)
    drawPipe(pipe2X, pipe2SpaceY)
    
    love.graphics.setColor(1, 1, 1)
    love.graphics.print(score, 15, 15)
end

function love.keypressed(key)
    if birdY > 0 then
        birdYSpeed = -165
    end
end

Next: Distribute your game in Windows

Comments

Popular posts from this blog

Bikharney Ka Mujhko Shauq Hai Bada | Qala | Guitar Chords and Lyrics

Full song here:  https://youtu.be/-lcd1ixHqjE?t=670  or here  https://youtu.be/1Hd7Gl_mLzw G                                             C Bikharney Ka Mujhko, Shauq Hai Bada G                D                        C  G  D Sameitega Mujhko, Tu Bata Zara... G                                                C Haaye, Bikharney Ka Mujhko, Shauq Hai Bada G                D                        C  G  D Sameitega Mujhko, Tu Bata Zara... G D G G                 C                                G Doob-ti Hai Tujh Mein, Aaj Meri Kashti G                    C                  G Guftagu Mein Utri Baat Ki Tarah G                   C                               G Ho, Dekh Ke Tujhey Hi, Raat Ki Hawa Ne G                         C            D               G Saans Thaam Li Hai Haath Ki Tarah, Haaye... G                                             C                Ki Aankhon Mein Teri Raat Ki Nadi G                         D Yeh Baazi To Haari Hai G      C        G D Sau Feesadi... G           

Timi Uta Ma Yeta (Sunsaan Raat Ma) Guitar Chords - Pratik Gurung Cover - Original by Milan Newar, Rajan Raj Shiwakoti

 I came across this song when Kanden Limbu sang this in Nepal Idol.  He was one of the contestants. I thought it was his original. Surprisingly, it is not. His was based on Pratik Gurung's cover. This is actually a full fledge song with a music video. Here's the original one. However, I like the cover version. Here are the chords for a portion of the song. The chords are based on this tutorial . In the tutorial, plucking is used. You can strum as well if that's what you prefer.  CAPO: 2nd fret STRUMMING: D-DU Plucking used: 5-2-3 (C and Am), 4-2-3 (FT and GT), 6-2-3 (G) Chords: C, Am, G Major, F Major Triad (FT), G Major Triad (GT) NOTE: It's easier if you play G Major in C's original position; just move 2 and 3 fingers one step up.   C                             Am (Sunsan raat ma yo mero aatma FT               GT    C Chasakka bijheko mayalu C                             Am Timlai nai samjhi ma ruda ruda FT       GT   C Sirani bhijheko ) x 2 C                  

Pokhiyera Ghaam Ko Jhulka Guitar Chords|| Narayan Gopal || Ambar Gurung || Hari Bhakta Katuwal

  Pokhiyera Ghaam Ko Jhulka Guitar Chords || Narayan Gopal  Composer: Ambar Gurung Lyrics: Haribhakta Katwal Vocals: Narayan Gopal Guitar Chords based on tutorial by Nepali E-Chords . I take no responsibility if the chords are not absolutely right. But it sounded good enough for me.  Chords used: G, C, D, Am, Em, Bm Strumming: D-DUDU Intro: D       G      D             G              D      G Pokhiyera Ghamko Jhulka, Bhari Sangharama G                     D                     Em                G Timro Jindagiko Dhoka, Kholu Kholu Lagchha Hai Am                     C           D Kholu Kholu Lagchha Hai D      G      D    G       D      G Sayapatri Fula Sitai, Fakri Aaganama G                  D          Em              G Batasko Bhaka Tipi, Bolu Bolu Lagchha Hai Am                C           D Bolu Bolu Lagchha Hai Verse:1 G                Bm                     G                         D (Kati