0% found this document useful (0 votes)
17 views

2 Dsanke

The document contains code for a snake game. It defines functions for: 1) Getting the screen size and pieces/segments of the snake. 2) Drawing the snake by updating its position each cycle and adding new segments when it eats food. 3) Handling input, movement, collision detection and restarting the game. 4) Placing random food and updating the score when eaten. 5) Building the UI with a stack of layers for snake, controls, food and score.

Uploaded by

nazarovnozim46
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

2 Dsanke

The document contains code for a snake game. It defines functions for: 1) Getting the screen size and pieces/segments of the snake. 2) Drawing the snake by updating its position each cycle and adding new segments when it eats food. 3) Handling input, movement, collision detection and restarting the game. 4) Placing random food and updating the score when eaten. 5) Building the UI with a stack of layers for snake, controls, food and score.

Uploaded by

nazarovnozim46
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

final screenSize = MediaQuery.of(context).

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;
}

void draw() async {


// 1
if (positions.length == 0) {
positions.add(getRandomPositionWithinRange());
}

// 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();

timer = Timer.periodic(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: 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,
],
),
),
);
}

bool detectCollision(Offset position) {

if (position.dx >= upperBoundX && direction == Direction.right) {


return true;
} else if (position.dx <= lowerBoundX && direction == Direction.left)
{
return true;
} else if (position.dy >= upperBoundY && direction == Direction.down)
{
return true;
} else if (position.dy <= lowerBoundY && direction == Direction.up) {
return true;
}

return false;
}

Future<Offset> getNextPosition(Offset position) async {


//...
if (detectCollision(position) == true) {
if (timer != null && timer.isActive) timer.cancel();
await Future.delayed(
Duration(milliseconds: 500), () => showGameOverDialog());
return position;
}
//...
}
Widget getScore() {
return Positioned(
top: 50.0,
right: 40.0,
child: Text(
"Score: " + score.toString(),
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(),
],
),
),
);
}

You might also like