flutter_snake 1.1.0 icon indicating copy to clipboard operation
flutter_snake: ^1.1.0 copied to clipboard

A snake game for your app'. Available on Mobile, WEB, Desktop ! You can manage his movement as you want.

flutter_snake #

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

Snake in game Big snake Little snake
A gif showing the snake in movement A big snake A little snake

Getting Started #

In the pubspec.yaml of your flutter project, add the following dependency:

dependencies:
  ...
  flutter_snake: any

In your library add the following import:

import 'package:flutter_snake/flutter_snake.dart';

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

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"),
      ),
    );
  }

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),
                ),
              ],
            ),

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

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;

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

9
likes
130
pub points
66%
popularity

Publisher

verified publisher icontri7ratops.fr

A snake game for your app'. Available on Mobile, WEB, Desktop ! You can manage his movement as you want.

Repository (GitHub)

Documentation

API reference

License

Icon for licenses.MIT (LICENSE)

Dependencies

flutter

More

Packages that depend on flutter_snake