Skip to content

Instantly share code, notes, and snippets.

@kuroodo
Last active August 1, 2022 16:08
Show Gist options
  • Save kuroodo/5820640952dd9281033ba235bffe2067 to your computer and use it in GitHub Desktop.
Save kuroodo/5820640952dd9281033ba235bffe2067 to your computer and use it in GitHub Desktop.
Messing Around; Arrow Movement Game
import 'package:flutter/material.dart';
import 'dart:math';
final Vector2 mapBoundsY = Vector2(3, -3);
final Vector2 mapBoundsX = mapBoundsY;
/*
p = player
e = enemy
# = obstacle
W = win
3 #
2 # # # #
1 # #
0 # p # #
-1 # # #
-2 # e #
-3 e W
-3 -2 -1 0 1 2 3
*/
final Map<Vector2, String> map = {
Vector2(-1, 3): "#",
Vector2(-1, 2): "#",
Vector2(-1, 1): "#",
Vector2(-1, 0): "#",
Vector2(-1, -1): "#",
Vector2(-1, -2): "#",
Vector2(0, -2): "e",
Vector2(1, 2): "#",
Vector2(1, 0): "#",
Vector2(1, -1): "#",
Vector2(1, -2): "#",
Vector2(2, 2): "#",
Vector2(2, -3): "e",
Vector2(3, 2): "#",
Vector2(3, 1): "#",
Vector2(3, 0): "#",
Vector2(3, -1): "#",
Vector2(3, -3): "W",
};
Vector2 player = Vector2.ZERO;
class Vector2 {
static final Vector2 ZERO = Vector2(0, 0);
double x;
double y;
Vector2(this.x, this.y);
Vector2 operator +(Vector2 v) => Vector2(x + v.x, y + v.y);
Vector2 operator -(Vector2 v) => Vector2(x - v.x, y - v.y);
Vector2 operator *(Vector2 v) => Vector2(x * v.x, y * v.y);
Vector2 operator /(Vector2 v) => Vector2(x / v.x, y / v.y);
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
if (other.runtimeType != runtimeType) return false;
return other is Vector2 && other.x == x && other.y == y;
}
Vector2 get normalized {
final double l = length;
return Vector2(x / l, y / l);
}
double get length => sqrt(pow(x, 2) + pow(y, 2));
@override
int get hashCode => hashValues(x, y);
@override
String toString() {
return "($x,$y)";
}
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(brightness: Brightness.dark),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String output = "";
int hp = 3;
bool gameOver = false;
@override
setState(VoidCallback fn) {
super.setState(fn);
if (hp <= 0) {
output = "---GAMEOVER---";
gameOver = true;
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
IconButton(
onPressed: () => move(Vector2(0, 1)),
icon: const Icon(Icons.arrow_circle_up),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
onPressed: () => move(Vector2(-1, 0)),
icon: const Icon(Icons.arrow_circle_left_outlined),
),
const SizedBox(width: 10),
IconButton(
onPressed: () => move(Vector2(1, 0)),
icon: const Icon(Icons.arrow_circle_right_outlined),
),
],
),
IconButton(
onPressed: () => move(Vector2(0, -1)),
icon: const Icon(Icons.arrow_circle_down),
),
Text(output.isEmpty
? "Output: Press a button to move!"
: "Output: $output"),
Text("HP remaining: $hp"),
],
),
);
}
void move(Vector2 movement) {
if (gameOver) return;
Vector2 pos = movement + player;
if (isOutOfBounds(pos)) {
return setState(() {
output = "Move Failed: Out of map bounds!";
});
}
if (map[pos] == "e") {
output = "Move Failed: Enemy - HP -1";
hp -= 1;
} else if (map[pos] == "#") {
output = "Move Failed: Obstacle";
} else if (map[pos] == "W") {
output = "Win!";
gameOver = true;
} else {
output = "Moved succesfully";
player = pos;
}
print("Player pos: $player");
setState(() {});
}
bool isOutOfBounds(Vector2 pos) {
return (pos.x > mapBoundsX.x || pos.x < mapBoundsX.y) ||
(pos.y > mapBoundsY.x || pos.y < mapBoundsY.y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment