feat(game): add drink collection mechanics and score tracking

- Added 'drink' image asset and loaded it in preload
- Introduced drink collection logic with scoring system
- Implemented overlap detection between player and drink items
- Added score display that updates with collected drinks and earned points
- Modified platform positioning and added physics interactions
This commit is contained in:
Brian Fertig 2025-07-13 17:12:19 -06:00
parent b8e865fe05
commit 5de7951bdc
3 changed files with 38 additions and 6 deletions

BIN
assets/drink.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB

BIN
assets/original/drink.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 894 KiB

44
game.js
View File

@ -28,6 +28,7 @@ function preload() {
this.load.image('bricks', 'assets/bricks.png');
this.load.image('pee', 'assets/pee_test.png');
this.load.image('clay', 'assets/platform_clay.png');
this.load.image('drink', 'assets/drink.png');
// //Create an animation for walking left
// this.anims.create({
@ -39,7 +40,7 @@ function preload() {
// });
}
let player, cursors, ground, platforms;
let player, cursors, ground, platforms, drinks=0, score = 0, scoreText;
function create() {
// Add background image
@ -60,7 +61,8 @@ function create() {
// Add Elevators
//let elevators = this.physics.add.staticGroup();
let elevators = this.physics.add.image(400, 150, 'clay').setScale(.3).setOffset(0, 30);
let elevators = this.physics.add.image(400, 200, 'clay')
.setScale(.3);
// Add Platforms
platforms.create(1200, 500, 'ground')
@ -74,9 +76,8 @@ function create() {
.setSize(1296/2, 50)
.setOffset(0, 25);
// Decorations
decorations = this.physics.add.staticGroup();
decorations.create(100, 250, 'pee')
// pee
peeTest = this.physics.add.image(100, 200, 'pee')
.setScale(.10)
.refreshBody();
@ -91,9 +92,40 @@ function create() {
// Add collision and sound
this.physics.add.collider(player, platforms);
//this.physics.add.collider(player, elevators);
this.physics.add.collider(player, elevators);
//this.sound.add('jump', { volume: 0.5 });
stars = this.physics.add.group({
key: 'drink',
repeat: 13,
setXY: { x: 200, y: 0, stepX: 70 }
});
stars.children.iterate(function (child) {
child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8));
});
this.physics.add.collider(stars, platforms);
this.physics.add.collider(peeTest, platforms);
this.physics.add.overlap(player, stars, collectDrink, null, this);
this.physics.add.overlap(player, peeTest, collectPee, null, this);
function collectDrink (player, star) {
star.disableBody(true, true);
drinks += 1;
scoreText.setText('Drinks: ' + drinks + ' Score: ' + score);
}
function collectPee (player, peeTest) {
score += drinks * 10;
drinks = 0;
scoreText.setText('Drinks: ' + drinks + ' Score: ' + score);
}
scoreText = this.add.text(16, 16, 'Drinks: 0 Score: 0', { fontSize: '32px', fill: '#000' });
// Enable camera follow
this.cameras.main.startFollow(player, true, 0, 0.1);