Skip to content

Commit

Permalink
update Ui
Browse files Browse the repository at this point in the history
1 - update barrier
2 - create bird
3 - make the movement smoother
4 - reorganize code
  • Loading branch information
wvzv committed Aug 21, 2022
1 parent 96bf888 commit 2c2cc9e
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 159 deletions.
Empty file removed lib/Bird.dart
Empty file.
147 changes: 0 additions & 147 deletions lib/HomePage.dart

This file was deleted.

22 changes: 22 additions & 0 deletions lib/Ui/Bird.dart
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,),);
}
}
160 changes: 160 additions & 0 deletions lib/Ui/HomePage.dart
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(),)),
],
);
},);
}
}
7 changes: 4 additions & 3 deletions lib/Score.dart → lib/Ui/Score.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// ignore_for_file: prefer_const_literals_to_create_immutables, prefer_const_constructors
// ignore_for_file: prefer_const_literals_to_create_immutables, prefer_const_constructors, prefer_const_constructors_in_immutables

import 'package:flutter/material.dart';

class Score extends StatefulWidget {
const Score({Key? key}) : super(key: key);
final int score;
Score(this.score, {Key? key}) : super(key: key);

@override
State<Score> createState() => _ScoreState();
Expand All @@ -21,7 +22,7 @@ class _ScoreState extends State<Score> {
children: [
Text("Score",style: TextStyle(color: Colors.white,fontSize: 30),),
SizedBox(height: 10,),
Text("0",style: TextStyle(color: Colors.white,fontSize: 30)),
Text(widget.score.toString(),style: TextStyle(color: Colors.white,fontSize: 30)),
],),
// Best TEXT
Column(
Expand Down
15 changes: 8 additions & 7 deletions lib/barrier.dart → lib/Ui/barrier.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// ignore_for_file: prefer_const_constructors
// ignore_for_file: prefer_const_constructors, prefer_const_constructors_in_immutables

import 'package:flutter/material.dart';

class Barrier extends StatefulWidget {
double height;
double Axis;
double direction;
Barrier(this.height, this.Axis, this.direction, {Key? key}) : super(key: key);
final double height;
final bool isTop;
final double direction;
Barrier(this.height, this.direction, this.isTop, {Key? key}) : super(key: key);

@override
State<Barrier> createState() => _BarrierState();
Expand All @@ -15,11 +15,12 @@ class Barrier extends StatefulWidget {
class _BarrierState extends State<Barrier> {
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return AnimatedContainer(
alignment: Alignment(widget.Axis,widget.direction),
alignment: Alignment(widget.direction,widget.isTop ? 1.1 : -1.1),
duration: Duration(milliseconds: 0),
child: Container(
height: widget.height,
height: widget.height * size.height - 170,
width: 70,
decoration: BoxDecoration(
color: Colors.green[800],
Expand Down
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import 'package:flutter/material.dart';

import 'HomePage.dart';
import 'Ui/HomePage.dart';

void main() {
runApp(MaterialApp(
Expand Down
Loading

0 comments on commit 2c2cc9e

Please sign in to comment.