Flutter Flappy Bird Jump Issue



我正在尝试从那里做一个flappy bird项目"https://www.youtube.com/watch?v=kM9gz7Q9rE4"但同时,我的跳跃部分与视频不一样,我没有找到解决方案。我的小鸟卡在屏幕左上方了。它没有做任何动作。

主:

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'),
);
}
}

我想让我的鸟跳起来,但是我不能

高度计算不好。我用布尔值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,
),
),
],
),
),
);
}
}

相关内容

  • 没有找到相关文章

最新更新