Skip to content

Instantly share code, notes, and snippets.

View Dev-Owl's full-sized avatar
🦉

Christian Dev-Owl

🦉
View GitHub Profile
@Dev-Owl
Dev-Owl / main_part1.dart
Last active October 13, 2019 15:20
Main Widget Part 1
final _game = GameOfLife();
Size _cellSize;
@override
void initState() {
super.initState();
//Fullscreen display (still including appbar)
SystemChrome.setEnabledSystemUIOverlays([]);
_game.resetWorld();
}
@Dev-Owl
Dev-Owl / main_part2.dart
Created October 13, 2019 15:29
Main widget part 2
@override
Widget build(BuildContext contGameOfLifeext) {
final screenSize = MediaQuery.of(context).size;
_cellSize = Size(
screenSize.width / GameOfLife.rowLength,
(screenSize.height - kToolbarHeight) /
(GameOfLife.worldSize / GameOfLife.rowLength));
return Scaffold(
appBar: AppBar(
@Dev-Owl
Dev-Owl / gameOfLive.dart
Created October 17, 2019 11:02
Game of life - logic class
import 'dart:async';
import 'dart:math';
class GameOfLife {
static const worldSize = 1024;
static const rowLength = 32;
static const cellMargin = 1;
final _world = List<bool>(worldSize);
final StreamController<double> _stateController = StreamController<double>();
@Dev-Owl
Dev-Owl / gameOfLive.dart
Created October 17, 2019 11:06
Game of life - reset world
static const worldSize = 1024;
static const rowLength = 32;
final _world = List<bool>(worldSize);
void resetWorld() {
final random = Random();
_world.fillRange(0, worldSize, false);
final totalSets = random.nextInt(((worldSize / 100) * 50).round());
@Dev-Owl
Dev-Owl / gameOfLive.dart
Created October 17, 2019 11:20
Game of life - runTheGame
Future<void> _runTheGame() async {
while (_running) {
await Future.delayed(Duration(milliseconds: 350));
var newWorld = List<bool>.from(_world);
for (var i = 0; i < worldSize; ++i) {
//In case x cell alive do this
var livingNeighbors = _countLivingCellsNearby(i);
if (livingNeighbors < 2 || livingNeighbors > 3) {
//die
newWorld[i] = false;
@Dev-Owl
Dev-Owl / cell.dart
Created October 17, 2019 11:47
Game of life - cell drawing
import 'package:flutter/material.dart';
class CellPainter extends CustomPainter {
bool cellIsAlive = false;
double left;
double top;
final Paint paintSetting = Paint();
CellPainter({this.cellIsAlive,this.left,this.top}){
paintSetting.strokeWidth = 1;
@Dev-Owl
Dev-Owl / board.dart
Created October 17, 2019 12:03
Game of life - draw the board
import 'package:flutter/material.dart';
import 'package:gameoflife/cell.dart';
class GameBoard extends StatelessWidget {
final List<bool> _world;
final Size cellSize;
final int _rowLength;
final int _cellMargin;
const GameBoard(this._cellMargin,this._world, this._rowLength, {Key key, this.cellSize})
@Dev-Owl
Dev-Owl / main.dart
Created November 24, 2019 14:31
Main file for our mazeball
import 'package:flame/util.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mazeball/game.dart';
import 'dart:async';
void main() async {
//Make sure flame is ready before we launch our game
await setupFlame();
var game = new MazeBallGame();
@Dev-Owl
Dev-Owl / game.dart
Created November 24, 2019 14:47
Game with flame, box2d
import 'dart:ui';
import 'package:box2d_flame/box2d.dart';
import 'package:flame/flame.dart';
import 'package:flame/game.dart';
class MazeBallGame extends Game {
//Needed for Box2D
static const int WORLD_POOL_SIZE = 100;
static const int WORLD_POOL_CONTAINER_SIZE = 10;
//Main physic object -> our game world
@Dev-Owl
Dev-Owl / ball.dart
Created December 1, 2019 13:00
Ball class for mazeball game
import 'dart:ui';
import 'package:box2d_flame/box2d.dart';
import 'package:mazeball/game.dart';
import 'package:sensors/sensors.dart';
class Ball {
final MazeBallGame game;
//Physic objects
Body body;
CircleShape shape;