summaryrefslogtreecommitdiff
path: root/lib/luavgl/examples/flappyBird/flappyBird.lua
blob: 26018bba500cd6badd9a2476a8de7486f51a3520 (plain)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
local lvgl = require("lvgl")

local MOVE_SPEED = 480 / 8000 -- 8s for 480 pixel, pixel per ms
local PIXEL_PER_METER = 80
local TOP_Y = 20
local BOTTOM_Y = 480 - 112
local PIPE_COUNT = 5
local PIPE_GAP = 100
local PIPE_SPACE = 120

-- SCRIPT_PATH is set in simulator/main.c, used to get the abs path of first
-- lua script lua get called. In this example, SCRIPT_PATH is set to
-- path of `examples.lua`, flappyBird.lua is called when button is clicked.

local IMAGE_PATH = SCRIPT_PATH
if not IMAGE_PATH then
    IMAGE_PATH = "/"
    print("Note image root path is set to: ", IMAGE_PATH)
end

IMAGE_PATH = IMAGE_PATH .. "/flappyBird/"
print("IMAGE_PATH:", IMAGE_PATH)

local function randomY()
    return math.random(TOP_Y + 30, BOTTOM_Y - 50 - 50)
end

local function screenCreate(parent)
    local property = {
        w = 480,
        h = 480,
        bg_opa = 0,
        border_width = 0,
        pad_all = 0
    }

    local scr
    if parent then
        scr = parent:Object{
            w = 480,
            h = 480,
            bg_opa = 0,
            border_width = 0,
            pad_all = 0
        }
    else
        scr = lvgl.Object(nil, property)
    end
    scr:clear_flag(lvgl.FLAG.SCROLLABLE)
    scr:clear_flag(lvgl.FLAG.CLICKABLE)
    return scr
end

local function Image(parent, src)
    local img = {}
    img.widget = parent:Image{
        src = src
    }

    img.w, img.h = img.widget:get_img_size()
    if not img.w or not img.h then
        error("failed to load image: " .. src)
    end
    return img
end

local function ImageScroll(root, src, animSpeed, y)
    -- image on right
    local right = Image(root, src).widget
    right:set{
        src = src,
        x = 480,
        y = y,
        pad_all = 0
    }

    local img = Image(root, src).widget
    img:set{
        x = 0,
        y = y,
        src = src,
        pad_all = 0
    }

    img:Anim{
        run = true,
        start_value = 0,
        end_value = -480,
        time = 480 / animSpeed,
        repeat_count = lvgl.ANIM_REPEAT_INFINITE,
        path = "linear",
        exec_cb = function(obj, value)
            img:set{
                x = value
            }

            right:set{
                x = value + 480
            }
        end
    }

    img:clear_flag(lvgl.FLAG.CLICKABLE)

    return img
end

local function Frames(parent, src, fps)
    local frame = Image(parent, src[1])
    fps = fps ~= 0 and fps or 25

    frame.src = src
    frame.len = #src
    frame.i = 0

    frame.timer = lvgl.Timer {
        period = 1000 / fps,
        cb = function(t)
            frame.widget:set{
                src = frame.src[frame.i]
            }

            frame.i = frame.i + 1
            if frame.i == frame.len then
                frame.i = 1
            end
        end
    }

    frame.start = function(self)
        self.timer:resume()
    end

    frame.pause = function(self)
        self.timer:pause()
    end

    return frame
end

local function Pipe(parent)
    local up = Image(parent, IMAGE_PATH .. "pipe_up.png")
    local down = Image(parent, IMAGE_PATH .. "pipe_down.png")
    local pipe = {
        up = up.widget,
        down = down.widget,
        w = up.w,
        h = up.h,
        x = 0,
        y = 0
    }

    function pipe:updatePipePos()
        self.up:set{
            x = self.x,
            y = self.y - up.h
        }

        self.down:set{
            x = self.x,
            y = self.y + PIPE_GAP
        }
    end

    pipe:updatePipePos()
    return pipe
end

local function ObjInfo(x, y, w, h)
    return {
        x = x,
        y = y,
        w = w,
        h = h
    }
end

local function Pipes(parent)
    local pipes = {}

    -- add initial pipe
    for i = 1, PIPE_COUNT do
        pipes[i] = Pipe(parent)
        if i == 1 then
            pipes.w = pipes[i].w -- record pipe size
            pipes.h = pipes[i].h
        end
    end

    local function pipesPosinit()
        local x = 480;
        local y = randomY()

        for i = 1, PIPE_COUNT do
            local pipe = pipes[i]
            pipe.x = x
            pipe.y = y
            pipe:updatePipePos()
            pipes[i] = pipe
            x = x + PIPE_SPACE + pipe.w
            y = randomY()
        end
    end

    pipesPosinit()

    pipes.score = 0
    pipes.last = PIPE_COUNT -- first pipe index in pipes.pipes
    pipes.totalWidth = (PIPE_COUNT) * (PIPE_SPACE + pipes.w)
    pipes.birdInfo = ObjInfo(0, 0, 0, 0)
    pipes.gapInfo = ObjInfo(0, 0, 0, 0)

    function pipes:setObjInfo(x, y, w, h)
        self.birdInfo.x = x
        self.birdInfo.y = y
        if w then
            self.birdInfo.w = w
        end
        if h then
            self.birdInfo.h = h
        end
    end

    local function setGapInfo(x, y, w, h)
        pipes.gapInfo.x = x
        pipes.gapInfo.y = y
        pipes.gapInfo.w = w
        pipes.gapInfo.h = h
    end

    pipes.objPassing = -1

    local function isBirdCollision()
        local bird = pipes.birdInfo
        local gap = pipes.gapInfo

        -- far left
        if bird.x + bird.w < gap.x then
            return false
        end

        -- far right
        if bird.x > gap.x + gap.w then
            return false
        end

        -- in middle

        if (bird.y > gap.y) and (bird.y + bird.h < gap.y + gap.h) then
            return false
        end
        return true
    end

    local function moveVirtualX(dx)
        for i = 1, PIPE_COUNT do
            local pipe = pipes[i]
            local newX = pipe.x + dx

            if newX + pipes.w < 0 then
                newX = newX + pipes.totalWidth
                pipe.y = randomY()
                pipes.last = i
            end

            pipe.x = newX
            pipe.updatePipePos(pipe)
        end
    end

    local function checkScore(i)
        local pipe = pipes[i]

        local bird = pipes.birdInfo
        local gap = pipes.gapInfo
        local passing = pipes.objPassing

        -- far left or right
        if bird.x + bird.w < gap.x or bird.x > gap.x + gap.w then
            if passing > 0 and i == passing then
                pipes.score = pipes.score + 1
                passing = -1
                pipes.scoreUpdateCB(pipes.score)
            end
        else
            if passing < 0 then
                passing = i
            end
        end

        pipes.objPassing = passing
    end

    --- Detect if obj has collision with pipes
    local function collisionDetect()
        local first = (pipes.last % PIPE_COUNT) + 1
        for idx = 0, PIPE_COUNT - 1 do
            local i = (first + idx - 1) % PIPE_COUNT + 1
            local pipe = pipes[i]
            setGapInfo(pipe.x, pipe.y, pipe.w, PIPE_GAP)

            if isBirdCollision() then
                local bird = pipes.birdInfo
                if pipes.collisionCB then
                    pipes.collisionCB()
                end
            end

            checkScore(i)
        end
    end

    pipes.preValue = 0
    pipes.anim = pipes[1].up:Anim{
        run = false,
        start_value = 0,
        end_value = 480,
        time = 480 / MOVE_SPEED, -- MOVE_SPEED
        repeat_count = lvgl.ANIM_REPEAT_INFINITE,
        path = "linear",
        exec_cb = function(obj, value)
            local x = pipes.preValue
            local d
            if value < x then
                d = value + 480 - x
            else
                d = value - x
            end
            pipes.preValue = value
            moveVirtualX(-d)
            collisionDetect()
        end
    }

    function pipes:start()
        self.anim:start()
    end

    function pipes:stop()
        self.anim:stop()
    end

    function pipes:reset()
        pipesPosinit()
        pipes.score = 0
        pipes.preValue = 0
        pipes.objPassing = -1
    end

    function pipes:setCollisionCB(collisionCB)
        self.collisionCB = collisionCB
    end

    function pipes:setScoreUpdateCB(cb)
        self.scoreUpdateCB = cb
    end

    return pipes
end

local function Bird(parent, birdMovedCB)
    -- create bird Frame(sprite) in 5FPS
    local bird = Frames(parent,
        {IMAGE_PATH .. "bird1.png", IMAGE_PATH .. "bird2.png", IMAGE_PATH .. "bird3.png"}, 5)

    local function birdVarInit()
        bird.x = 240 - bird.w / 2
        bird.y = 240 - bird.h / 2
        bird.widget:set{
            x = bird.x,
            y = bird.y
        }

        bird.head = 0
        bird.force = 0 -- in unit of m/s^2 rather than N
        bird.velocity = 0 -- vertical verlocity
        bird.time = 0 -- time stamp when it updates
        bird.moving = false
    end

    birdVarInit()

    bird.setY = function(self)
        bird.widget:set{
            y = bird.y
        }
    end

    bird.setHead = function(self)
        bird.widget:set{
            angle = self.head
        }
    end

    bird.applyForce = function(self, force)
        self.force = force
        if bird.moving then
            return
        end

        bird.moving = true
        self.y_anim:start()
    end

    bird.pressed = function(self)
        bird:applyForce(-13)
        bird.velocity = 0
    end

    bird.released = function(self)
        bird:applyForce(9.8)
        bird.velocity = 0
    end

    local function velocity2HeadAngle(v)
        -- -9.8 ~ 9.8:90 ~ -90
        return v * 60
    end

    -- y moving anim, in time.
    bird.y_anim = bird.widget:Anim{
        run = false,
        start_value = 0,
        end_value = 1000,
        time = 1000, -- 1000 ms
        repeat_count = lvgl.ANIM_REPEAT_INFINITE,
        path = "linear",
        exec_cb = function(obj, tNow)
            -- we use anim to get current time, can calculate position based on force/velocity
            if tNow < bird.time then
                tNow = tNow + 1000
            end
            local y = bird.y
            local preT = bird.time
            local v = bird.velocity
            local t = tNow < preT and tNow + 1000 - preT or tNow - preT
            t = t * 0.001 -- ms to s

            v = bird.force * t + v
            if v > 10 then
                v = 10
            end

            if v < -10 then
                v = -10
            end

            y = y + v * t * PIXEL_PER_METER
            if y > BOTTOM_Y - 30 then
                y = BOTTOM_Y - 30
                v = 0
            end
            if y < TOP_Y then
                y = TOP_Y
                v = 0
            end

            bird.y = y
            bird.time = tNow
            bird.velocity = v
            bird.head = velocity2HeadAngle(v)

            birdMovedCB(bird.x, bird.y)
            -- set y
            bird:setY()
            bird:setHead()
        end
    }

    function bird:stop()
        bird.y_anim:stop()
    end

    function bird:gameOver()
        -- like it's released forever
        bird.released()
    end

    function bird:start()
        bird.y_anim:start()
    end

    function bird:reset()
        bird.stop()
        birdVarInit()
    end

    return bird;
end

local function Background(root, bgEventCB)
    local bgLayer = screenCreate(root) -- background layer
    bgLayer:add_flag(lvgl.FLAG.CLICKABLE) --  we accept event here

    local bg = ImageScroll(bgLayer, IMAGE_PATH .. "bg_day.png", MOVE_SPEED * 0.4, 0)
    local pipes = Pipes(bgLayer)
    local land = ImageScroll(bgLayer, IMAGE_PATH .. "land.png", MOVE_SPEED, BOTTOM_Y)

    bgLayer:onevent(lvgl.EVENT.PRESSED, function(obj, code)
        bgEventCB(lvgl.EVENT.PRESSED)
    end)

    bgLayer:onevent(lvgl.EVENT.RELEASED, function(obj, code)
        bgEventCB(lvgl.EVENT.RELEASED)
    end)

    return {
        pipes = pipes
    }
end

local function SysLayer(root)
    local sysLayer = screenCreate(root) -- upper layer
    return sysLayer
end

local function createPlayBtn(sysLayer, onEvent)
    local playBtn = Image(sysLayer, IMAGE_PATH .. "button_play.png").widget
    playBtn:add_flag(lvgl.FLAG.CLICKABLE)
    playBtn:set{
        align = {
            type = lvgl.ALIGN.CENTER,
            y_ofs = 80
        }
    }

    playBtn:onevent(lvgl.EVENT.PRESSED, onEvent)

    return playBtn
end

local function entry()
    local scr = screenCreate()
    local bgLayer
    local mainLayer
    local sysLayer
    local bird
    local pipes
    local bgEventCB -- background layer pressed/released event
    local birdMovedCB -- callback when bird position updates
    local collisionCB -- callback when collision happends
    local flagRunning = false
    local gameStart -- API to start game
    local gameOver -- API to stop game
    local scoreLabel
    local scoreBest = 0
    local scoreNow = 0
    local debouncing = false
    -- global event process

    local scoreUpdateCB = function(score)
        scoreLabel:set{
            text = string.format("%03d", score)
        }
        scoreNow = score
    end

    print("font:", lvgl.BUILTIN_FONT.MONTSERRAT_26)
    gameStart = function()
        if flagRunning then
            return
        end

        bird:reset()
        pipes:reset()
        pipes:start()
        bird:start()
        flagRunning = true
        scoreNow = 0
        if scoreLabel then
            scoreLabel:set{
                text = string.format("%03d", 0)
            }
        end
    end

    gameOver = function()
        if not flagRunning then
            return
        end

        debouncing = true
        flagRunning = false

        pipes:stop()
        bird:gameOver()
        if scoreNow > scoreBest then
            scoreBest = scoreNow
        end

        local gameoverImg = Image(sysLayer, IMAGE_PATH .. "text_game_over.png").widget
        gameoverImg:set{
            align = {
                type = lvgl.ALIGN.TOP_MID,
                y_ofs = 100
            }
        }

        gameoverImg:Anim{
            run = true,
            start_value = 0,
            end_value = 3600,
            time = 2000,
            repeat_count = 2,
            path = "bounce",
            exec_cb = function(obj, value)
                obj:set{
                    angle = value
                }
            end
        }

        local scoreImg = Image(sysLayer, IMAGE_PATH .. "score.png").widget
        scoreImg:set{
            align = {
                type = lvgl.ALIGN.CENTER,
                y_ofs = -20,
                x_ofs = 0
            }
        }
        scoreImg:Anim{
            run = true,
            start_value = 480,
            end_value = 0,
            time = 1000,
            repeat_count = 1,
            path = "ease_in",
            exec_cb = function(obj, value)
                obj:set{
                    align = {
                        type = lvgl.ALIGN.CENTER,
                        x_ofs = value,
                        y_ofs = -20
                    }
                }
            end
        }

        local scoreResultLabel = scoreImg:Label{
            text = string.format("%03d", scoreNow),
            text_font = lvgl.BUILTIN_FONT.MONTSERRAT_22,
            align = {
                type = lvgl.ALIGN.TOP_MID,
                x_ofs = 0,
                y_ofs = 25
            }
        }

        local scoreBestLabel = scoreImg:Label{
            text = string.format("%03d", scoreBest),
            text_font = lvgl.BUILTIN_FONT.MONTSERRAT_22,
            align = {
                type = lvgl.ALIGN.BOTTOM_MID,
                x_ofs = 0,
                y_ofs = -5
            }
        }
        scoreNow = 0

        local playBtn;
        playBtn = createPlayBtn(sysLayer, function(obj, code)
            if debouncing then
                return
            end

            gameStart()
            playBtn:delete()
            playBtn = nil
            gameoverImg:delete()
            gameoverImg = nil
            scoreImg:delete()
            scoreImg = nil
        end)

        lvgl.Timer {
            period = 1000,
            cb = function(t)
                t:delete()
                debouncing = false
            end
        }
    end

    bgEventCB = function(event)
        if not flagRunning then
            return
        end

        if event == lvgl.EVENT.PRESSED then
            bird:pressed()
        else
            bird:released()
        end
    end

    local birdMovedCB = function(x, y)
        pipes:setObjInfo(bird.x, bird.y) -- set intial bird position.
    end

    local collisionCB = function()
        print("bird collision, stop game")
        -- call later
        lvgl.Timer {
            period = 10,
            cb = function(t)
                t:delete()
                gameOver()
            end
        }

    end

    -- background layer, including sky, then pipes and land above
    bgLayer = Background(scr, bgEventCB) -- background layer
    pipes = bgLayer.pipes -- get pipes from bg layer for set bird info etc.
    pipes:setCollisionCB(collisionCB)
    pipes:setScoreUpdateCB(scoreUpdateCB)

    -- main layer, the bird
    mainLayer = screenCreate(scr) -- main layer
    bird = Bird(mainLayer, birdMovedCB)
    pipes:setObjInfo(bird.x, bird.y, bird.w, bird.h)
    -- system layer, score etc.
    sysLayer = SysLayer(scr)

    local title = Image(sysLayer, IMAGE_PATH .. "title.png").widget
    title:set{
        align = {
            type = lvgl.ALIGN.TOP_MID,
            y_ofs = 80
        }
    }

    local playBtn;
    playBtn = createPlayBtn(sysLayer, function()
        print("pressed")
        gameStart()
        playBtn:delete()
        playBtn = nil
        title:delete()
        title = nil

        local medal = Image(sysLayer, IMAGE_PATH .. "medals.png").widget
        medal:set{
            align = {
                type = lvgl.ALIGN.TOP_MID,
                y_ofs = 10,
                x_ofs = -50
            }
        }
        scoreLabel = sysLayer:Label{
            text = " 000",
            text_font = lvgl.BUILTIN_FONT.MONTSERRAT_28,
            align = {
                type = lvgl.ALIGN.TOP_MID,
                x_ofs = 10,
                y_ofs = 20
            }
        }
    end)
end

entry()

-- bird = Bird(, nil)