|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:math'; |
| 3 | + |
| 4 | +import 'package:SnakeGameFlutter/game_over.dart'; |
| 5 | +import 'package:flutter/material.dart'; |
| 6 | + |
| 7 | +class GamePage extends StatefulWidget { |
| 8 | + @override |
| 9 | + _GamePageState createState() => _GamePageState(); |
| 10 | +} |
| 11 | + |
| 12 | +class _GamePageState extends State<GamePage> with TickerProviderStateMixin { |
| 13 | + |
| 14 | + int _playerScore; |
| 15 | + bool _hasStarted; |
| 16 | + Animation _snakeAnimation; |
| 17 | + AnimationController _snakeController; |
| 18 | + List _snake = [404, 405, 406, 407]; |
| 19 | + final int _noOfSquares = 500; |
| 20 | + final Duration _duration = Duration(milliseconds: 250); |
| 21 | + final int _squareSize = 20; |
| 22 | + String _currentSnakeDirection; |
| 23 | + int _snakeFoodPosition; |
| 24 | + Random _random = new Random(); |
| 25 | + |
| 26 | + @override |
| 27 | + void initState() { |
| 28 | + super.initState(); |
| 29 | + _setUpGame(); |
| 30 | + } |
| 31 | + |
| 32 | + void _setUpGame() { |
| 33 | + _playerScore = 0; |
| 34 | + _currentSnakeDirection = 'RIGHT'; |
| 35 | + _hasStarted = true; |
| 36 | + do { |
| 37 | + _snakeFoodPosition = _random.nextInt(_noOfSquares); |
| 38 | + } while(_snake.contains(_snakeFoodPosition)); |
| 39 | + _snakeController = AnimationController(vsync: this, duration: _duration); |
| 40 | + _snakeAnimation = CurvedAnimation(curve: Curves.easeInOut, parent: _snakeController); |
| 41 | + } |
| 42 | + |
| 43 | + void _gameStart() { |
| 44 | + Timer.periodic(Duration(milliseconds: 250), (Timer timer) { |
| 45 | + _updateSnake(); |
| 46 | + if(_hasStarted) timer.cancel(); |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + bool _gameOver() { |
| 51 | + for (int i = 0; i < _snake.length - 1; i++) if (_snake.last == _snake[i]) return true; |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + void _updateSnake() { |
| 56 | + if(!_hasStarted) { |
| 57 | + setState(() { |
| 58 | + _playerScore = (_snake.length - 4) * 100; |
| 59 | + switch (_currentSnakeDirection) { |
| 60 | + case 'DOWN': |
| 61 | + if (_snake.last > _noOfSquares) _snake.add(_snake.last + _squareSize - (_noOfSquares + _squareSize)); |
| 62 | + else _snake.add(_snake.last + _squareSize); |
| 63 | + break; |
| 64 | + case 'UP': |
| 65 | + if (_snake.last < _squareSize) _snake.add(_snake.last - _squareSize + (_noOfSquares + _squareSize)); |
| 66 | + else _snake.add(_snake.last - _squareSize); |
| 67 | + break; |
| 68 | + case 'RIGHT': |
| 69 | + if ((_snake.last + 1) % _squareSize == 0) _snake.add(_snake.last + 1 - _squareSize); |
| 70 | + else _snake.add(_snake.last + 1); |
| 71 | + break; |
| 72 | + case 'LEFT': |
| 73 | + if ((_snake.last) % _squareSize == 0) _snake.add(_snake.last - 1 + _squareSize); |
| 74 | + else _snake.add(_snake.last - 1); |
| 75 | + } |
| 76 | + |
| 77 | + if (_snake.last != _snakeFoodPosition) _snake.removeAt(0); |
| 78 | + else { |
| 79 | + do { |
| 80 | + _snakeFoodPosition = _random.nextInt(_noOfSquares); |
| 81 | + } while (_snake.contains(_snakeFoodPosition)); |
| 82 | + } |
| 83 | + |
| 84 | + if (_gameOver()) { |
| 85 | + setState(() { |
| 86 | + _hasStarted = !_hasStarted; |
| 87 | + }); |
| 88 | + Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context) => GameOver(score: _playerScore))); |
| 89 | + } |
| 90 | + }); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @override |
| 95 | + Widget build(BuildContext context) { |
| 96 | + return Scaffold( |
| 97 | + appBar: AppBar( |
| 98 | + title: Text('SnakeGameFlutter', style: TextStyle(color: Colors.white, fontSize: 20.0)), |
| 99 | + centerTitle: false, |
| 100 | + backgroundColor: Colors.redAccent, |
| 101 | + actions: <Widget>[ |
| 102 | + Center( |
| 103 | + child: Padding( |
| 104 | + padding: const EdgeInsets.symmetric(horizontal: 20.0), |
| 105 | + child: Text('Score: $_playerScore', style: TextStyle(fontSize: 16.0)), |
| 106 | + ) |
| 107 | + ) |
| 108 | + ], |
| 109 | + ), |
| 110 | + floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat, |
| 111 | + floatingActionButton: FloatingActionButton.extended( |
| 112 | + backgroundColor: Colors.redAccent, |
| 113 | + elevation: 20, |
| 114 | + label: Text( |
| 115 | + _hasStarted ? 'Start' : 'Pause', |
| 116 | + style: TextStyle(), |
| 117 | + ), |
| 118 | + onPressed: () { |
| 119 | + setState(() { |
| 120 | + if(_hasStarted) _snakeController.forward(); |
| 121 | + else _snakeController.reverse(); |
| 122 | + _hasStarted = !_hasStarted; |
| 123 | + _gameStart(); |
| 124 | + }); |
| 125 | + }, |
| 126 | + icon: AnimatedIcon(icon: AnimatedIcons.play_pause, progress: _snakeAnimation) |
| 127 | + ), |
| 128 | + body: Center( |
| 129 | + child: GestureDetector( |
| 130 | + onVerticalDragUpdate: (drag) { |
| 131 | + if (drag.delta.dy > 0 && _currentSnakeDirection != 'UP') _currentSnakeDirection = 'DOWN'; |
| 132 | + else if (drag.delta.dy < 0 && _currentSnakeDirection != 'DOWN') _currentSnakeDirection = 'UP'; |
| 133 | + }, |
| 134 | + onHorizontalDragUpdate: (drag) { |
| 135 | + if (drag.delta.dx > 0 && _currentSnakeDirection != 'LEFT') _currentSnakeDirection = 'RIGHT'; |
| 136 | + else if (drag.delta.dx < 0 && _currentSnakeDirection != 'RIGHT') _currentSnakeDirection = 'LEFT'; |
| 137 | + }, |
| 138 | + child: Container( |
| 139 | + width: MediaQuery.of(context).size.width, |
| 140 | + child: GridView.builder( |
| 141 | + itemCount: _squareSize + _noOfSquares, |
| 142 | + physics: NeverScrollableScrollPhysics(), |
| 143 | + gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: _squareSize), |
| 144 | + itemBuilder: (BuildContext context, int index) { |
| 145 | + return Center( |
| 146 | + child: Container( |
| 147 | + color: Colors.white, |
| 148 | + padding: _snake.contains(index) ? EdgeInsets.all(1) : EdgeInsets.all(0), |
| 149 | + child: ClipRRect( |
| 150 | + borderRadius: index == _snakeFoodPosition || index == _snake.last ? BorderRadius.circular(7) : _snake.contains(index) ? BorderRadius.circular(2.5) : BorderRadius.circular(1), |
| 151 | + child: Container( |
| 152 | + color: _snake.contains(index) ? Colors.black : index == _snakeFoodPosition ? Colors.green : Colors.blue |
| 153 | + ), |
| 154 | + ), |
| 155 | + ), |
| 156 | + ); |
| 157 | + }, |
| 158 | + ), |
| 159 | + ), |
| 160 | + ), |
| 161 | + ), |
| 162 | + ); |
| 163 | + } |
| 164 | +} |
0 commit comments