Skip to content

Commit 4efc97b

Browse files
committed
.
1 parent 069384c commit 4efc97b

File tree

4 files changed

+125
-101
lines changed

4 files changed

+125
-101
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
file.extension linguist-language=Dart

lib/main.dart

+35-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:flutter/material.dart';
2+
import 'package:flutter/services.dart';
23
import 'package:snake/snake_pixel.dart';
34

45
void main() {
@@ -25,32 +26,44 @@ class HomePage extends StatelessWidget {
2526

2627
@override
2728
Widget build(BuildContext context) {
29+
SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);
2830
return Scaffold(
2931
body: ListView(
30-
children: <Widget>[
31-
Container(
32-
margin: const EdgeInsets.symmetric(horizontal: 120.0),
33-
padding: const EdgeInsets.symmetric(vertical: 20.0),
34-
child: ElevatedButton(
35-
onPressed: () {
36-
Navigator.of(context).pushNamed('/snake_pixel');
37-
},
38-
style: ElevatedButton.styleFrom(
39-
shadowColor: Colors.blue,
40-
// 按钮背景颜色
41-
shape: RoundedRectangleBorder(
42-
borderRadius: BorderRadius.circular(30), // 设置圆角
43-
),
44-
padding:
45-
const EdgeInsets.symmetric(horizontal: 30, vertical: 15),
46-
// 内边距
47-
elevation: 5, // 阴影效果
48-
),
49-
child: const Text('复古像素贪吃蛇'),
50-
),
51-
),
32+
children: const <Widget>[
33+
Button(txt: '贪吃蛇A'),
5234
],
5335
),
5436
);
5537
}
5638
}
39+
40+
class Button extends StatelessWidget {
41+
const Button({super.key, required this.txt});
42+
43+
final String txt;
44+
45+
@override
46+
Widget build(BuildContext context) {
47+
return Container(
48+
margin: const EdgeInsets.symmetric(horizontal: 120.0),
49+
padding: const EdgeInsets.symmetric(vertical: 20.0),
50+
child: ElevatedButton(
51+
onPressed: () {
52+
Navigator.of(context).pushNamed('/snake_pixel');
53+
},
54+
style: ElevatedButton.styleFrom(
55+
shadowColor: Colors.blue,
56+
shape: RoundedRectangleBorder(
57+
borderRadius: BorderRadius.circular(30),
58+
),
59+
padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 15),
60+
elevation: 12,
61+
),
62+
child: Text(
63+
txt,
64+
style: const TextStyle(fontSize: 32.0, color: Colors.black),
65+
),
66+
),
67+
);
68+
}
69+
}

lib/snake_pixel.dart

+88-8
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,17 @@ class SnakeGame extends StatefulWidget {
1313

1414
class _SnakeGameState extends State<SnakeGame> {
1515
final int squaresPerRow = 20;
16-
1716
final int squaresPerCol = 43;
18-
1917
List<int> snake = [];
20-
2118
var direction = 'down';
22-
2319
var isPlaying = false;
24-
2520
int food = 0;
2621

2722
@override
2823
void initState() {
24+
WidgetsBinding.instance.addPostFrameCallback((_) {
25+
showGameStartDialog(context);
26+
});
2927
resetGame();
3028
super.initState();
3129
}
@@ -41,7 +39,6 @@ class _SnakeGameState extends State<SnakeGame> {
4139

4240
void startGame() {
4341
isPlaying = true;
44-
4542
Timer.periodic(const Duration(milliseconds: 200), (Timer timer) {
4643
setState(() {
4744
moveSnake();
@@ -110,16 +107,99 @@ class _SnakeGameState extends State<SnakeGame> {
110107
return false;
111108
}
112109

110+
void showGameStartDialog(BuildContext context) {
111+
showDialog(
112+
context: context,
113+
barrierDismissible: false, // 点击对话框外部关闭
114+
builder: (BuildContext context) {
115+
return Dialog(
116+
child: ClipRRect(
117+
borderRadius: BorderRadius.circular(32.0),
118+
child: SizedBox(
119+
height: 150.0,
120+
child: Column(
121+
children: [
122+
Expanded(
123+
child: Row(
124+
children: [
125+
Expanded(
126+
child: GestureDetector(
127+
onTap: () {
128+
Navigator.of(context).pop();
129+
},
130+
child: Container(
131+
alignment: Alignment.center,
132+
color: Colors.white,
133+
child: const Text(
134+
'手动模式',
135+
style: TextStyle(
136+
fontSize: 32.0,
137+
color: Colors.black,
138+
),
139+
),
140+
),
141+
),
142+
),
143+
Container(
144+
width: 1,
145+
height: double.infinity,
146+
color: Colors.black,
147+
),
148+
Expanded(
149+
child: GestureDetector(
150+
onTap: () {
151+
Navigator.of(context).pop();
152+
},
153+
child: Container(
154+
alignment: Alignment.center,
155+
color: Colors.white,
156+
child: const Text(
157+
'AI模式',
158+
style: TextStyle(
159+
fontSize: 32.0,
160+
color: Colors.black,
161+
),
162+
),
163+
),
164+
),
165+
),
166+
],
167+
),
168+
),
169+
],
170+
),
171+
),
172+
),
173+
);
174+
},
175+
);
176+
}
177+
113178
void showGameOverDialog() {
114179
showDialog(
115180
context: context,
181+
barrierDismissible: false,
116182
builder: (BuildContext context) {
117183
return AlertDialog(
118184
title: const Text('游戏结束'),
119-
content: Text('你的分数: ${snake.length}'),
185+
content: Text(
186+
'你的分数: ${snake.length}',
187+
style: const TextStyle(fontSize: 24.0),
188+
),
120189
actions: <Widget>[
121190
TextButton(
122-
child: const Text('关闭'),
191+
child: const Text(
192+
'排行榜',
193+
style: TextStyle(fontSize: 24.0),
194+
),
195+
onPressed: () {},
196+
),
197+
const SizedBox(width: 100.0),
198+
TextButton(
199+
child: const Text(
200+
'重新开始',
201+
style: TextStyle(fontSize: 24.0),
202+
),
123203
onPressed: () {
124204
Navigator.of(context).pop();
125205
setState(() {

pubspec.yaml

+1-71
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,20 @@
11
name: snake
22
description: "A new Flutter project."
3-
# The following line prevents the package from being accidentally published to
4-
# pub.dev using `flutter pub publish`. This is preferred for private packages.
5-
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
63

7-
# The following defines the version and build number for your application.
8-
# A version number is three numbers separated by dots, like 1.2.43
9-
# followed by an optional build number separated by a +.
10-
# Both the version and the builder number may be overridden in flutter
11-
# build by specifying --build-name and --build-number, respectively.
12-
# In Android, build-name is used as versionName while build-number used as versionCode.
13-
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
14-
# In iOS, build-name is used as CFBundleShortVersionString while build-number is used as CFBundleVersion.
15-
# Read more about iOS versioning at
16-
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
17-
# In Windows, build-name is used as the major, minor, and patch parts
18-
# of the product and file versions while build-number is used as the build suffix.
194
version: 1.0.0+1
205

216
environment:
227
sdk: ^3.5.4
238

24-
# Dependencies specify other packages that your package needs in order to work.
25-
# To automatically upgrade your package dependencies to the latest versions
26-
# consider running `flutter pub upgrade --major-versions`. Alternatively,
27-
# dependencies can be manually updated by changing the version numbers below to
28-
# the latest version available on pub.dev. To see which dependencies have newer
29-
# versions available, run `flutter pub outdated`.
309
dependencies:
3110
flutter:
3211
sdk: flutter
33-
34-
35-
# The following adds the Cupertino Icons font to your application.
36-
# Use with the CupertinoIcons class for iOS style icons.
3712
cupertino_icons: ^1.0.8
3813

3914
dev_dependencies:
4015
flutter_test:
4116
sdk: flutter
42-
43-
# The "flutter_lints" package below contains a set of recommended lints to
44-
# encourage good coding practices. The lint set provided by the package is
45-
# activated in the `analysis_options.yaml` file located at the root of your
46-
# package. See that file for information about deactivating specific lint
47-
# rules and activating additional ones.
4817
flutter_lints: ^4.0.0
4918

50-
# For information on the generic Dart part of this file, see the
51-
# following page: https://dart.dev/tools/pub/pubspec
52-
53-
# The following section is specific to Flutter packages.
5419
flutter:
55-
56-
# The following line ensures that the Material Icons font is
57-
# included with your application, so that you can use the icons in
58-
# the material Icons class.
59-
uses-material-design: true
60-
61-
# To add assets to your application, add an assets section, like this:
62-
# assets:
63-
# - images/a_dot_burr.jpeg
64-
# - images/a_dot_ham.jpeg
65-
66-
# An image asset can refer to one or more resolution-specific "variants", see
67-
# https://flutter.dev/to/resolution-aware-images
68-
69-
# For details regarding adding assets from package dependencies, see
70-
# https://flutter.dev/to/asset-from-package
71-
72-
# To add custom fonts to your application, add a fonts section here,
73-
# in this "flutter" section. Each entry in this list should have a
74-
# "family" key with the font family name, and a "fonts" key with a
75-
# list giving the asset and other descriptors for the font. For
76-
# example:
77-
# fonts:
78-
# - family: Schyler
79-
# fonts:
80-
# - asset: fonts/Schyler-Regular.ttf
81-
# - asset: fonts/Schyler-Italic.ttf
82-
# style: italic
83-
# - family: Trajan Pro
84-
# fonts:
85-
# - asset: fonts/TrajanPro.ttf
86-
# - asset: fonts/TrajanPro_Bold.ttf
87-
# weight: 700
88-
#
89-
# For details regarding fonts from package dependencies,
90-
# see https://flutter.dev/to/font-from-package
20+
uses-material-design: true

0 commit comments

Comments
 (0)