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 / 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;
}
}
@AlabasterAxe
AlabasterAxe / dino.dart
Created November 10, 2020 00:02
The DinoState Enum
enum DinoState {
running,
jumping,
}
@AlabasterAxe
AlabasterAxe / main.dart
Created November 10, 2020 00:02
Listen for taps to trigger the dinosaur's jump.
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
dino.jump();
},
child: Stack(
@AlabasterAxe
AlabasterAxe / dino.dart
Created November 10, 2020 00:01
Actually Implement the Dino's Jump
void jump() {
velY = 650;
}
@AlabasterAxe
AlabasterAxe / dino.dart
Created November 10, 2020 00:00
Updating the dinosaur's displacement and velocity in update()
@override
void update(Duration lastTime, Duration elapsedTime) {
double elapsedSeconds =
((elapsedTime.inMilliseconds - lastTime.inMilliseconds) / 1000);
dispY += velY * elapsedSeconds;
if (dispY <= 0) {
dispY = 0;
velY = 0;
} else {
@AlabasterAxe
AlabasterAxe / dino.dart
Created November 9, 2020 23:59
Updated getRect implementation for the Dino GameObject
@override
Rect getRect(Size screenSize, double _) {
return Rect.fromLTWH(
screenSize.width / 10,
4 / 7 * screenSize.height - dino.imageHeight - dispY,
dino.imageWidth.toDouble(),
dino.imageHeight.toDouble());
}
@AlabasterAxe
AlabasterAxe / main.dart
Created November 9, 2020 23:57
Animated rendering code for our dino.
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Stack(
alignment: Alignment.center,
children: [
AnimatedBuilder(
animation: worldController,
builder: (context, child) {
@AlabasterAxe
AlabasterAxe / main.dart
Created November 9, 2020 23:56
Starting our game animation.
@override
void initState() {
super.initState();
worldController =
AnimationController(vsync: this, duration: Duration(days: 99));
worldController.addListener(_update);
worldController.forward();
}
@AlabasterAxe
AlabasterAxe / dino.dart
Created November 9, 2020 23:55
Adding a run animation to our dinosaur.
class Dino extends GameObject {
int frame = 1;
@override
Widget render() {
return Image.asset(
"assets/images/dino/dino_$frame.png",
gaplessPlayback: true,
);
}
@AlabasterAxe
AlabasterAxe / main.dart
Created November 9, 2020 23:54
registering our update call as a listener to our worldController
Duration lastUpdateCall = Duration();
@override
void initState() {
super.initState();
worldController =
AnimationController(vsync: this, duration: Duration(days: 99));
worldController.addListener(_update);
}