2 Dsanke
2 Dsanke
size;
final screenWidth = screenSize.width;
final screenHeight = screenSize.height;
List<Piece> getPieces() {
final pieces = <Piece>[];
draw();
drawFood();
// 1
for (var i = 0; i < length; ++i) {
// 2
if (i >= positions.length) {
continue;
}
// 3
pieces.add(
Piece(
posX: positions[i].dx.toInt(),
posY: positions[i].dy.toInt(),
// 4
size: step,
color: Colors.red,
),
);
}
return pieces;
}
// 2
while (length > positions.length) {
positions.add(positions[positions.length - 1]);
}
// 3
for (var i = positions.length - 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 == Direction.right) {
nextPosition = Offset(position.dx + step, position.dy);
} else if (direction == Direction.left) {
nextPosition = Offset(position.dx - step, position.dy);
} else if (direction == Direction.up) {
nextPosition = Offset(position.dx, position.dy - step);
} else if (direction == Direction.down) {
nextPosition = Offset(position.dx, position.dy + step);
}
return nextPosition;
}
return Scaffold(
body: Container(
color: Color(0XFFF5BB00),
child: Stack(
children: [
Stack(
children: getPieces(),
),
],
),
),
);
void changeSpeed() {
if (timer != null && timer.isActive) timer.cancel();
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: foodPosition.dx.toInt(),
posY: foodPosition.dy.toInt(),
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,
],
),
),
);
}
return false;
}
@override
Widget build(BuildContext context) {
//...
return Scaffold(
body: Container(
color: Color(0XFFF5BB00),
child: Stack(
children: [
getPlayAreaBorder(),
Stack(
children: getPieces(),
),
getControls(),
food,
getScore(),
],
),
),
);
}