Skip to content

Commit 73af743

Browse files
committed
Add scrolling background, add spawnrate difficulty curve
1 parent cdb336c commit 73af743

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

public/assets/gfx/bg.png

237 Bytes
Loading

src/game/createSpriteAssets.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ export class SpriteAssets {
5050
frameHeight: 64,
5151
});
5252
}
53+
if (!scene.textures.exists("bg")) {
54+
scene.load.spritesheet("bg", "./assets/gfx/bg.png", {
55+
frameWidth: 16,
56+
frameHeight: 16,
57+
});
58+
}
5359
}
5460

5561
static createSprites(scene: Phaser.Scene): void {
@@ -113,5 +119,16 @@ export class SpriteAssets {
113119
repeat: -1,
114120
});
115121
}
122+
if (!scene.anims.exists("bg")) {
123+
scene.anims.create({
124+
key: "bg",
125+
frames: scene.anims.generateFrameNumbers("bg", {
126+
start: 0,
127+
end: 0,
128+
}),
129+
frameRate: 6,
130+
repeat: -1,
131+
});
132+
}
116133
}
117134
}

src/game/scenes/GameScene.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,18 @@ export class GameScene extends Phaser.Scene {
6363
const gameWidth = this.cameras.main.width;
6464
const gameHeight = this.cameras.main.height;
6565

66-
// Set up background to fill the screen
67-
this.background = this.add
68-
.tileSprite(0, 0, gameWidth, gameHeight, "background")
69-
.setOrigin(0, 0)
70-
.setScrollFactor(0);
71-
7266
// Update physics settings for framerate independence
7367
this.physics.world.setFPS(60);
7468

7569
// Load sprites from asset handler
7670
SpriteAssets.createSprites(this);
7771

72+
// Set up background to fill the screen
73+
this.background = this.add
74+
.tileSprite(0, 0, gameWidth, gameHeight, "bg")
75+
.setOrigin(0, 0)
76+
.setScrollFactor(0);
77+
7878
document.fonts.load('24px "Monogram"').then(() => {
7979
this.scoreText = this.add
8080
.text(8, 0, `${this.score}`, {
@@ -116,14 +116,17 @@ export class GameScene extends Phaser.Scene {
116116
// Convert delta to seconds for easier calculations
117117
const deltaSeconds = delta / 1000;
118118

119+
this.background.tilePositionY -= delta * 0.01;
120+
119121
// The A key moves from center to the left of the screen
120122
// The D key moves from center to the right of the screen
121123
// Update position according to horizontal
122124
this.player.update(this.horizontal);
123125

124126
this.obstacleTimer += delta;
125127

126-
if (this.obstacleTimer > 1000) {
128+
// difficulty curve
129+
if (this.obstacleTimer > Math.max(300, 1000 * Math.pow(0.97, this.score))) {
127130
this.spawnObstacle();
128131
this.obstacleTimer = 0;
129132
}

0 commit comments

Comments
 (0)