|
1 |
| -import 'package:chemistry_game/page3.dart'; |
2 | 1 | import 'package:flutter/material.dart';
|
| 2 | +import 'dart:async'; |
3 | 3 |
|
4 |
| -class Page2 extends StatelessWidget { |
| 4 | +double balloonPositionX = 0.5; // Initial horizontal position of the balloon |
| 5 | +double balloonPositionY = 2.0; // Initial vertical position of the balloon |
| 6 | +bool gameStarted = false; |
| 7 | +bool gameResultShown = false; |
| 8 | +bool gameWon = false; |
| 9 | + |
| 10 | +class Page2 extends StatefulWidget { |
| 11 | + const Page2({Key? key}) : super(key: key); |
| 12 | + @override |
| 13 | + _Page2State createState() => _Page2State(); |
| 14 | +} |
| 15 | + |
| 16 | +class _Page2State extends State<Page2> { |
5 | 17 | @override
|
6 | 18 | Widget build(BuildContext context) {
|
| 19 | + @override |
| 20 | + void initState() { |
| 21 | + super.initState(); |
| 22 | + } // dont mess with this func, cause idk why it is even alive xd, jk... |
| 23 | + |
| 24 | + void startGame() { |
| 25 | + setState(() { |
| 26 | + balloonPositionX = 0.5; // Reset horizontal position of the balloon |
| 27 | + balloonPositionY = 0.8; // Reset vertical position of the balloon |
| 28 | + gameStarted = true; |
| 29 | + gameResultShown = false; |
| 30 | + gameWon = false; |
| 31 | + }); |
| 32 | + |
| 33 | + Timer.periodic(const Duration(milliseconds: 20), (timer) { |
| 34 | + // setState(() { |
| 35 | + // balloonPositionY -= 0.0005; // Move the balloon upwards |
| 36 | + // }); |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + void moveBalloonRight() { |
| 41 | + if (balloonPositionX < 1) { |
| 42 | + setState(() { |
| 43 | + balloonPositionX += 0.05; // Move the balloon to the right |
| 44 | + }); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + void moveBalloonLeft() { |
| 49 | + if (balloonPositionX > 0) { |
| 50 | + setState(() { |
| 51 | + balloonPositionX -= 0.05; // Move the balloon to the left |
| 52 | + }); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + void restartGame() { |
| 57 | + setState(() { |
| 58 | + balloonPositionX = 0.5; // Reset balloon position |
| 59 | + // currentBlockIndex = 0; // Reset the current block index |
| 60 | + gameStarted = false; |
| 61 | + gameResultShown = false; |
| 62 | + gameWon = false; |
| 63 | + }); |
| 64 | + } |
| 65 | + |
7 | 66 | return Scaffold(
|
8 | 67 | appBar: AppBar(
|
9 |
| - title: Text('Page 2'), |
| 68 | + title: const Text('Level 3'), |
10 | 69 | ),
|
11 | 70 | body: Center(
|
12 |
| - child: ElevatedButton( |
13 |
| - onPressed: () { |
14 |
| - Navigator.pushNamed(context, '/page3'); |
15 |
| - }, |
16 |
| - child: const Text("Move to page 3")), |
| 71 | + child: Column( |
| 72 | + mainAxisAlignment: MainAxisAlignment.center, |
| 73 | + children: <Widget>[ |
| 74 | + SizedBox( |
| 75 | + width: double.infinity, |
| 76 | + height: 400, |
| 77 | + child: Stack( |
| 78 | + children: [ |
| 79 | + Positioned( |
| 80 | + top: balloonPositionY * 400, |
| 81 | + left: MediaQuery.of(context).size.width * balloonPositionX - |
| 82 | + 25, |
| 83 | + child: GestureDetector( |
| 84 | + onTap: () { |
| 85 | + if (!gameStarted) { |
| 86 | + startGame(); |
| 87 | + } |
| 88 | + }, |
| 89 | + child: SizedBox( |
| 90 | + width: 50, |
| 91 | + height: 50, |
| 92 | + child: Image.asset( |
| 93 | + 'player.png', |
| 94 | + width: 100, |
| 95 | + height: 100, |
| 96 | + ), |
| 97 | + // decoration: const BoxDecoration( |
| 98 | + // color: Colors.red, |
| 99 | + // shape: BoxShape.circle, |
| 100 | + // ), |
| 101 | + ), |
| 102 | + ), |
| 103 | + ), |
| 104 | + // Render all four blocks at fixed positions |
| 105 | + |
| 106 | + Positioned( |
| 107 | + top: 0, |
| 108 | + left: MediaQuery.of(context).size.width * 0.1, |
| 109 | + child: Column( |
| 110 | + children: [ |
| 111 | + Container( |
| 112 | + width: 10, |
| 113 | + height: 100, |
| 114 | + color: Colors.blue, |
| 115 | + ), |
| 116 | + const Text('Block A'), |
| 117 | + ], |
| 118 | + ), |
| 119 | + ), |
| 120 | + |
| 121 | + Positioned( |
| 122 | + top: 0, |
| 123 | + left: MediaQuery.of(context).size.width * 0.7, |
| 124 | + child: Column( |
| 125 | + children: [ |
| 126 | + Container( |
| 127 | + width: 10, |
| 128 | + height: 100, |
| 129 | + color: Colors.orange, |
| 130 | + ), |
| 131 | + const Text('Block C'), |
| 132 | + ], |
| 133 | + ), |
| 134 | + ), |
| 135 | + ], |
| 136 | + ), |
| 137 | + ), |
| 138 | + const SizedBox(height: 20), |
| 139 | + if (gameResultShown) |
| 140 | + Text( |
| 141 | + gameWon ? 'You Win!' : 'You Lose!', |
| 142 | + style: const TextStyle(fontSize: 20), |
| 143 | + ), |
| 144 | + const SizedBox(height: 20), |
| 145 | + Column( |
| 146 | + children: [ |
| 147 | + ElevatedButton( |
| 148 | + onPressed: () { |
| 149 | + if (!gameStarted) { |
| 150 | + startGame(); |
| 151 | + } |
| 152 | + }, |
| 153 | + child: const Text('Start'), |
| 154 | + ), |
| 155 | + ], |
| 156 | + ), |
| 157 | + const SizedBox(height: 20), |
| 158 | + Column( |
| 159 | + children: [ |
| 160 | + ElevatedButton( |
| 161 | + onPressed: moveBalloonLeft, |
| 162 | + child: const Text('Left'), |
| 163 | + ), |
| 164 | + ], |
| 165 | + ), |
| 166 | + const SizedBox(height: 20), |
| 167 | + Column( |
| 168 | + children: [ |
| 169 | + ElevatedButton( |
| 170 | + onPressed: moveBalloonRight, |
| 171 | + child: const Text('Right'), |
| 172 | + ), |
| 173 | + ], |
| 174 | + ), |
| 175 | + const SizedBox(height: 20), |
| 176 | + if (gameStarted && gameResultShown && gameWon == false) |
| 177 | + ElevatedButton( |
| 178 | + onPressed: restartGame, |
| 179 | + child: const Text('Restart'), |
| 180 | + ), |
| 181 | + ], |
| 182 | + ), |
17 | 183 | ),
|
18 | 184 | );
|
19 | 185 | }
|
|
0 commit comments