Skip to content

Commit 0d18c3b

Browse files
committed
create database
> Create Database > make Score & Top Score Global > update pubspec > create model class (user) `not used yet`
1 parent 2c2cc9e commit 0d18c3b

File tree

8 files changed

+119
-18
lines changed

8 files changed

+119
-18
lines changed

lib/Database/database.dart

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// ignore_for_file: avoid_print
2+
3+
import 'package:flappy_bird/model/user.dart';
4+
import 'package:sqflite/sqflite.dart';
5+
import 'package:path/path.dart';
6+
7+
class Sql{
8+
9+
static Database? _db ;
10+
11+
Future<Database?> get db async {
12+
if (_db == null){
13+
_db = await initial() ;
14+
return _db ;
15+
}else {
16+
return _db ;
17+
}
18+
}
19+
20+
initial() async {
21+
String file = await getDatabasesPath() ;
22+
String path = join(file , 'database.db') ;
23+
Database database = await openDatabase(path , onCreate: _onCreate, version: 1) ;
24+
return database ;
25+
}
26+
27+
_onCreate(Database database , int version) async {
28+
await database.execute('''
29+
CREATE TABLE "$User" (
30+
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
31+
"best" INTEGER NOT NULL
32+
)
33+
''') ;
34+
print(" onCreate =====================================") ;
35+
}
36+
37+
readData(String name,int score) async {
38+
Database? database = await db ;
39+
List<Map> response = await database!.rawQuery("Statement");
40+
return response ;
41+
}
42+
43+
insertData(String name,int score) async {
44+
Database? database = await db ;
45+
int response = await database!.rawInsert("Statement");
46+
return response ;
47+
}
48+
49+
// updateData(String sql) async {
50+
// Database? database = await db ;
51+
// int response = await database!.rawUpdate(sql);
52+
// return response ;
53+
// }
54+
55+
// deleteData(String sql) async {
56+
// Database? database = await db ;
57+
// int response = await database!.rawDelete(sql);
58+
// return response ;
59+
// }
60+
61+
// _onUpgrade(Database db , int oldVersion , int newVersion ) {
62+
// print("onUpgrade =====================================") ;
63+
// }
64+
}

lib/Ui/HomePage.dart

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables, sized_box_for_whitespace, avoid_unnecessary_containers, empty_statements, unused_field
1+
// 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
22
import 'dart:async';
33
import 'package:flappy_bird/Ui/Bird.dart';
44
import 'package:flappy_bird/Ui/Score.dart';
55
import 'package:flappy_bird/Ui/barrier.dart';
6+
import 'package:flappy_bird/constant/constant.dart';
67
import 'package:flutter/material.dart';
78

89
class HomePage extends StatefulWidget {
@@ -13,16 +14,14 @@ class HomePage extends StatefulWidget {
1314
}
1415

1516
class _HomePageState extends State<HomePage> {
16-
int score = 0;
1717
// Bird Variables
1818
static double yAxis = 0;
19-
static double xAxis = 0;
2019
double time = 0;
2120
double height = 0;
2221
double gravity = -3.9; // How strong the Gravity
2322
double velocity = 2.5; // How strong the jump
2423
double initialHeight = yAxis;
25-
bool gameHasStarted = false;
24+
bool gameHasStarted = false; //TODO: Make it Global
2625
// Barrier Variables
2726
static List<double> barrierX = [2, 3.4];
2827
List<List<double>> barrierY = [
@@ -60,7 +59,7 @@ class _HomePageState extends State<HomePage> {
6059
),
6160
Expanded(
6261
flex: 1,
63-
child: Score(score),),
62+
child: Score(),),
6463
]),
6564
),
6665
);
@@ -100,15 +99,18 @@ class _HomePageState extends State<HomePage> {
10099
timer.cancel();
101100
_showDialog();
102101
};
103-
time += 0.04 ;
102+
time += 0.032 ;
104103
});
105104
Timer.periodic(Duration(seconds: 2), (timer) {
106105
if(birdIsDead()){
107106
timer.cancel();
108-
score = 0;
107+
SCORE = 0;
109108
}else{
110109
setState(() {
111-
score ++;
110+
if(SCORE == TOP_SCORE) {
111+
TOP_SCORE++;
112+
}
113+
SCORE++;
112114
});
113115
}
114116
});
@@ -125,7 +127,6 @@ class _HomePageState extends State<HomePage> {
125127
}
126128
// Barrier
127129
for(int i = 0; i < barrierX.length; i++){
128-
129130
}
130131
return false;
131132
}
@@ -136,7 +137,7 @@ class _HomePageState extends State<HomePage> {
136137
yAxis = 0;
137138
gameHasStarted = false;
138139
time = 0;
139-
score = 0;
140+
SCORE = 0;
140141
initialHeight = yAxis;
141142
barrierX[0] = 2;
142143
barrierX[1] = 3.4;

lib/Ui/Score.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
// ignore_for_file: prefer_const_literals_to_create_immutables, prefer_const_constructors, prefer_const_constructors_in_immutables
22

3+
import 'package:flappy_bird/model/user.dart';
34
import 'package:flutter/material.dart';
5+
import '../constant/constant.dart';
46

57
class Score extends StatefulWidget {
6-
final int score;
7-
Score(this.score, {Key? key}) : super(key: key);
8-
8+
Score({Key? key}) : super(key: key);
99
@override
1010
State<Score> createState() => _ScoreState();
1111
}
1212

1313
class _ScoreState extends State<Score> {
1414
@override
1515
Widget build(BuildContext context) {
16+
1617
return Container(color: Colors.brown,child: Row(
1718
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
1819
children: [
@@ -22,15 +23,15 @@ class _ScoreState extends State<Score> {
2223
children: [
2324
Text("Score",style: TextStyle(color: Colors.white,fontSize: 30),),
2425
SizedBox(height: 10,),
25-
Text(widget.score.toString(),style: TextStyle(color: Colors.white,fontSize: 30)),
26+
Text(SCORE.toString(),style: TextStyle(color: Colors.white,fontSize: 30)),
2627
],),
2728
// Best TEXT
2829
Column(
2930
mainAxisAlignment: MainAxisAlignment.center,
3031
children: [
3132
Text("Best",style: TextStyle(color: Colors.white,fontSize: 30)),
3233
SizedBox(height: 10,),
33-
Text("0",style: TextStyle(color: Colors.white,fontSize: 30)),
34+
Text(TOP_SCORE.toString(),style: TextStyle(color: Colors.white,fontSize: 30)),
3435
],),
3536
],
3637
),);

lib/constant/constant.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
int SCORE = 0;
2+
int TOP_SCORE = 0;

lib/main.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
// ignore_for_file: prefer_const_constructors
2-
1+
// ignore_for_file: prefer_const_constructors, unnecessary_import
32
import 'package:flutter/material.dart';
4-
53
import 'Ui/HomePage.dart';
64

75
void main() {
6+
87
runApp(MaterialApp(
98
home: HomePage(),
109
debugShowCheckedModeBanner: false,

lib/model/user.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// import 'package:hive/hive.dart';
2+
3+
// part 'user.g.dart';
4+
5+
class User {
6+
int id;
7+
String name;
8+
int topScore;
9+
10+
User(this.id, this.name, this.topScore);
11+
}

pubspec.lock

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,20 @@ packages:
343343
url: "https://pub.dartlang.org"
344344
source: hosted
345345
version: "1.8.2"
346+
sqflite:
347+
dependency: "direct main"
348+
description:
349+
name: sqflite
350+
url: "https://pub.dartlang.org"
351+
source: hosted
352+
version: "2.0.3+1"
353+
sqflite_common:
354+
dependency: transitive
355+
description:
356+
name: sqflite_common
357+
url: "https://pub.dartlang.org"
358+
source: hosted
359+
version: "2.2.1+1"
346360
stack_trace:
347361
dependency: transitive
348362
description:
@@ -364,6 +378,13 @@ packages:
364378
url: "https://pub.dartlang.org"
365379
source: hosted
366380
version: "1.1.0"
381+
synchronized:
382+
dependency: transitive
383+
description:
384+
name: synchronized
385+
url: "https://pub.dartlang.org"
386+
source: hosted
387+
version: "3.0.0+2"
367388
term_glyph:
368389
dependency: transitive
369390
description:

pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ dependencies:
3535
# Use with the CupertinoIcons class for iOS style icons.
3636
cupertino_icons: ^1.0.2
3737
audioplayers: ^1.0.1
38+
sqflite: ^2.0.3+1
3839

3940

4041
dev_dependencies:
@@ -49,6 +50,7 @@ dev_dependencies:
4950
flutter_launcher_icons: "^0.10.0"
5051

5152

53+
5254
flutter_icons:
5355
android: true
5456
ios: true

0 commit comments

Comments
 (0)