Skip to content

Commit 0e4c42b

Browse files
committedJul 2, 2024·
game over screen added
1 parent ea7f20d commit 0e4c42b

File tree

1 file changed

+82
-41
lines changed

1 file changed

+82
-41
lines changed
 

‎lib/screens/games/game_screen.dart

+82-41
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class _GameScreenState extends State<GameScreen> {
1616
int _timeLeft = 20; // Time left in seconds
1717
late Timer _timer;
1818
bool _gameLoaded = false; // Tracks if the game is loaded
19+
bool _gameOver = false; // Tracks if the game is over
1920

2021
@override
2122
void initState() {
@@ -29,9 +30,12 @@ class _GameScreenState extends State<GameScreen> {
2930
(Timer timer) {
3031
if (_progress <= 0 || _timeLeft <= 0) {
3132
timer.cancel();
33+
setState(() {
34+
_gameOver = true;
35+
});
3236
} else {
3337
setState(() {
34-
_progress -= 0.05;
38+
_progress -= 1 / 20; // Decrease progress based on total time
3539
_timeLeft -= 1;
3640
});
3741
}
@@ -64,53 +68,90 @@ class _GameScreenState extends State<GameScreen> {
6468
appBar: AppBar(
6569
title: const Header(title: 'Quiz Game'),
6670
),
67-
body: Column(
71+
body: Stack(
6872
children: <Widget>[
69-
Padding(
70-
padding: const EdgeInsets.all(16.0),
71-
child: Row(
72-
mainAxisAlignment: MainAxisAlignment.spaceBetween,
73-
children: <Widget>[
74-
Expanded(
75-
child: SizedBox(
76-
height: 20, // Make the progress bar thicker
77-
child: TweenAnimationBuilder(
78-
tween: Tween<double>(begin: 1.0, end: _progress),
79-
duration: const Duration(milliseconds: 500),
80-
builder: (context, value, child) {
81-
return LinearProgressIndicator(
82-
value: value,
83-
backgroundColor: Colors.grey[300],
84-
color: Colors.blue,
85-
);
86-
},
87-
),
88-
),
89-
),
90-
const SizedBox(width: 20),
91-
Column(
92-
crossAxisAlignment: CrossAxisAlignment.end,
73+
Column(
74+
children: <Widget>[
75+
Padding(
76+
padding: const EdgeInsets.all(16.0),
77+
child: Row(
78+
mainAxisAlignment: MainAxisAlignment.spaceBetween,
9379
children: <Widget>[
94-
Text(
95-
'Score: $_score',
96-
style: const TextStyle(
97-
fontSize: 24, fontWeight: FontWeight.bold),
80+
Expanded(
81+
child: Container(
82+
decoration: BoxDecoration(
83+
color: Colors.blue.shade700, // Dark blue background
84+
borderRadius: BorderRadius.circular(20.0),
85+
boxShadow: <BoxShadow>[
86+
BoxShadow(
87+
color: Colors.black.withOpacity(0.2),
88+
spreadRadius: 2,
89+
blurRadius: 4,
90+
offset: const Offset(0, 2), // Shadow position
91+
),
92+
],
93+
),
94+
height: 30, // Make the progress bar thicker
95+
child: ClipRRect(
96+
borderRadius: BorderRadius.circular(20.0),
97+
child: TweenAnimationBuilder(
98+
tween: Tween<double>(begin: 1.0, end: _progress),
99+
duration: const Duration(milliseconds: 500),
100+
builder: (BuildContext context, double value,
101+
Widget? child) {
102+
return LinearProgressIndicator(
103+
value: value,
104+
backgroundColor: Colors.grey[300],
105+
color: Colors.blue,
106+
);
107+
},
108+
),
109+
),
110+
),
98111
),
99-
Text(
100-
'Time Left: $_timeLeft s',
101-
style: const TextStyle(fontSize: 16),
112+
const SizedBox(width: 20),
113+
Column(
114+
crossAxisAlignment: CrossAxisAlignment.end,
115+
children: <Widget>[
116+
Text(
117+
'Score: $_score',
118+
style: const TextStyle(
119+
fontSize: 24, fontWeight: FontWeight.bold),
120+
),
121+
const SizedBox(height: 5),
122+
Text(
123+
'Time Left: $_timeLeft s',
124+
style: const TextStyle(fontSize: 16),
125+
),
126+
],
102127
),
103128
],
104129
),
105-
],
106-
),
130+
),
131+
Expanded(
132+
child: GameWrapper(
133+
onLoaded: onGameLoaded,
134+
onScoreUpdate: updateScore,
135+
),
136+
),
137+
],
107138
),
108-
Expanded(
109-
child: GameWrapper(
110-
onLoaded: onGameLoaded,
111-
onScoreUpdate: updateScore,
139+
if (_gameOver)
140+
Positioned.fill(
141+
child: Container(
142+
color: Colors.grey.withOpacity(0.7),
143+
child: const Center(
144+
child: Text(
145+
'Game Over!',
146+
style: TextStyle(
147+
fontSize: 40,
148+
fontWeight: FontWeight.bold,
149+
color: Colors.white,
150+
),
151+
),
152+
),
153+
),
112154
),
113-
),
114155
],
115156
),
116157
);
@@ -127,7 +168,7 @@ class GameWrapper extends StatelessWidget {
127168
@override
128169
Widget build(BuildContext context) {
129170
// Simulate a delay for loading the game
130-
Future.delayed(const Duration(seconds: 2), onLoaded);
171+
Future.delayed(const Duration(seconds: 4), onLoaded);
131172

132173
return const Center(
133174
child: SelectTheAreaGame(),

0 commit comments

Comments
 (0)
Please sign in to comment.