Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/barriers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class MyBarrier extends StatelessWidget {
const MyBarrier({super.key, this.size});

@override
//successfully implemented the barrier code
Widget build(BuildContext context) {
return Container(
width: 100,
Expand All @@ -15,7 +16,7 @@ class MyBarrier extends StatelessWidget {
color: const Color.fromARGB(34, 76, 175, 79),
width: 2,
),
borderRadius: BorderRadius.all(Radius.circular(10)),
borderRadius: const BorderRadius.all(Radius.circular(10)),
),
);
}
Expand Down
78 changes: 58 additions & 20 deletions lib/homepage.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:async';

import 'package:flappy_bird/barriers.dart';
import 'package:flappy_bird/bird.dart';
import 'package:flutter/material.dart';
Expand All @@ -20,23 +19,26 @@ class _HomepageState extends State<Homepage> {
double barrierx2 = barrierx1 + 1.5;
Timer? gameTimer;
int score = 0;
// ignore: unused_element

void _showDialog() {
showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return AlertDialog(
backgroundColor: Colors.brown,
title: Text('Game Over', style: TextStyle(color: Colors.white)),
title: const Text('Game Over', style: TextStyle(color: Colors.white)),
content: Text(
'Your bird has fallen!\nScore: $score',
style: TextStyle(color: Colors.white),
style: const TextStyle(color: Colors.white),
),
actions: [
TextButton(
onPressed: resetGame,
child: Text('Restart', style: TextStyle(color: Colors.white)),
child: const Text(
'Restart',
style: TextStyle(color: Colors.white),
),
),
],
);
Expand All @@ -48,14 +50,18 @@ class _HomepageState extends State<Homepage> {
setState(() {
isGameStarted = true;
});
gameTimer = Timer.periodic(Duration(milliseconds: 50), (timer) {

gameTimer = Timer.periodic(const Duration(milliseconds: 50), (timer) {
setState(() {
time += 0.05;
height = -4.9 * time * time + 2.8 * time;
birdY = initialPos - height;

// Move barriers
barrierx1 -= 0.05;
barrierx2 -= 0.05;

// Reposition barriers and increase score
if (barrierx1 < -1.5) {
barrierx1 = 1.5;
score++;
Expand All @@ -64,7 +70,9 @@ class _HomepageState extends State<Homepage> {
barrierx2 = 1.5;
score++;
}
// Score incremented by 1 for each barrier passed

// Check if bird is dead
if (birdisDead()) {
gameTimer?.cancel();
_showDialog();
Expand All @@ -83,7 +91,8 @@ class _HomepageState extends State<Homepage> {
barrierx1 = 1;
barrierx2 = barrierx1 + 1.5;
score = 0;
});
}); // reset the game variables to their initial values

gameTimer?.cancel();
Navigator.of(context).pop();
}
Expand All @@ -94,7 +103,36 @@ class _HomepageState extends State<Homepage> {
}

bool birdisDead() {
return birdY > 1 || birdY < -1;
// Check if bird hits top or bottom of screen
if (birdY > 1 || birdY < -1) {
return true;
}

// Check collision with first barrier
if (barrierx1 >= -0.15 && barrierx1 <= 0.15) {
// Bottom barrier (size 50) - small barrier
if (birdY > 0.6) {
return true;
}
// Top barrier (size 200) - large barrier
if (birdY < -0.2) {
return true;
}
}

// Check collision with second barrier
if (barrierx2 >= -0.15 && barrierx2 <= 0.15) {
// Bottom barrier (size 150) - large barrier
if (birdY > 0.0) {
return true;
}
// Top barrier (size 50) - small barrier
if (birdY < -0.6) {
return true;
}
}

return false;
}

@override
Expand All @@ -113,43 +151,43 @@ class _HomepageState extends State<Homepage> {
children: [
MyBird(birdY: birdY),
Container(
alignment: Alignment(0, -0.5),
alignment: const Alignment(0, -0.5),
child: Text(
isGameStarted ? '' : 'T A P T O P L A Y',
style: TextStyle(color: Colors.white),
style: const TextStyle(color: Colors.white),
),
),
if (isGameStarted)
Container(
alignment: Alignment(-0.8, -0.8),
alignment: const Alignment(-0.8, -0.8),
child: Text(
'Score: $score',
style: TextStyle(
style: const TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
AnimatedContainer(
duration: Duration(milliseconds: 0),
duration: const Duration(milliseconds: 0),
alignment: Alignment(barrierx1, 1),
child: MyBarrier(size: 50.0),
child: const MyBarrier(size: 50.0),
),
AnimatedContainer(
duration: Duration(milliseconds: 0),
duration: const Duration(milliseconds: 0),
alignment: Alignment(barrierx1, -1),
child: MyBarrier(size: 200.0),
child: const MyBarrier(size: 200.0),
),
AnimatedContainer(
duration: Duration(milliseconds: 0),
duration: const Duration(milliseconds: 0),
alignment: Alignment(barrierx2, 1),
child: MyBarrier(size: 150.0),
child: const MyBarrier(size: 150.0),
),
AnimatedContainer(
duration: Duration(milliseconds: 0),
duration: const Duration(milliseconds: 0),
alignment: Alignment(barrierx2, -1),
child: MyBarrier(size: 50.0),
child: const MyBarrier(size: 50.0),
),
],
),
Expand Down
Loading