mirror of
https://github.com/tiennm99/ai-coding-workflow-labs.git
synced 2026-09-17 08:20:37 +00:00
98 lines
2.1 KiB
JavaScript
98 lines
2.1 KiB
JavaScript
const config = {
|
|
type: Phaser.AUTO,
|
|
width: 800,
|
|
height: 600,
|
|
physics: {
|
|
default: 'arcade',
|
|
arcade: {
|
|
gravity: { y: 1000 },
|
|
debug: false
|
|
}
|
|
},
|
|
scene: {
|
|
preload: preload,
|
|
create: create,
|
|
update: update
|
|
}
|
|
};
|
|
|
|
let bird;
|
|
let pipes;
|
|
let score = 0;
|
|
let gameOver = false;
|
|
let scoreText;
|
|
|
|
const game = new Phaser.Game(config);
|
|
|
|
function preload() {
|
|
// Placeholder assets - using Phaser's graphics API instead of loading images
|
|
}
|
|
|
|
function create() {
|
|
// Create bird
|
|
bird = this.add.circle(100, 250, 15, 0xffffff);
|
|
this.physics.add.existing(bird);
|
|
bird.body.velocity.y = -300;
|
|
|
|
// Create pipes
|
|
pipes = this.physics.add.group();
|
|
|
|
// Create score text
|
|
scoreText = this.add.text(16, 16, 'Score: 0', { fontSize: '32px', fill: '#fff' });
|
|
|
|
// Input
|
|
this.input.keyboard.on('keydown-SPACE', () => {
|
|
if (gameOver) {
|
|
restartGame();
|
|
} else {
|
|
bird.body.velocity.y = -300;
|
|
}
|
|
});
|
|
|
|
// Timer for pipe generation
|
|
this.time.addEvent({
|
|
delay: 1500,
|
|
callback: addPipe,
|
|
callbackScope: this,
|
|
loop: true
|
|
});
|
|
}
|
|
|
|
function update() {
|
|
// Check for collisions
|
|
this.physics.overlap(bird, pipes, () => {
|
|
gameOver = true;
|
|
scoreText.setText('Game Over! Score: ' + score);
|
|
});
|
|
|
|
// Move pipes
|
|
pipes.getChildren().forEach(pipe => {
|
|
pipe.x -= 200 * this.game.loop.delta;
|
|
|
|
// Reset pipe position
|
|
if (pipe.x < -50) {
|
|
score++;
|
|
scoreText.setText('Score: ' + score);
|
|
pipe.x = 850;
|
|
pipe.y = Phaser.Math.Between(50, 550);
|
|
}
|
|
});
|
|
}
|
|
|
|
function addPipe() {
|
|
const pipe = this.physics.add.image(850, Phaser.Math.Between(50, 550), 'pipe');
|
|
pipe.setImmovable(true);
|
|
pipes.add(pipe);
|
|
}
|
|
|
|
function restartGame() {
|
|
score = 0;
|
|
gameOver = false;
|
|
scoreText.setText('Score: 0');
|
|
bird.setPosition(100, 250);
|
|
bird.body.velocity.y = -300;
|
|
pipes.getChildren().forEach(pipe => {
|
|
pipe.setPosition(850, Phaser.Math.Between(50, 550));
|
|
});
|
|
}
|