11import 'dart:async' ;
2-
32import 'package:flappy_bird/barriers.dart' ;
43import 'package:flappy_bird/bird.dart' ;
54import 'package:flutter/material.dart' ;
@@ -20,23 +19,26 @@ class _HomepageState extends State<Homepage> {
2019 double barrierx2 = barrierx1 + 1.5 ;
2120 Timer ? gameTimer;
2221 int score = 0 ;
23- // ignore: unused_element
22+
2423 void _showDialog () {
2524 showDialog (
2625 context: context,
2726 barrierDismissible: false ,
2827 builder: (context) {
2928 return AlertDialog (
3029 backgroundColor: Colors .brown,
31- title: Text ('Game Over' , style: TextStyle (color: Colors .white)),
30+ title: const Text ('Game Over' , style: TextStyle (color: Colors .white)),
3231 content: Text (
3332 'Your bird has fallen!\n Score: $score ' ,
34- style: TextStyle (color: Colors .white),
33+ style: const TextStyle (color: Colors .white),
3534 ),
3635 actions: [
3736 TextButton (
3837 onPressed: resetGame,
39- child: Text ('Restart' , style: TextStyle (color: Colors .white)),
38+ child: const Text (
39+ 'Restart' ,
40+ style: TextStyle (color: Colors .white),
41+ ),
4042 ),
4143 ],
4244 );
@@ -48,14 +50,18 @@ class _HomepageState extends State<Homepage> {
4850 setState (() {
4951 isGameStarted = true ;
5052 });
51- gameTimer = Timer .periodic (Duration (milliseconds: 50 ), (timer) {
53+
54+ gameTimer = Timer .periodic (const Duration (milliseconds: 50 ), (timer) {
5255 setState (() {
5356 time += 0.05 ;
5457 height = - 4.9 * time * time + 2.8 * time;
5558 birdY = initialPos - height;
59+
60+ // Move barriers
5661 barrierx1 -= 0.05 ;
5762 barrierx2 -= 0.05 ;
5863
64+ // Reposition barriers and increase score
5965 if (barrierx1 < - 1.5 ) {
6066 barrierx1 = 1.5 ;
6167 score++ ;
@@ -64,7 +70,9 @@ class _HomepageState extends State<Homepage> {
6470 barrierx2 = 1.5 ;
6571 score++ ;
6672 }
73+ // Score incremented by 1 for each barrier passed
6774
75+ // Check if bird is dead
6876 if (birdisDead ()) {
6977 gameTimer? .cancel ();
7078 _showDialog ();
@@ -83,7 +91,8 @@ class _HomepageState extends State<Homepage> {
8391 barrierx1 = 1 ;
8492 barrierx2 = barrierx1 + 1.5 ;
8593 score = 0 ;
86- });
94+ }); // reset the game variables to their initial values
95+
8796 gameTimer? .cancel ();
8897 Navigator .of (context).pop ();
8998 }
@@ -94,7 +103,36 @@ class _HomepageState extends State<Homepage> {
94103 }
95104
96105 bool birdisDead () {
97- return birdY > 1 || birdY < - 1 ;
106+ // Check if bird hits top or bottom of screen
107+ if (birdY > 1 || birdY < - 1 ) {
108+ return true ;
109+ }
110+
111+ // Check collision with first barrier
112+ if (barrierx1 >= - 0.15 && barrierx1 <= 0.15 ) {
113+ // Bottom barrier (size 50) - small barrier
114+ if (birdY > 0.6 ) {
115+ return true ;
116+ }
117+ // Top barrier (size 200) - large barrier
118+ if (birdY < - 0.2 ) {
119+ return true ;
120+ }
121+ }
122+
123+ // Check collision with second barrier
124+ if (barrierx2 >= - 0.15 && barrierx2 <= 0.15 ) {
125+ // Bottom barrier (size 150) - large barrier
126+ if (birdY > 0.0 ) {
127+ return true ;
128+ }
129+ // Top barrier (size 50) - small barrier
130+ if (birdY < - 0.6 ) {
131+ return true ;
132+ }
133+ }
134+
135+ return false ;
98136 }
99137
100138 @override
@@ -113,43 +151,43 @@ class _HomepageState extends State<Homepage> {
113151 children: [
114152 MyBird (birdY: birdY),
115153 Container (
116- alignment: Alignment (0 , - 0.5 ),
154+ alignment: const Alignment (0 , - 0.5 ),
117155 child: Text (
118156 isGameStarted ? '' : 'T A P T O P L A Y' ,
119- style: TextStyle (color: Colors .white),
157+ style: const TextStyle (color: Colors .white),
120158 ),
121159 ),
122160 if (isGameStarted)
123161 Container (
124- alignment: Alignment (- 0.8 , - 0.8 ),
162+ alignment: const Alignment (- 0.8 , - 0.8 ),
125163 child: Text (
126164 'Score: $score ' ,
127- style: TextStyle (
165+ style: const TextStyle (
128166 color: Colors .white,
129167 fontSize: 20 ,
130168 fontWeight: FontWeight .bold,
131169 ),
132170 ),
133171 ),
134172 AnimatedContainer (
135- duration: Duration (milliseconds: 0 ),
173+ duration: const Duration (milliseconds: 0 ),
136174 alignment: Alignment (barrierx1, 1 ),
137- child: MyBarrier (size: 50.0 ),
175+ child: const MyBarrier (size: 50.0 ),
138176 ),
139177 AnimatedContainer (
140- duration: Duration (milliseconds: 0 ),
178+ duration: const Duration (milliseconds: 0 ),
141179 alignment: Alignment (barrierx1, - 1 ),
142- child: MyBarrier (size: 200.0 ),
180+ child: const MyBarrier (size: 200.0 ),
143181 ),
144182 AnimatedContainer (
145- duration: Duration (milliseconds: 0 ),
183+ duration: const Duration (milliseconds: 0 ),
146184 alignment: Alignment (barrierx2, 1 ),
147- child: MyBarrier (size: 150.0 ),
185+ child: const MyBarrier (size: 150.0 ),
148186 ),
149187 AnimatedContainer (
150- duration: Duration (milliseconds: 0 ),
188+ duration: const Duration (milliseconds: 0 ),
151189 alignment: Alignment (barrierx2, - 1 ),
152- child: MyBarrier (size: 50.0 ),
190+ child: const MyBarrier (size: 50.0 ),
153191 ),
154192 ],
155193 ),
0 commit comments