Skip to content

Instantly share code, notes, and snippets.

View AlabasterAxe's full-sized avatar
🍊
<= Look at this Orange

Matthew Keller AlabasterAxe

🍊
<= Look at this Orange
View GitHub Profile
@AlabasterAxe
AlabasterAxe / main.dart
Created November 10, 2020 00:14
Adding new cacti to the world when another disappears off the left edge of the screen.
Rect dinoRect = dino.getRect(screenSize, runDistance).deflate(15);
for (Cactus obstacle in obstacles) {
Rect obstacleRect = obstacle.getRect(screenSize, runDistance);
if (dinoRect.overlaps(obstacleRect.deflate(15))) {
_die();
}
if (obstacleRect.right < 0) {
setState(() {
obstacles.remove(obstacle);
obstacles.add(Cactus(
@AlabasterAxe
AlabasterAxe / main.dart
Created November 10, 2020 00:12
the main implementation of _die() for Flutter Dino Game
void _die() {
setState(() {
worldController.stop();
dino.die();
});
}
@AlabasterAxe
AlabasterAxe / dino.dart
Last active November 10, 2020 00:12
The Dinosaur's implementation of die()
void die() {
state = DinoState.dead;
frame = 6;
}
@AlabasterAxe
AlabasterAxe / main.dart
Created November 10, 2020 00:10
Performing Collision Checks for the Flutter Dino Game
Size screenSize = MediaQuery.of(context).size;
Rect dinoRect = dino.getRect(screenSize, runDistance).deflate(15);
for (Cactus obstacle in obstacles) {
Rect obstacleRect = obstacle.getRect(screenSize, runDistance);
if (dinoRect.overlaps(obstacleRect.deflate(15))) {
_die();
}
}
@AlabasterAxe
AlabasterAxe / main.dart
Created November 10, 2020 00:09
Maintaining the run distance for the Flutter Dino Game implementation
void _update() {
double elapsedSeconds =
((worldController.lastElapsedDuration.inMilliseconds -
lastUpdateCall.inMilliseconds) /
1000);
runDistance = max(runDistance + runSpeed elapsedSeconds, 0);
runSpeed += RUN_SPEED_ACC_PPSPS elapsedSeconds;
dino.update(lastUpdateCall, worldController.lastElapsedDuration);
lastUpdateCall = worldController.lastElapsedDuration
}
@AlabasterAxe
AlabasterAxe / main.dart
Created November 10, 2020 00:08
Drawing of all the GameObjects to the screen
for (GameObject object in [...obstacles, dino]) {
children.add(
AnimatedBuilder(
animation: worldController,
builder: (context, child) {
Rect objectRect = object.getRect(screenSize, runDistance);
return Positioned(
top: objectRect.top,
left: objectRect.left,
width: objectRect.width,
@AlabasterAxe
AlabasterAxe / main.dart
Created November 10, 2020 00:07
Initializing the obstacles for the Flutter Dino Game
List<Cactus> obstacles = [
Cactus(location: Offset(200, 0)),
];
@AlabasterAxe
AlabasterAxe / cactus.dart
Created November 10, 2020 00:06
Calculation for the position of the left edge for the Cactus GameObject Rect
(location.dx - runDistance) * WORLD_TO_PIXEL_RATIO
@AlabasterAxe
AlabasterAxe / cactus.dart
Created November 10, 2020 00:04
Implementation of a Cactus GameObject for Flutter implementation of Google Chrome dinosaur game
class Cactus extends GameObject {
// This is the location of the cactus in "world" coordinates which is translated to "screen" coordinates
final Offset worldLocation;
final Sprite sprite;
Cactus({this.location}) : sprite = CACTI[Random().nextInt(CACTI.length)];
@override
Rect getRect(Size screenSize, double runDistance) {
return Rect.fromLTWH(
@AlabasterAxe
AlabasterAxe / dino.dart
Created November 10, 2020 00:03
Checking if the dinosaur is already jumping to disallow double jumping
void jump() {
if (state != DinoState.jumping) {
velY = 650;
}
}