Skip to content
Closed
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
1 change: 1 addition & 0 deletions 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 Down
67 changes: 56 additions & 11 deletions lib/homepage.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// import 'dart:async';
import 'dart:async';

import 'package:flappy_bird/barriers.dart';
import 'package:flappy_bird/bird.dart';
Expand All @@ -18,6 +18,8 @@ class _HomepageState extends State<Homepage> {
bool isGameStarted = false;
static double barrierx1 = 1;
double barrierx2 = barrierx1 + 1.5;
Timer? gameTimer;
int score = 0;
// ignore: unused_element
void _showDialog() {
showDialog(
Expand All @@ -28,7 +30,7 @@ class _HomepageState extends State<Homepage> {
backgroundColor: Colors.brown,
title: Text('Game Over', style: TextStyle(color: Colors.white)),
content: Text(
'Your bird has fallen!',
'Your bird has fallen!\nScore: $score',
style: TextStyle(color: Colors.white),
),
actions: [
Expand All @@ -43,15 +45,48 @@ class _HomepageState extends State<Homepage> {
}

void startGame() {
// TODO: Implement the game loop logic here.
// Hint: Use a Timer to periodically update the bird's position and barriers.
// Hint: Update the bird's position using physics equations.
// Hint: Check if the bird is dead and stop the game if necessary.
setState(() {
isGameStarted = true;
});
gameTimer = Timer.periodic(Duration(milliseconds: 50), (timer) {
setState(() {
time += 0.05;
height = -4.9 * time * time + 2.8 * time;
birdY = initialPos - height;
barrierx1 -= 0.05;
barrierx2 -= 0.05;

if (barrierx1 < -1.5) {
barrierx1 = 1.5;
score++;
}
if (barrierx2 < -1.5) {
barrierx2 = 1.5;
score++;
}
//score incremented by 1 for each barrier passed

if (birdisDead()) {
gameTimer?.cancel();
_showDialog();
}
});
});
}

void resetGame() {
// TODO: Reset the game state to its initial values.
// Hint: Reset birdY, initialPos, time, and isGameStarted.
setState(() {
birdY = 0.0;
initialPos = birdY;
time = 0;
height = 0;
isGameStarted = false;
barrierx1 = 1;
barrierx2 = barrierx1 + 1.5;
score = 0;
});//reset the game variables to their initial values
gameTimer?.cancel();
Navigator.of(context).pop();
}

void jump() {
Expand All @@ -60,9 +95,7 @@ class _HomepageState extends State<Homepage> {
}

bool birdisDead() {
// TODO: Check if the bird is out of bounds.
// Hint: Return true if birdY is greater than 1 or less than -1.
return false; // Replace this with the correct condition.
return birdY > 1 || birdY < -1;
}

@override
Expand All @@ -87,6 +120,18 @@ class _HomepageState extends State<Homepage> {
style: TextStyle(color: Colors.white),
),
),
if (isGameStarted)
Container(
alignment: Alignment(-0.8, -0.8),
child: Text(
'Score: $score',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
AnimatedContainer(
duration: Duration(milliseconds: 0),
alignment: Alignment(barrierx1, 1),
Expand Down