-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHpage.dart
240 lines (228 loc) · 7.76 KB
/
Hpage.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// main code fo the game which displays all the widgets and content on the screen
import 'dart:async';
import 'package:angryFlappyBird/barriers.dart';
import 'package:flutter/material.dart';
import 'bird.dart';
import 'barriers.dart';
class Hpage extends StatefulWidget {
@override
_HpageState createState() => _HpageState();
}
class _HpageState extends State<Hpage> {
static double bYaxis = 0; // bird's y coordinate
double time = 0; // time of flight
double height = 0; // height of bird
double initHeight = bYaxis; // initial height of the bird
bool gameStarted = false;
double xBarrier1 = 1;
double xBarrier2 = 2;
double xBarrier3 = 3;
int score = 0;
void resetGame() {
setState(() {
bYaxis = 0;
time = 0;
height = 0;
initHeight = bYaxis;
xBarrier1 = 1;
xBarrier2 = 2;
xBarrier3 = 3;
gameStarted = false;
score = 0;
});
}
void jump() {
gameStarted = true;
setState(() {
time = 0;
initHeight = bYaxis;
});
}
// checking if bird crashes into any barrier
bool crashCheck() {
if (xBarrier1 < 0.2 && xBarrier1 > -0.2) {
if (bYaxis < -0.6 || bYaxis > 0.4) return true;
}
if (xBarrier2 < 0.2 && xBarrier2 > -0.2) {
if (bYaxis < -0.6 || bYaxis > 0.4) return true;
}
return false;
}
void showGameOverDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
backgroundColor: Colors.yellow[900],
title: Text(
"GAME OVER",
style: TextStyle(color: Colors.yellow),
),
// ignore: unused_label
actions: [
// ignore: deprecated_member_use
FlatButton(
child: Text(
"PLAY AGAIN ?",
style: TextStyle(color: Colors.white),
),
onPressed: () {
resetGame();
setState(() {
gameStarted = false;
});
Navigator.of(context).pop();
},
)
],
);
});
}
void gameBeg() {
gameStarted = true;
Timer.periodic(Duration(milliseconds: 29), (timer) {
// manages the frames
// one frame for 10 millisecs
time +=
0.035; // increasing time continously to increase bird's height in below eq.n
height = -4.9 * time * time +
3 * time; // 2nd equation of gravitational motion (g/2 = 4.9)
setState(() {
// dynamically set the bird at the required position
bYaxis = initHeight - height;
// looping the barriers
if (xBarrier1 < -2) {
score += 1;
xBarrier1 += 3.1;
} else {
xBarrier1 -= 0.05;
}
if (xBarrier2 < -2) {
score += 1;
xBarrier2 += 3.1;
} else {
xBarrier2 -= 0.05;
}
if (xBarrier3 < -2) {
score += 1;
xBarrier3 += 3.1;
} else {
xBarrier3 -= 0.05;
}
});
// to prevent the bird from escaping BELOW screen
if (bYaxis >= 0.95 || crashCheck()) {
// bYaxis = 1; // change the height of bird
timer.cancel(); // cancel that particular time count
showGameOverDialog();
}
// to prevent bird escaping the ABOVE screen
if (bYaxis <= -1) {
bYaxis = -1; // change the height of bird
timer.cancel(); // cancel that particular time count
gameBeg(); // recursive call to continue bird's flight
}
}); // 1 frame ends here
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
// once clicked or tapped on the screen
if (gameStarted) {
// if the game has started (which initially isn't)
jump(); // call the jump function
} else if (!gameStarted) {
gameBeg(); // else start the game
}
},
child: Scaffold(
body: Column(
children: [
Expanded(
// for the sky background
flex: 5, // to fill the available space along the axis
child: Stack(
// to stack all widgets on 1 another respectively
children: [
AnimatedContainer(
alignment: Alignment(
0, bYaxis), // Bird's position at (x,y) coordinate
duration: Duration(milliseconds: 29),
color: Colors.blue[200],
child:
Bird(), // Object of class Bird (image imported from the device)
),
Container(
// to display : tap to play
alignment: Alignment(0, -0.25),
child: gameStarted
? Text("")
: Text("TAP TO FLY",
style: TextStyle(fontSize: 35))),
// deploying barriers
AnimatedContainer(
alignment: Alignment(xBarrier1, 1),
duration: Duration(milliseconds: 0),
child: Barrier(
size: 150.0, // barrier's height
),
),
AnimatedContainer(
alignment: Alignment(xBarrier1, -1), // inverted barrier
duration: Duration(milliseconds: 0),
child: Barrier(
size: 90.0, // barrier's height
),
),
AnimatedContainer(
alignment: Alignment(xBarrier2, 1),
duration: Duration(milliseconds: 0),
child: Barrier(
size: 190.0, // barrier's height
),
),
AnimatedContainer(
alignment: Alignment(xBarrier2, -1), // inverted barrier
duration: Duration(milliseconds: 0),
child: Barrier(
size: 85.0, // barrier's height
),
),
AnimatedContainer(
alignment: Alignment(xBarrier1, 1),
duration: Duration(milliseconds: 0),
child: Barrier(
size: 150.0, // barrier's height
),
),
AnimatedContainer(
alignment: Alignment(xBarrier1, -1), // inverted barrier
duration: Duration(milliseconds: 0),
child: Barrier(
size: 90.0, // barrier's height
),
),
],
)),
Expanded(
// fir green ground
child: Container(
color: Colors.green[900],
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// to display the scores
Text("SCORE : ",
style: TextStyle(color: Colors.white, fontSize: 20)),
Text(score.toString(),
style: TextStyle(color: Colors.white, fontSize: 20))
],
),
))
],
),
),
);
}
}