From 6cd8c7c284f2ea43476bee2945d3ab9f9c643815 Mon Sep 17 00:00:00 2001 From: tiennm99 Date: Wed, 14 May 2025 18:10:57 +0700 Subject: [PATCH] feat: init --- cline/index.html | 11 ++++++ cline/package.json | 18 +++++++++ cline/src/main.js | 97 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 cline/index.html create mode 100644 cline/package.json create mode 100644 cline/src/main.js diff --git a/cline/index.html b/cline/index.html new file mode 100644 index 0000000..9525b91 --- /dev/null +++ b/cline/index.html @@ -0,0 +1,11 @@ + + + + + Phaser Project + + + + + + diff --git a/cline/package.json b/cline/package.json new file mode 100644 index 0000000..de04ee9 --- /dev/null +++ b/cline/package.json @@ -0,0 +1,18 @@ +{ + "name": "phaser-project", + "version": "1.0.0", + "description": "Phaser 3 TypeScript Project", + "main": "src/main.js", + "scripts": { + "start": "webpack serve --open", + "build": "webpack --mode production" + }, + "devDependencies": { + "webpack": "^5.76.3", + "webpack-cli": "^5.1.4", + "webpack-dev-server": "^4.13.3", + "babel-loader": "^9.1.2", + "@babel/core": "^7.22.11", + "@babel/preset-env": "^7.22.10" + } +} diff --git a/cline/src/main.js b/cline/src/main.js new file mode 100644 index 0000000..44c343a --- /dev/null +++ b/cline/src/main.js @@ -0,0 +1,97 @@ +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)); + }); +}