|
|
当然可以!把游戏的背景替换成一张星空图片会让游戏变得更酷。你只需要在画布上绘制这张图片作为背景即可。下面是详细的步骤:
### 第一步:准备星空图片
首先,请确保你有一张星空图片,并将其保存到与你的 `index.html` 文件同一目录下,比如命名为 `background.jpg`。
### 第二步:修改代码
接着,你需要在 JavaScript 中加载并绘制这张图片。这里是完整的修改步骤:
1. **加载星空图片**:在你的脚本文件顶部,添加代码来加载背景图片。
2. **绘制背景图片**:在每一帧更新时绘制背景图片。
### 修改后的 JavaScript(script.js):
- const canvas = document.getElementById('gameCanvas');
- const ctx = canvas.getContext('2d');
- canvas.width = 600; // 设置画布宽度
- canvas.height = 600; // 设置画布高度
- const playerWidth = 50;
- const playerHeight = 50;
- let playerX = (canvas.width - playerWidth) / 2;
- let playerY = canvas.height - playerHeight - 10;
- const playerSpeed = 7;
- let bullets = [];
- let enemies = [];
- let score = 0;
- // 加载飞机图片
- const playerImage = new Image();
- playerImage.src = 'player.png';
- // 加载背景图片
- const backgroundImage = new Image();
- backgroundImage.src = 'background.jpg';
- document.addEventListener('keydown', movePlayer);
- document.addEventListener('keydown', shootBullet);
- function movePlayer(e) {
- if (e.key === 'ArrowLeft' && playerX > 0) {
- playerX -= playerSpeed;
- }
- if (e.key === 'ArrowRight' && playerX < canvas.width - playerWidth) {
- playerX += playerSpeed;
- }
- }
- function shootBullet(e) {
- if (e.key === ' ') {
- bullets.push({ x: playerX + playerWidth / 2 - 2.5, y: playerY, speed: 10 });
- }
- }
- function drawPlayer() {
- const degrees = 45; // 旋转角度
- const radians = degrees * (Math.PI / 180); // 将角度转换为弧度
- ctx.save(); // 保存当前画布状态
- ctx.translate(playerX + playerWidth / 2, playerY + playerHeight / 2); // 将原点移动到图像中心
- ctx.rotate(radians); // 旋转画布
- ctx.drawImage(playerImage, -playerWidth / 2, -playerHeight / 2, playerWidth, playerHeight); // 绘制图像
- ctx.restore(); // 恢复画布到之前保存的状态
- }
- function drawBullets() {
- ctx.fillStyle = '#ff0';
- bullets.forEach((bullet, index) => {
- bullet.y -= bullet.speed;
- ctx.fillRect(bullet.x, bullet.y, 5, 15);
- if (bullet.y < 0) {
- bullets.splice(index, 1);
- }
- });
- }
- function drawEnemies() {
- ctx.fillStyle = '#f00';
- enemies.forEach((enemy, index) => {
- enemy.y += enemy.speed;
- ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
- if (enemy.y > canvas.height) {
- enemies.splice(index, 1);
- gameOver();
- }
- });
- }
- function generateEnemies() {
- // 调整生成敌人的概率
- if (Math.random() < 0.01) { // 将概率从 0.02 降低到 0.01
- const x = Math.random() * (canvas.width - playerWidth);
- enemies.push({ x: x, y: 0, width: 50, height: 50, speed: 3 });
- }
- }
- function detectCollisions() {
- bullets.forEach((bullet, bIndex) => {
- enemies.forEach((enemy, eIndex) => {
- if (bullet.x > enemy.x && bullet.x < enemy.x + enemy.width &&
- bullet.y > enemy.y && bullet.y < enemy.y + enemy.height) {
- score += 10;
- bullets.splice(bIndex, 1);
- enemies.splice(eIndex, 1);
- }
- });
- });
- enemies.forEach(enemy => {
- if (playerX < enemy.x + enemy.width &&
- playerX + playerWidth > enemy.x &&
- playerY < enemy.y + enemy.height &&
- playerY + playerHeight > enemy.y) {
- gameOver();
- }
- });
- }
- function gameOver() {
- alert(`Game Over! Your score is ${score}`);
- document.location.reload();
- }
- function drawBackground() {
- ctx.drawImage(backgroundImage, 0, 0, canvas.width, canvas.height);
- }
- function update() {
- ctx.clearRect(0, 0, canvas.width, canvas.height);
- drawBackground(); // 绘制背景
- drawPlayer();
- drawBullets();
- drawEnemies();
- detectCollisions();
- generateEnemies();
- requestAnimationFrame(update);
- }
- update();
复制代码
### 解释:
1. **加载背景图片**:
- const backgroundImage = new Image();
- backgroundImage.src = 'background.jpg';
复制代码
2. **绘制背景图片**:在 `update` 函数中添加 `drawBackground` 函数来绘制背景图片。
- function drawBackground() {
- ctx.drawImage(backgroundImage, 0, 0, canvas.width, canvas.height);
- }
复制代码
3. **在每一帧更新时绘制背景图片**:在 `update` 函数中调用 `drawBackground` 函数。
- function update() {
- ctx.clearRect(0, 0, canvas.width, canvas.height);
- drawBackground(); // 绘制背景
- drawPlayer();
- drawBullets();
- drawEnemies();
- detectCollisions();
- generateEnemies();
- requestAnimationFrame(update);
- }
复制代码
这样,你的游戏就会有一个漂亮的星空背景啦!如果还有其他问题或者需要进一步的帮助,随时告诉我哦! |
|