## 🎮 Bikin Game Flappy Bird Sendiri di Flutter Dart dengan Zappy.run – Seru, Mudah, & Bikin Ketagihan! 🚀

 


https://zbhc06jjbhd0.zapp.page/#/

---

Siapa bilang bikin game itu ribet? 😎 Dengan **Flutter Dart** dan platform **Zappy.run**, aku berhasil membuat *Flappy Bird* versi aku sendiri yang bisa langsung dimainkan di browser! 🐦✨ Mulai dari mekanik terbang yang bikin deg-degan, rintangan pipa yang bikin gemas 😆, sampai skor yang terus bertambah setiap kali burung melewati pipa — semua ini bisa dibuat hanya dengan beberapa baris kode.


Prosesnya nggak cuma seru, tapi juga bikin otak kita terlatih untuk berpikir logis, memecahkan masalah, dan mengasah kreativitas. 💡 Yang lebih keren lagi, game ini bisa dimainkan cukup dengan menekan tombol **SPASI** di keyboard atau klik layar untuk membuat burung terbang melewati pipa-pipa penghalang.


Kalau kamu suka belajar sambil bikin sesuatu yang langsung bisa dimainkan, **Flutter + Zappy.run** adalah kombinasi yang pas. Cocok buat pemula maupun yang sudah terbiasa ngoding. Dan siapa tahu, game buatanmu nanti bisa viral dan dimainkan banyak orang di seluruh dunia! 🌍✨


---


## 📜 Kode Program Flappy Bird di Flutter Dart


Nah, ini dia kode lengkapnya. Kamu bisa langsung copy dan paste ke **Zappy.run** atau project Flutter kamu, lalu jalankan untuk mencoba.


```dart

import 'dart:async';

import 'dart:math';

import 'package:flutter/material.dart';

import 'package:flutter/services.dart';


void main() {

  runApp(const FlappyBirdGame());

}


class FlappyBirdGame extends StatelessWidget {

  const FlappyBirdGame({super.key});


  @override

  Widget build(BuildContext context) {

    return const MaterialApp(

      home: GamePage(),

      debugShowCheckedModeBanner: false,

    );

  }

}


class GamePage extends StatefulWidget {

  const GamePage({super.key});


  @override

  State<GamePage> createState() => _GamePageState();

}


class _GamePageState extends State<GamePage> {

  double birdY = 0;

  double birdVelocity = 0;

  final double gravity = -4.9;

  final double jumpStrength = 1.2;

  final double timeStep = 0.04;


  double obstacleX = 1.5;

  double gapY = 0;

  final double gapSize = 0.3;


  bool isPlaying = false;

  int score = 0;


  Timer? gameTimer;


  @override

  void initState() {

    super.initState();

    RawKeyboard.instance.addListener(_handleKeyPress);

  }


  @override

  void dispose() {

    gameTimer?.cancel();

    RawKeyboard.instance.removeListener(_handleKeyPress);

    super.dispose();

  }


  void _handleKeyPress(RawKeyEvent event) {

    if (event is RawKeyDownEvent && event.logicalKey == LogicalKeyboardKey.space) {

      if (!isPlaying) {

        startGame();

      }

      jump();

    }

  }


  void startGame() {

    isPlaying = true;

    score = 0;

    birdY = 0;

    birdVelocity = 0;

    obstacleX = 1.5;

    gapY = Random().nextDouble() * 0.6 - 0.3;

    gameTimer = Timer.periodic(Duration(milliseconds: (timeStep * 1000).toInt()), (timer) {

      updateGame();

    });

  }


  void updateGame() {

    birdVelocity += gravity * timeStep;

    birdY += birdVelocity * timeStep;


    obstacleX -= 1.2 * timeStep;


    if (obstacleX < -0.5) {

      obstacleX = 1.5;

      gapY = Random().nextDouble() * 0.6 - 0.3;

      score++;

    }


    if (birdY < -1 || birdY > 1) {

      endGame();

    }

    if (obstacleX < 0.1 && obstacleX > -0.1) {

      if (birdY < gapY - gapSize / 2 || birdY > gapY + gapSize / 2) {

        endGame();

      }

    }


    setState(() {});

  }


  void jump() {

    birdVelocity = jumpStrength;

  }


  void endGame() {

    isPlaying = false;

    gameTimer?.cancel();

    setState(() {});

  }


  @override

  Widget build(BuildContext context) {

    return Scaffold(

      backgroundColor: Colors.lightBlue,

      body: GestureDetector(

        onTap: jump,

        child: Stack(

          children: [

            Center(

              child: AspectRatio(

                aspectRatio: 3 / 4,

                child: Container(

                  color: Colors.lightBlue[200],

                  child: CustomPaint(

                    painter: GamePainter(

                      birdY: birdY,

                      obstacleX: obstacleX,

                      gapY: gapY,

                      gapSize: gapSize,

                    ),

                  ),

                ),

              ),

            ),

            Positioned(

              top: 50,

              left: 0,

              right: 0,

              child: Text(

                isPlaying ? "$score" : "Press SPACE to start",

                textAlign: TextAlign.center,

                style: const TextStyle(

                  fontSize: 32,

                  fontWeight: FontWeight.bold,

                  color: Colors.white,

                  shadows: [Shadow(blurRadius: 4, color: Colors.black, offset: Offset(2, 2))],

                ),

              ),

            )

          ],

        ),

      ),

    );

  }

}


class GamePainter extends CustomPainter {

  final double birdY;

  final double obstacleX;

  final double gapY;

  final double gapSize;


  GamePainter({

    required this.birdY,

    required this.obstacleX,

    required this.gapY,

    required this.gapSize,

  });


  @override

  void paint(Canvas canvas, Size size) {

    final paint = Paint();


    paint.color = Colors.yellow;

    double birdSize = size.width * 0.05;

    canvas.drawCircle(

      Offset(size.width / 2, size.height / 2 - birdY * size.height / 2),

      birdSize,

      paint,

    );


    paint.color = Colors.green;

    double obstacleWidth = size.width * 0.1;


    double topHeight = (0.5 - gapY - gapSize / 2) * size.height;

    canvas.drawRect(

      Rect.fromLTWH(

        (obstacleX + 1) / 2 * size.width - obstacleWidth / 2,

        0,

        obstacleWidth,

        topHeight,

      ),

      paint,

    );


    double bottomY = (0.5 - gapY + gapSize / 2) * size.height;

    canvas.drawRect(

      Rect.fromLTWH(

        (obstacleX + 1) / 2 * size.width - obstacleWidth / 2,

        bottomY,

        obstacleWidth,

        size.height - bottomY,

      ),

      paint,

    );

  }


  @override

  bool shouldRepaint(covariant CustomPainter oldDelegate) => true;

}

```


---


💬 **Tips:**


* Gunakan tombol **SPASI** untuk bermain di PC atau tap layar untuk bermain di HP.

* Kalau mau modifikasi, kamu bisa ubah warna burung, ukuran pipa, atau tingkat kesulitan dengan mengatur nilai `gravity` dan `gapSize`.


Kalau mau, aku bisa tambahkan **gambar preview** dari game ini supaya artikel Blogspot-mu terlihat lebih menarik dan interaktif.

Comments

Popular posts from this blog

Mengenal Aplikasi Mobile Absensi Modern