gamez 0.0.3+1 copy "gamez: ^0.0.3+1" to clipboard
gamez: ^0.0.3+1 copied to clipboard

A small 2D game engine to easily create games in Flutter by providing different methods for rendering, gesture detection, etc.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:gamez/gamez.dart';

/// This example is to demonstrate the methods of updating, rendering and
/// gestures.
///
/// It renders a circle that moves horizontally and bounces if it collides with
/// the edges of the screen.
/// Also, if you tap it, it will change its color.

void main() => runApp(
      MaterialApp(
        debugShowCheckedModeBanner: false,
        home: MyGameWidget(),
      ),
    );

class MyCharacter extends CircularEntity {
  final color1 = Colors.red;
  final color2 = Colors.green;

  MyCharacter(Vector position) : super(position, 50) {
    color = color1;
    velocity.x = 150;
  }

  @override
  void update(double dt) {
    position.x += velocity.x * dt;
  }

  @override
  void handleGesture(Offset position, Gesture gesture) {
    if (gesture == Gesture.tapDown && contains(position)) {
      color = color == color1 ? color2 : color1;
    }
  }

  @override
  void reset() {}
}

class MyGame extends Game {
  late MyCharacter myCharacter;

  MyGame() {
    myCharacter = MyCharacter(Vector(size.width / 2, size.height / 2));
    addEntity(myCharacter);
  }

  @override
  void render(Canvas canvas, Size size) {
    // draw a background
    final paint = Paint()..color = Color.fromRGBO(100, 91, 125, 1);
    canvas.drawRect(Offset.zero & size, paint);

    // render all children.
    super.render(canvas, size);
  }

  @override
  void update(double dt) {
    super.update(dt);

    /// Bouncing
    if (myCharacter.x + myCharacter.radius > size.width ||
        myCharacter.x - myCharacter.radius < 0) {
      myCharacter.velocity.x *= -1;
    }
  }
}

// ignore: must_be_immutable
class MyGameWidget extends GameWidget {
  MyGameWidget() : super(game: MyGame());
}
4
likes
130
pub points
0%
popularity

Publisher

unverified uploader

A small 2D game engine to easily create games in Flutter by providing different methods for rendering, gesture detection, etc.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter, path_provider, provider

More

Packages that depend on gamez