flutter_snake

Last updated:

0 purchases

flutter_snake Image
flutter_snake Images
Add to Cart

Description:

flutter snake

flutter_snake #
This package provides a way to have a game of snake in his app' !



Snake in game
Big snake
Little snake
















Getting Started #
In the pubspec.yaml of your flutter project, add the following dependency:
dependencies:
...
flutter_snake: any
copied to clipboard
In your library add the following import:
import 'package:flutter_snake/flutter_snake.dart';
copied to clipboard
For help getting started with Flutter, view the online documentation.
Usage #
First, define the SnakeGame:
SnakeGame? snakeGame;

@override
void initState() {
super.initState();

// Example:
snakeGame = new SnakeGame(
caseWidth: 25.0,
numberCaseHorizontally: 11,
numberCaseVertically: 11,
durationBetweenTicks: Duration(milliseconds: 400),
colorBackground1: Color(0XFF7CFC00),
colorBackground2: Color(0XFF32CD32),
);
}
copied to clipboard
Then, add the snakeGame in your page:
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: snakeGame ?? Text("Not initialized"),
),
);
}
copied to clipboard
The snake will start, but he will crash into the wall because we didn't implemented his movement.
For example, you can manage his movement like this :
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
onPressed: () => snakeGame?.nextDirection = SNAKE_MOVE.left, // Define left as the next direction of the snake
icon: Icon(Icons.subdirectory_arrow_left),
),
Text("SNAKE"),
IconButton(
onPressed: () => snakeGame?.nextDirection = SNAKE_MOVE.right, // Define right as the next direction of the snake
icon: Icon(Icons.subdirectory_arrow_right),
),
],
),
copied to clipboard
You can also add a controller to the snakeGame so you can listen to the GameEvent (win, loose, food eaten)
StreamController<GAME_EVENT>? controller;
SnakeGame? snakeGame;

@override
void initState() {
super.initState();
controller = StreamController<GAME_EVENT>();
controller?.stream.listen((GAME_EVENT value) {
print(value.toString());
});

snakeGame = new SnakeGame(
caseWidth: 25.0,
numberCaseHorizontally: 11,
numberCaseVertically: 11,
controllerEvent: controller,
durationBetweenTicks: Duration(milliseconds: 400),
colorBackground1: Color(0XFF7CFC00),
colorBackground2: Color(0XFF32CD32),
);
}

@override
void dispose() {
controller?.close();
super.dispose();
}
copied to clipboard
Customize #
SnakeGame #
/// Case width / height (It's a square)
double caseWidth;

/// Duration between each ticks
final Duration durationBetweenTicks;

/// Number of case horizontally (x)
final int numberCaseHorizontally;

/// Number of case vertically (y)
final int numberCaseVertically;

/// If defines, the controller stream receive the game event
final StreamController<GAME_EVENT>? controllerEvent;

/// Color variation for the background
final Color colorBackground1;
final Color colorBackground2;

/// Snake image body and fruit
final String? snakeHeadImgPath;
final String? snakeBodyImgPath;
final String? snakeBodyTurnImgPath;
final String? snakeTailImgPath;
final String? snakeFruitImgPath;
copied to clipboard
Examples #
Main example: example
If you need more example, don't hesitate to ask me ! :)
FYI #
Don't hesitate to report bugs, to ask for more features or participate in the developement :)
Thanks to #
Gaƫlle Bauvin and Alyssia C for the snake default sprites ! <3

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.