-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 - update barrier 2 - create bird 3 - make the movement smoother 4 - reorganize code
- Loading branch information
Showing
8 changed files
with
196 additions
and
159 deletions.
There are no files selected for viewing
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// ignore_for_file: sized_box_for_whitespace, prefer_const_constructors | ||
|
||
import 'package:flutter/material.dart'; | ||
|
||
class Bird extends StatefulWidget { | ||
double yAxis; | ||
Bird(this.yAxis, {Key? key}) : super(key: key); | ||
|
||
@override | ||
State<Bird> createState() => _BirdState(); | ||
} | ||
|
||
class _BirdState extends State<Bird> { | ||
@override | ||
Widget build(BuildContext context) { | ||
Size size = MediaQuery.of(context).size; | ||
return AnimatedContainer( | ||
alignment: Alignment(0,widget.yAxis), | ||
duration: Duration(milliseconds: 0), | ||
child: Image.asset("assets/pics/bird.png",width: size.width * 0.4,height: size.height * 0.2,),); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables, sized_box_for_whitespace, avoid_unnecessary_containers, empty_statements, unused_field | ||
import 'dart:async'; | ||
import 'package:flappy_bird/Ui/Bird.dart'; | ||
import 'package:flappy_bird/Ui/Score.dart'; | ||
import 'package:flappy_bird/Ui/barrier.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
class HomePage extends StatefulWidget { | ||
const HomePage({Key? key}) : super(key: key); | ||
|
||
@override | ||
State<HomePage> createState() => _HomePageState(); | ||
} | ||
|
||
class _HomePageState extends State<HomePage> { | ||
int score = 0; | ||
// Bird Variables | ||
static double yAxis = 0; | ||
static double xAxis = 0; | ||
double time = 0; | ||
double height = 0; | ||
double gravity = -3.9; // How strong the Gravity | ||
double velocity = 2.5; // How strong the jump | ||
double initialHeight = yAxis; | ||
bool gameHasStarted = false; | ||
// Barrier Variables | ||
static List<double> barrierX = [2, 3.4]; | ||
List<List<double>> barrierY = [ | ||
// TODO: list of Lists to make different height for the barrier [topHeight,bottomHeight] | ||
[0.6, 0.4], | ||
[0.4, 0.6], | ||
]; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return GestureDetector( | ||
onTap: gameHasStarted? jump: startGame, | ||
child: Scaffold( | ||
body: Column( | ||
children: [ | ||
Expanded( | ||
flex: 3, | ||
child: Container( | ||
decoration: BoxDecoration(image: DecorationImage(image: AssetImage("assets/pics/background-day.png"),fit: BoxFit.cover)), | ||
child: Stack( | ||
children: [ | ||
Bird(yAxis), | ||
// Tap to play text | ||
Container( | ||
alignment: Alignment(0, -0.3), | ||
child: Text( gameHasStarted?'': 'TAP TO START',style: TextStyle(color: Colors.white,fontSize: 25),), | ||
), | ||
Barrier(barrierY[0][0], barrierX[0], true), | ||
Barrier(barrierY[0][1], barrierX[0], false), | ||
Barrier(barrierY[1][0], barrierX[1], true), | ||
Barrier(barrierY[1][1], barrierX[1], false), | ||
], | ||
), | ||
), | ||
), | ||
Expanded( | ||
flex: 1, | ||
child: Score(score),), | ||
]), | ||
), | ||
); | ||
} | ||
|
||
// Jump Function: | ||
void jump() { | ||
setState((){ | ||
time = 0; | ||
initialHeight = yAxis; | ||
}); | ||
} | ||
|
||
//Start Game Function: | ||
void startGame(){ | ||
gameHasStarted = true; | ||
Timer.periodic(Duration(milliseconds: 35), (timer) { | ||
height = gravity * time * time + velocity * time; | ||
setState((){ | ||
yAxis = initialHeight - height; | ||
}); | ||
setState(() { | ||
if(barrierX[0] < -1.9){ | ||
barrierX[0] += 3.5; | ||
}else{ | ||
barrierX[0] -= 0.05; | ||
} | ||
}); | ||
setState(() { | ||
if(barrierX[1] < -1.9){ | ||
barrierX[1] += 3.5; | ||
}else{ | ||
barrierX[1] -= 0.05; | ||
} | ||
}); | ||
if(birdIsDead()){ | ||
timer.cancel(); | ||
_showDialog(); | ||
}; | ||
time += 0.04 ; | ||
}); | ||
Timer.periodic(Duration(seconds: 2), (timer) { | ||
if(birdIsDead()){ | ||
timer.cancel(); | ||
score = 0; | ||
}else{ | ||
setState(() { | ||
score ++; | ||
}); | ||
} | ||
}); | ||
} | ||
/* | ||
* TODO: Make sure the bird doesn't go out screen | ||
* | ||
* TODO: Check if the bird hit the barrier | ||
*/ | ||
bool birdIsDead(){ | ||
// Screen | ||
if(yAxis > 1.26 || yAxis < -1.1){ | ||
return true ; | ||
} | ||
// Barrier | ||
for(int i = 0; i < barrierX.length; i++){ | ||
|
||
} | ||
return false; | ||
} | ||
|
||
void resetGame() { | ||
Navigator.pop(context); // dismisses the alert dialog | ||
setState(() { | ||
yAxis = 0; | ||
gameHasStarted = false; | ||
time = 0; | ||
score = 0; | ||
initialHeight = yAxis; | ||
barrierX[0] = 2; | ||
barrierX[1] = 3.4; | ||
}); | ||
} | ||
|
||
// TODO: Alert Dialog with 2 options (try again, exit) | ||
void _showDialog(){ | ||
showDialog(context: context, builder: (context) { | ||
return AlertDialog( | ||
backgroundColor: Colors.white,shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(18)), | ||
title: Text("..Oops",style: TextStyle(color: Colors.blue[900],fontSize: 25),), | ||
actions: [ | ||
Container( | ||
margin: EdgeInsets.all(10), | ||
child: GestureDetector(child: Text("try again",style: TextStyle(color: Colors.blue[900],fontSize: 17)),onTap: () => resetGame(),)), | ||
], | ||
); | ||
},); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.