flutter 扑啦扑啦鸟跳特刊

bcs8qyzn  于 2023-01-02  发布在  Flutter
关注(0)|答案(1)|浏览(83)

I am trying to make a flappy bird project from there "https://www.youtube.com/watch?v=kM9gz7Q9rE4" but meanwhile jump part mine as not same as video and I didn't find solution.My bird stuck in the top left of my screen.It isn't doing any movement.
主要:

import 'package:flutter/material.dart';
import 'homepage.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: HomePage(),
    );
  }
}

主页:

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_flappybird/bird.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  //bird variables
  static double birdY = 0;
  double initialPos = birdY;
  double height = 0;
  double time = 0;
  double gravity = -4.9; // strong of gravity
  double velocity = 5; // strong of jump

  //game settings
  bool gameHasStarted = false;

  void startGame() {
    gameHasStarted = true;
    Timer.periodic(Duration(milliseconds: 50), (timer) {
      //real physic jump
      height = gravity * time * time + velocity * time;

      setState(() {
        birdY = initialPos - height;
      });

      if (birdY < -1 || birdY > 1) {
        timer.cancel();
      }

      time += 0.1; //time keep going
    });
  }

  void jump() {
    setState(() {
      time = 0;
      initialPos = birdY;
    });
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: gameHasStarted ? jump : startGame,
      child: Scaffold(
        body: Column(
          children: [
            Expanded(
              flex: 3,
              child: Container(
                color: Colors.lightBlue,
                child: Center(
                  child: Stack(
                    children: [
                      MyBird(
                        birdY: birdY,
                      ),
                      Container(
                        alignment: Alignment(0, -0.5),
                        child: Text(
                          ' T A P  T O  P L A Y',
                          style: TextStyle(color: Colors.white, fontSize: 30),
                        ),
                      )
                    ],
                  ),
                ),
              ),
            ),
            Expanded(
              child: Container(
                color: Colors.brown,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

鸟:

import 'package:flutter/material.dart';

class MyBird extends StatelessWidget {
  var birdY;

  MyBird({required this.birdY});

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment(0, birdY),
      height: 50,
      width: 50,
      child: Image.asset('lib/images/bird.png'),
    );
  }
}

我想让我的鸟跳起来但我做不到

u3r8eeie

u3r8eeie1#

高度计算不好。我用布尔值isJump编写它。每次用户点击,isJump = true。并影响下一次循环迭代的速度。

import 'dart:async';

import 'package:flutter/material.dart';
import 'bird.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  //bird variables
  static double birdY = 0;
  double initialPos = birdY;
  double height = 0;
  double time = 0;
  double gravity = 0.005; // strong of gravity
  double velocity = 0; // strong of jump
  double jumpForce = 0.6;
  bool gameHasStarted = false;
  bool isJump = false;

  void startGame() {
    gameHasStarted = true;
    Timer.periodic(const Duration(milliseconds: 10), (timer) {
      if (isJump) {
        velocity = -jumpForce;
      } else {
        velocity += gravity * time;
      }
      setState(() {
        height += velocity * time;
      });
      if (isJump) {
        time = 0.00;
        isJump = false;
      } else {
        time += 0.01;
      }
    });
  }

  void jump() {
    isJump = true;
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: gameHasStarted ? jump : startGame,
      child: Scaffold(
        body: Column(
          children: [
            Expanded(
              flex: 3,
              child: Container(
                color: Colors.lightBlue,
                child: Center(
                  child: Stack(
                    children: [
                      MyBird(
                        birdY: height,
                      ),
                      Container(
                        alignment: const Alignment(0, -0.5),
                        child: const Text(
                          ' T A P  T O  P L A Y',
                          style: TextStyle(color: Colors.white, fontSize: 30),
                        ),
                      )
                    ],
                  ),
                ),
              ),
            ),
            Expanded(
              child: Container(
                color: Colors.brown,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

相关问题