diff --git a/lib/Database/database.dart b/lib/Database/database.dart new file mode 100644 index 0000000..51cde3a --- /dev/null +++ b/lib/Database/database.dart @@ -0,0 +1,64 @@ +// ignore_for_file: avoid_print + +import 'package:flappy_bird/model/user.dart'; +import 'package:sqflite/sqflite.dart'; +import 'package:path/path.dart'; + +class Sql{ + + static Database? _db ; + + Future get db async { + if (_db == null){ + _db = await initial() ; + return _db ; + }else { + return _db ; + } + } + + initial() async { + String file = await getDatabasesPath() ; + String path = join(file , 'database.db') ; + Database database = await openDatabase(path , onCreate: _onCreate, version: 1) ; + return database ; + } + + _onCreate(Database database , int version) async { + await database.execute(''' + CREATE TABLE "$User" ( + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + "best" INTEGER NOT NULL + ) + ''') ; + print(" onCreate =====================================") ; + } + + readData(String name,int score) async { + Database? database = await db ; + List response = await database!.rawQuery("Statement"); + return response ; + } + + insertData(String name,int score) async { + Database? database = await db ; + int response = await database!.rawInsert("Statement"); + return response ; + } + + // updateData(String sql) async { + // Database? database = await db ; + // int response = await database!.rawUpdate(sql); + // return response ; + // } + + // deleteData(String sql) async { + // Database? database = await db ; + // int response = await database!.rawDelete(sql); + // return response ; + // } + + // _onUpgrade(Database db , int oldVersion , int newVersion ) { + // print("onUpgrade =====================================") ; + // } +} \ No newline at end of file diff --git a/lib/Ui/HomePage.dart b/lib/Ui/HomePage.dart index 195cca6..1c72dcf 100644 --- a/lib/Ui/HomePage.dart +++ b/lib/Ui/HomePage.dart @@ -1,8 +1,9 @@ -// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables, sized_box_for_whitespace, avoid_unnecessary_containers, empty_statements, unused_field +// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables, sized_box_for_whitespace, avoid_unnecessary_containers, empty_statements, unused_field, avoid_print 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:flappy_bird/constant/constant.dart'; import 'package:flutter/material.dart'; class HomePage extends StatefulWidget { @@ -13,16 +14,14 @@ class HomePage extends StatefulWidget { } class _HomePageState extends State { - 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; + bool gameHasStarted = false; //TODO: Make it Global // Barrier Variables static List barrierX = [2, 3.4]; List> barrierY = [ @@ -60,7 +59,7 @@ class _HomePageState extends State { ), Expanded( flex: 1, - child: Score(score),), + child: Score(),), ]), ), ); @@ -100,15 +99,18 @@ class _HomePageState extends State { timer.cancel(); _showDialog(); }; - time += 0.04 ; + time += 0.032 ; }); Timer.periodic(Duration(seconds: 2), (timer) { if(birdIsDead()){ timer.cancel(); - score = 0; + SCORE = 0; }else{ setState(() { - score ++; + if(SCORE == TOP_SCORE) { + TOP_SCORE++; + } + SCORE++; }); } }); @@ -125,7 +127,6 @@ class _HomePageState extends State { } // Barrier for(int i = 0; i < barrierX.length; i++){ - } return false; } @@ -136,7 +137,7 @@ class _HomePageState extends State { yAxis = 0; gameHasStarted = false; time = 0; - score = 0; + SCORE = 0; initialHeight = yAxis; barrierX[0] = 2; barrierX[1] = 3.4; diff --git a/lib/Ui/Score.dart b/lib/Ui/Score.dart index 0b9e84e..5b99416 100644 --- a/lib/Ui/Score.dart +++ b/lib/Ui/Score.dart @@ -1,11 +1,11 @@ // ignore_for_file: prefer_const_literals_to_create_immutables, prefer_const_constructors, prefer_const_constructors_in_immutables +import 'package:flappy_bird/model/user.dart'; import 'package:flutter/material.dart'; +import '../constant/constant.dart'; class Score extends StatefulWidget { - final int score; - Score(this.score, {Key? key}) : super(key: key); - + Score({Key? key}) : super(key: key); @override State createState() => _ScoreState(); } @@ -13,6 +13,7 @@ class Score extends StatefulWidget { class _ScoreState extends State { @override Widget build(BuildContext context) { + return Container(color: Colors.brown,child: Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ @@ -22,7 +23,7 @@ class _ScoreState extends State { children: [ Text("Score",style: TextStyle(color: Colors.white,fontSize: 30),), SizedBox(height: 10,), - Text(widget.score.toString(),style: TextStyle(color: Colors.white,fontSize: 30)), + Text(SCORE.toString(),style: TextStyle(color: Colors.white,fontSize: 30)), ],), // Best TEXT Column( @@ -30,7 +31,7 @@ class _ScoreState extends State { children: [ Text("Best",style: TextStyle(color: Colors.white,fontSize: 30)), SizedBox(height: 10,), - Text("0",style: TextStyle(color: Colors.white,fontSize: 30)), + Text(TOP_SCORE.toString(),style: TextStyle(color: Colors.white,fontSize: 30)), ],), ], ),); diff --git a/lib/constant/constant.dart b/lib/constant/constant.dart new file mode 100644 index 0000000..a66365f --- /dev/null +++ b/lib/constant/constant.dart @@ -0,0 +1,2 @@ +int SCORE = 0; +int TOP_SCORE = 0; \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 63f938b..a129ab8 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,10 +1,9 @@ -// ignore_for_file: prefer_const_constructors - +// ignore_for_file: prefer_const_constructors, unnecessary_import import 'package:flutter/material.dart'; - import 'Ui/HomePage.dart'; void main() { + runApp(MaterialApp( home: HomePage(), debugShowCheckedModeBanner: false, diff --git a/lib/model/user.dart b/lib/model/user.dart new file mode 100644 index 0000000..b337b22 --- /dev/null +++ b/lib/model/user.dart @@ -0,0 +1,11 @@ +// import 'package:hive/hive.dart'; + +// part 'user.g.dart'; + +class User { + int id; + String name; + int topScore; + + User(this.id, this.name, this.topScore); +} \ No newline at end of file diff --git a/pubspec.lock b/pubspec.lock index 74524ed..2929c50 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -343,6 +343,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.8.2" + sqflite: + dependency: "direct main" + description: + name: sqflite + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.3+1" + sqflite_common: + dependency: transitive + description: + name: sqflite_common + url: "https://pub.dartlang.org" + source: hosted + version: "2.2.1+1" stack_trace: dependency: transitive description: @@ -364,6 +378,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.1.0" + synchronized: + dependency: transitive + description: + name: synchronized + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.0+2" term_glyph: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 5a0dc50..a04f537 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -35,6 +35,7 @@ dependencies: # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.2 audioplayers: ^1.0.1 + sqflite: ^2.0.3+1 dev_dependencies: @@ -49,6 +50,7 @@ dev_dependencies: flutter_launcher_icons: "^0.10.0" + flutter_icons: android: true ios: true