flutter로 간단한 flappy bird 구현(1)

min_chan·2022년 11월 22일
0

flappy bird

목록 보기
1/1

플래피 버드 게임


  • 변수 설정

homepage.dart

static double birdY = 0;
double initialPos = birdY;
double height = 0;
double time = 0;
double gravity = -4.9; // how strong the gravity is
double velocity = 3.5; // how strong the jump is
double birdWidth = 0.1; // out of 2, 2 being the entire width of the screen
double birdHeight = 0.1; /

  • 방벽 구현

    barrier.dart

    class MyBarrier extends StatelessWidget {
    final size;
    
    MyBarrier({this.size});
    
    @override
    Widget build(BuildContext context) {
    return Container(
        width: 100,
        height: size,
        decoration: BoxDecoration(
          color: Colors.green,
          border: Border.all(width: 10, color: Colors.greenAccent),
          borderRadius: BorderRadius.circular(15)
        ));
    
    }

    }


  • 방벽을 4개로 설정

homepage.dart

AnimatedContainer(
                    duration: Duration(milliseconds: 0),
                    alignment: Alignment(barrierXone, 1.1),
                    child: MyBarrier(
                      size: 200.0,
                    ),
                  ),
                  AnimatedContainer(
                    duration: Duration(milliseconds: 0),
                    alignment: Alignment(barrierXone, -1.1),
                    child: MyBarrier(
                      size: 200.0,
                    ),
                  ),
                  AnimatedContainer(
                    duration: Duration(milliseconds: 0),
                    alignment: Alignment(barrierXtwo, 1.1),
                    child: MyBarrier(
                      size: 150.0,
                    ),
                  ),
                  AnimatedContainer(
                    duration: Duration(milliseconds: 0),
                    alignment: Alignment(barrierXtwo, -1.1),
                    child: MyBarrier(
                      size: 250.0,
                    ),
                  ),
                  

  • 새가 부딪혔을 경우

homepage.dart

bool birdIsDead() {
if (birdY < -1 || birdY > 1) {
  return true;
}

  • 새가 죽었을 경우

homepage.dart

  // check if bird is dead
  if (birdIsDead()) {
    timer.cancel();
    gameHasStarted = false;
    _showDialog();
  }
  
  

사진


  • 내용 정리

중력과 가속도를 설정해주어 새가 정교하게 움직일 수 있게하고, Y축으로 움직이는새가 -1,1에 만났을 경우 게임이 종료된다. 방벽은 4개로 구현하고, X축을 barrierXone으로 설정해주어 방벽이 움직이게 한다.


  • 실패한 부분

  1. 스코어 기록
  2. 방벽 충돌 기능

0개의 댓글

관련 채용 정보