final screenSize = [Link](context).
size;
final screenWidth = [Link];
final screenHeight = [Link];
List<Piece> getPieces() {
final pieces = <Piece>[];
draw();
drawFood();
// 1
for (var i = 0; i < length; ++i) {
// 2
if (i >= [Link]) {
continue;
}
// 3
[Link](
Piece(
posX: positions[i].[Link](),
posY: positions[i].[Link](),
// 4
size: step,
color: [Link],
),
);
}
return pieces;
}
void draw() async {
// 1
if ([Link] == 0) {
[Link](getRandomPositionWithinRange());
}
// 2
while (length > [Link]) {
[Link](positions[[Link] - 1]);
}
// 3
for (var i = [Link] - 1; i > 0; i--) {
positions[i] = positions[i - 1];
}
// 4
positions[0] = await getNextPosition(positions[0]);
}
Future<Offset> getNextPosition(Offset position) async {
Offset nextPosition;
if (direction == [Link]) {
nextPosition = Offset([Link] + step, [Link]);
} else if (direction == [Link]) {
nextPosition = Offset([Link] - step, [Link]);
} else if (direction == [Link]) {
nextPosition = Offset([Link], [Link] - step);
} else if (direction == [Link]) {
nextPosition = Offset([Link], [Link] + step);
}
return nextPosition;
}
return Scaffold(
body: Container(
color: Color(0XFFF5BB00),
child: Stack(
children: [
Stack(
children: getPieces(),
),
],
),
),
);
void changeSpeed() {
if (timer != null && [Link]) [Link]();
timer = [Link](Duration(milliseconds: 200 ~/ speed), (timer)
{
setState(() {});
});
}
void restart() {
changeSpeed();
}
Widget getControls() {
return ControlPanel( // 1
onTapped: (Direction newDirection) { // 2
direction = newDirection; // 3
},
);
}
import 'control_panel.dart';
@override
Widget build(BuildContext context) {
// ...
return Scaffold(
body: Container(
color: Color(0XFFF5BB00),
child: Stack(
children: [
Stack(
children: getPieces(),
),
getControls(),
],
),
),
);
}
void drawFood() {
// 1
if (foodPosition == null) {
foodPosition = getRandomPositionWithinRange();
}
// 2
food = Piece(
posX: [Link](),
posY: [Link](),
size: step,
color: Color(0XFF8EA604),
isAnimated: true,
);
}
@override
Widget build(BuildContext context) {
//...
return Scaffold(
body: Container(
color: Color(0XFFF5BB00),
child: Stack(
children: [
Stack(
children: getPieces(),
),
getControls(),
food,
],
),
),
);
}
void drawFood() {
// ...
if (foodPosition == positions[0]) {
length++;
speed = speed + 0.25;
score = score + 5;
changeSpeed();
foodPosition = getRandomPositionWithinRange();
}
// ...
}
@override
Widget build(BuildContext context) {
//...
return Scaffold(
body: Container(
color: Color(0XFFF5BB00),
child: Stack(
children: [
getPlayAreaBorder(),
Stack(
children: getPieces(),
),
getControls(),
food,
],
),
),
);
}
bool detectCollision(Offset position) {
if ([Link] >= upperBoundX && direction == [Link]) {
return true;
} else if ([Link] <= lowerBoundX && direction == [Link])
{
return true;
} else if ([Link] >= upperBoundY && direction == [Link])
{
return true;
} else if ([Link] <= lowerBoundY && direction == [Link]) {
return true;
}
return false;
}
Future<Offset> getNextPosition(Offset position) async {
//...
if (detectCollision(position) == true) {
if (timer != null && [Link]) [Link]();
await [Link](
Duration(milliseconds: 500), () => showGameOverDialog());
return position;
}
//...
}
Widget getScore() {
return Positioned(
top: 50.0,
right: 40.0,
child: Text(
"Score: " + [Link](),
style: TextStyle(fontSize: 24.0),
),
);
}
@override
Widget build(BuildContext context) {
//...
return Scaffold(
body: Container(
color: Color(0XFFF5BB00),
child: Stack(
children: [
getPlayAreaBorder(),
Stack(
children: getPieces(),
),
getControls(),
food,
getScore(),
],
),
),
);
}