diff --git a/.dart_tool/package_config.json b/.dart_tool/package_config.json index d3d3821..cb842b1 100644 --- a/.dart_tool/package_config.json +++ b/.dart_tool/package_config.json @@ -3,163 +3,163 @@ "packages": [ { "name": "async", - "rootUri": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache/hosted/pub.dev/async-2.13.0", + "rootUri": "file:///C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/async-2.13.0", "packageUri": "lib/", "languageVersion": "3.4" }, { "name": "boolean_selector", - "rootUri": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache/hosted/pub.dev/boolean_selector-2.1.2", + "rootUri": "file:///C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/boolean_selector-2.1.2", "packageUri": "lib/", "languageVersion": "3.1" }, { "name": "characters", - "rootUri": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache/hosted/pub.dev/characters-1.4.0", + "rootUri": "file:///C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/characters-1.4.0", "packageUri": "lib/", "languageVersion": "3.4" }, { "name": "clock", - "rootUri": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache/hosted/pub.dev/clock-1.1.2", + "rootUri": "file:///C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/clock-1.1.2", "packageUri": "lib/", "languageVersion": "3.4" }, { "name": "collection", - "rootUri": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache/hosted/pub.dev/collection-1.19.1", + "rootUri": "file:///C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/collection-1.19.1", "packageUri": "lib/", "languageVersion": "3.4" }, { "name": "cupertino_icons", - "rootUri": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache/hosted/pub.dev/cupertino_icons-1.0.8", + "rootUri": "file:///C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/cupertino_icons-1.0.8", "packageUri": "lib/", "languageVersion": "3.1" }, { "name": "fake_async", - "rootUri": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache/hosted/pub.dev/fake_async-1.3.3", + "rootUri": "file:///C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/fake_async-1.3.3", "packageUri": "lib/", "languageVersion": "3.3" }, { "name": "flutter", - "rootUri": "file:///C:/Users/UTKARSH/Desktop/flutter/packages/flutter", + "rootUri": "file:///C:/flutter/src/flutter/packages/flutter", "packageUri": "lib/", "languageVersion": "3.8" }, { "name": "flutter_lints", - "rootUri": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache/hosted/pub.dev/flutter_lints-5.0.0", + "rootUri": "file:///C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/flutter_lints-5.0.0", "packageUri": "lib/", "languageVersion": "3.5" }, { "name": "flutter_test", - "rootUri": "file:///C:/Users/UTKARSH/Desktop/flutter/packages/flutter_test", + "rootUri": "file:///C:/flutter/src/flutter/packages/flutter_test", "packageUri": "lib/", "languageVersion": "3.8" }, { "name": "leak_tracker", - "rootUri": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache/hosted/pub.dev/leak_tracker-11.0.2", + "rootUri": "file:///C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/leak_tracker-11.0.2", "packageUri": "lib/", "languageVersion": "3.2" }, { "name": "leak_tracker_flutter_testing", - "rootUri": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache/hosted/pub.dev/leak_tracker_flutter_testing-3.0.10", + "rootUri": "file:///C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/leak_tracker_flutter_testing-3.0.10", "packageUri": "lib/", "languageVersion": "3.2" }, { "name": "leak_tracker_testing", - "rootUri": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache/hosted/pub.dev/leak_tracker_testing-3.0.2", + "rootUri": "file:///C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/leak_tracker_testing-3.0.2", "packageUri": "lib/", "languageVersion": "3.2" }, { "name": "lints", - "rootUri": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache/hosted/pub.dev/lints-5.1.1", + "rootUri": "file:///C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/lints-5.1.1", "packageUri": "lib/", "languageVersion": "3.6" }, { "name": "matcher", - "rootUri": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache/hosted/pub.dev/matcher-0.12.17", + "rootUri": "file:///C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/matcher-0.12.17", "packageUri": "lib/", "languageVersion": "3.4" }, { "name": "material_color_utilities", - "rootUri": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache/hosted/pub.dev/material_color_utilities-0.11.1", + "rootUri": "file:///C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/material_color_utilities-0.11.1", "packageUri": "lib/", "languageVersion": "2.17" }, { "name": "meta", - "rootUri": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache/hosted/pub.dev/meta-1.16.0", + "rootUri": "file:///C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/meta-1.16.0", "packageUri": "lib/", "languageVersion": "2.12" }, { "name": "path", - "rootUri": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache/hosted/pub.dev/path-1.9.1", + "rootUri": "file:///C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/path-1.9.1", "packageUri": "lib/", "languageVersion": "3.4" }, { "name": "sky_engine", - "rootUri": "file:///C:/Users/UTKARSH/Desktop/flutter/bin/cache/pkg/sky_engine", + "rootUri": "file:///C:/flutter/src/flutter/bin/cache/pkg/sky_engine", "packageUri": "lib/", "languageVersion": "3.8" }, { "name": "source_span", - "rootUri": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache/hosted/pub.dev/source_span-1.10.1", + "rootUri": "file:///C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/source_span-1.10.1", "packageUri": "lib/", "languageVersion": "3.1" }, { "name": "stack_trace", - "rootUri": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache/hosted/pub.dev/stack_trace-1.12.1", + "rootUri": "file:///C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/stack_trace-1.12.1", "packageUri": "lib/", "languageVersion": "3.4" }, { "name": "stream_channel", - "rootUri": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache/hosted/pub.dev/stream_channel-2.1.4", + "rootUri": "file:///C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/stream_channel-2.1.4", "packageUri": "lib/", "languageVersion": "3.3" }, { "name": "string_scanner", - "rootUri": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache/hosted/pub.dev/string_scanner-1.4.1", + "rootUri": "file:///C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/string_scanner-1.4.1", "packageUri": "lib/", "languageVersion": "3.1" }, { "name": "term_glyph", - "rootUri": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache/hosted/pub.dev/term_glyph-1.2.2", + "rootUri": "file:///C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/term_glyph-1.2.2", "packageUri": "lib/", "languageVersion": "3.1" }, { "name": "test_api", - "rootUri": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache/hosted/pub.dev/test_api-0.7.6", + "rootUri": "file:///C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/test_api-0.7.6", "packageUri": "lib/", "languageVersion": "3.5" }, { "name": "vector_math", - "rootUri": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache/hosted/pub.dev/vector_math-2.2.0", + "rootUri": "file:///C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/vector_math-2.2.0", "packageUri": "lib/", "languageVersion": "3.1" }, { "name": "vm_service", - "rootUri": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache/hosted/pub.dev/vm_service-15.0.0", + "rootUri": "file:///C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/vm_service-15.0.0", "packageUri": "lib/", "languageVersion": "3.3" }, @@ -172,7 +172,7 @@ ], "generator": "pub", "generatorVersion": "3.9.2", - "flutterRoot": "file:///C:/Users/UTKARSH/Desktop/flutter", - "flutterVersion": "3.35.3", - "pubCache": "file:///C:/Users/UTKARSH/AppData/Local/Pub/Cache" + "flutterRoot": "file:///C:/flutter/src/flutter", + "flutterVersion": "3.35.6", + "pubCache": "file:///C:/Users/Asus/AppData/Local/Pub/Cache" } diff --git a/.dart_tool/version b/.dart_tool/version index 398f1f6..eb4861a 100644 --- a/.dart_tool/version +++ b/.dart_tool/version @@ -1 +1 @@ -3.35.3 \ No newline at end of file +3.35.6 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 39d752c..9ed90a6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,53 @@ -node_modules +# Flutter/Dart/Pub related .dart_tool/ .packages +.pub/ build/ +.flutter-plugins +.flutter-plugins-dependencies + +# IDE directories +.idea/ +.vscode/ +*.iml + +# Flutter specific +.flutter-plugins-dependencies +.flutter-plugins +.melos_tool/ + +# Android +android/.gradle/ +android/local.properties +android/**/GeneratedPluginRegistrant.java +android/app/key.properties + +# iOS +ios/Pods/ +ios/Flutter/Flutter.framework +ios/Flutter/Flutter.podspec +ios/Runner/GeneratedPluginRegistrant.* + +# Web +web/.dart_tool/ + +# Linux +linux/flutter/ephemeral/ + +# macOS +macos/Flutter/ephemeral/ + +# Windows +windows/flutter/ephemeral/ + +# Files for app signing +*.jks +*.keystore + +# Symbolication data +*.symbols + +# Misc +.DS_Store +*.log +*.tmp diff --git a/lib/homepage.dart b/lib/homepage.dart index 25e42bd..3874c40 100644 --- a/lib/homepage.dart +++ b/lib/homepage.dart @@ -1,8 +1,8 @@ import 'dart:async'; import 'package:flappy_bird/barriers.dart'; + import 'package:flappy_bird/bird.dart'; import 'package:flutter/material.dart'; - class Homepage extends StatefulWidget { const Homepage({super.key}); @@ -16,10 +16,15 @@ class _HomepageState extends State { double height = 0, time = 0; bool isGameStarted = false; static double barrierx1 = 1; - double barrierx2 = barrierx1 + 1.5; + double barrierx2 = barrierx1 + 2.5; // Increased offset for delayed entry + Timer? gameTimer; - int score = 0; + double gravity = 4.5; // Gravity constant (positive, pulls down) + // <-- CORRECTION 1: Velocity must be negative for an upward jump + double velocity = -2.5; // Jump velocity (negative, pushes up) + double barrierSpeed = 0.05; // Speed of barriers moving + // ignore: unused_element void _showDialog() { showDialog( context: context, @@ -27,18 +32,15 @@ class _HomepageState extends State { builder: (context) { return AlertDialog( backgroundColor: Colors.brown, - title: const Text('Game Over', style: TextStyle(color: Colors.white)), + title: Text('Game Over', style: TextStyle(color: Colors.white)), content: Text( - 'Your bird has fallen!\nScore: $score',//display the score when game is over - style: const TextStyle(color: Colors.white), + 'Your bird has fallen!', + style: TextStyle(color: Colors.white), ), actions: [ TextButton( onPressed: resetGame, - child: const Text( - 'Restart', - style: TextStyle(color: Colors.white), - ), + child: Text('Restart', style: TextStyle(color: Colors.white)), ), ], ); @@ -47,91 +49,89 @@ class _HomepageState extends State { } void startGame() { - setState(() { - isGameStarted = true; - }); + isGameStarted = true; - gameTimer = Timer.periodic(const Duration(milliseconds: 50), (timer) { - setState(() { - time += 0.05; - height = -4.2 * time * time + 2.6 * time; - birdY = initialPos - height; + gameTimer = Timer.periodic(Duration(milliseconds: 50), (timer) { + // Update time + time += 0.05; - // Move barriers - barrierx1 -= 0.03; - barrierx2 -= 0.03; + // <-- CORRECTION 2: Use standard kinematic equation s = v_0*t + 0.5*a*t^2 + // Calculate vertical displacement (height) + height = (0.5 * gravity * time * time) + (velocity * time); - // Reposition barriers and score incremented by 1 for each barrier passed + setState(() { + // <-- CORRECTION 3: Update birdY by ADDING displacement + // A negative displacement (jump) makes birdY smaller (moves up) + // A positive displacement (gravity) makes birdY larger (moves down) + birdY = initialPos + height; + }); + + // Move barriers + setState(() { if (barrierx1 < -1.5) { barrierx1 = 1.5; - score++; + } else { + barrierx1 -= barrierSpeed; } + if (barrierx2 < -1.5) { barrierx2 = 1.5; - score++; - } - // Score incremented by 1 for each barrier passed - - // Check if bird is dead - if (birdisDead()) { - gameTimer?.cancel(); - _showDialog(); + } else { + barrierx2 -= barrierSpeed; } }); + + // Check if bird is dead + if (birdisDead()) { + timer.cancel(); + _showDialog(); + } }); } void resetGame() { + Navigator.pop(context); // Close the dialog setState(() { - birdY = 0.0; + birdY = 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(); + barrierx1 = 3; + barrierx2 = barrierx1 + 2.5; // Keep the same offset on reset + }); } void jump() { setState(() { - time = 0; - initialPos = birdY; - birdY -= 0.05; + time = 0; // Reset time for the physics equation + initialPos = + birdY; // Set the start of the jump to the bird's current position }); } - bool birdisDead() { - // Check if bird hits top or bottom of screen + // Check if bird is out of bounds (top or bottom) 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) { + // <-- CORRECTION 4: Swapped collision logic to match build() method. + + // Check collision with first set of barriers (Top: 200, Bottom: 50) + // This has *more* barrier, so it should have the *smaller* gap. + if ((barrierx1 >= -0.1 && barrierx1 <= 0.1)) { + if (birdY <= -0.4 || birdY >= 0.4) { + // Small gap 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) { + // Check collision with second set of barriers (Top: 50, Bottom: 150) + // This has *less* barrier, so it should have the *larger* gap. + if ((barrierx2 >= -0.1 && barrierx2 <= 0.1)) { + if (birdY <= -0.6 || birdY >= 0.6) { + // Large gap return true; } } @@ -139,6 +139,12 @@ class _HomepageState extends State { return false; } + @override + void dispose() { + gameTimer?.cancel(); + super.dispose(); + } + @override Widget build(BuildContext context) { return GestureDetector( @@ -155,44 +161,35 @@ class _HomepageState extends State { children: [ MyBird(birdY: birdY), Container( - alignment: const Alignment(0, -0.5), + alignment: Alignment(0, -0.5), child: Text( isGameStarted ? '' : 'T A P T O P L A Y', - style: const TextStyle(color: Colors.white), + style: TextStyle(color: Colors.white), ), ), - //score board implemented - if (isGameStarted) - Container( - alignment: const Alignment(-0.8, -0.8), - child: Text( - 'Score: $score', - style: const TextStyle( - color: Colors.white, - fontSize: 20, - fontWeight: FontWeight.bold, - ), - ), - ), - AnimatedContainer( - duration: const Duration(milliseconds: 0), - alignment: Alignment(barrierx1, 1), - child: const MyBarrier(size: 50.0), - ), + + // Barrier 1 (Top: 200, Bottom: 50) -> Small Gap AnimatedContainer( - duration: const Duration(milliseconds: 0), + duration: Duration(milliseconds: 0), alignment: Alignment(barrierx1, -1), - child: const MyBarrier(size: 200.0), + child: MyBarrier(size: 200.0), // Large top ), AnimatedContainer( - duration: const Duration(milliseconds: 0), - alignment: Alignment(barrierx2, 1), - child: const MyBarrier(size: 150.0), + duration: Duration(milliseconds: 0), + alignment: Alignment(barrierx1, 1), + child: MyBarrier(size: 50.0), // Small bottom ), + + // Barrier 2 (Top: 50, Bottom: 150) -> Large Gap AnimatedContainer( - duration: const Duration(milliseconds: 0), + duration: Duration(milliseconds: 0), alignment: Alignment(barrierx2, -1), - child: const MyBarrier(size: 50.0), + child: MyBarrier(size: 50.0), // Small top + ), + AnimatedContainer( + duration: Duration(milliseconds: 0), + alignment: Alignment(barrierx2, 1), + child: MyBarrier(size: 150.0), // Large bottom ), ], ),