|
|
好的!我会将你的网页文本改为英文。
### 1. HTML 部分
我们将文本和按钮等内容翻译成英文:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Snake Game</title>
- <link rel="stylesheet" href="styles.css">
- </head>
- <body>
- <h1>Snake Game</h1>
- <button id="startButton">Start Game</button>
- <canvas id="gameCanvas" width="400" height="400"></canvas>
- <div id="score">Score: 0</div>
- <script src="snake_game.js"></script>
- </body>
- </html>
复制代码
### 2. JavaScript 部分
我们不需要对逻辑部分做大改动,只需要确保提示信息是英文的。例如,游戏结束时的提示:
- // 获取画布并设置上下文
- const canvas = document.getElementById('gameCanvas');
- const ctx = canvas.getContext('2d');
- // 定义蛇的初始状态
- let snake = [
- {x: 150, y: 150},
- {x: 140, y: 150},
- {x: 130, y: 150},
- {x: 120, y: 150},
- {x: 110, y: 150}
- ];
- // 初始化食物位置
- let foodX;
- let foodY;
- // 初始速度 - 蛇向右移动
- let dx = 10;
- let dy = 0;
- // 游戏运行标志
- let isGameRunning = true;
- // 创建游戏循环
- function main() {
- if (isGameRunning) {
- setTimeout(function onTick() {
- clearCanvas();
- moveSnake();
- drawSnake();
- drawFood();
- checkGameOver();
- main();
- }, 100);
- } else {
- alert("Game Over!");
- }
- }
- // 清除画布
- function clearCanvas() {
- ctx.fillStyle = "black";
- ctx.fillRect(0, 0, canvas.width, canvas.height);
- }
- // 绘制蛇
- function drawSnake() {
- snake.forEach(snakePart => {
- ctx.fillStyle = 'lightgreen';
- ctx.strokestyle = 'darkgreen';
- ctx.fillRect(snakePart.x, snakePart.y, 10, 10);
- ctx.strokeRect(snakePart.x, snakePart.y, 10, 10);
- });
- }
- // 移动蛇
- function moveSnake() {
- const head = {x: snake[0].x + dx, y: snake[0].y + dy};
- snake.unshift(head);
- // 检查蛇是否吃到食物
- if (snake[0].x === foodX && snake[0].y === foodY) {
- createFood();
- } else {
- snake.pop();
- }
- }
- // 随机生成食物位置
- function createFood() {
- foodX = Math.floor((Math.random() * canvas.width) / 10) * 10;
- foodY = Math.floor((Math.random() * canvas.height) / 10) * 10;
- // 确保食物不会生成在蛇身体上
- snake.forEach(snakePart => {
- if (snakePart.x === foodX && snakePart.y === foodY) {
- createFood();
- }
- });
- }
- // 绘制食物
- function drawFood() {
- ctx.fillStyle = 'red';
- ctx.strokestyle = 'darkred';
- ctx.fillRect(foodX, foodY, 10, 10);
- ctx.strokeRect(foodX, foodY, 10, 10);
- }
- // 检查游戏是否结束
- function checkGameOver() {
- for (let i = 4; i < snake.length; i++) {
- if (snake[0].x === snake[i].x && snake[0].y === snake[i].y) {
- isGameRunning = false;
- }
- }
- const hitLeftWall = snake[0].x < 0;
- const hitRightWall = snake[0].x >= canvas.width;
- const hitTopWall = snake[0].y < 0;
- const hitBottomWall = snake[0].y >= canvas.height;
- if (hitLeftWall || hitRightWall || hitTopWall || hitBottomWall) {
- isGameRunning = false;
- }
- }
- // 更改方向
- document.addEventListener("keydown", changeDirection);
- function changeDirection(event) {
- const LEFT_KEY = 37;
- const RIGHT_KEY = 39;
- const UP_KEY = 38;
- const DOWN_KEY = 40;
- const keyPressed = event.keyCode;
- const goingUp = dy === -10;
- const goingDown = dy === 10;
- const goingRight = dx === 10;
- const goingLeft = dx === -10;
- if (keyPressed === LEFT_KEY && !goingRight) {
- dx = -10;
- dy = 0;
- }
- if (keyPressed === UP_KEY && !goingDown) {
- dx = 0;
- dy = -10;
- }
- if (keyPressed === RIGHT_KEY && !goingLeft) {
- dx = 10;
- dy = 0;
- }
- if (keyPressed === DOWN_KEY && !goingUp) {
- dx = 0;
- dy = 10;
- }
- }
- // 开始游戏
- createFood();
- main();
复制代码
### 3. CSS 部分
我们之前已经修改了背景颜色,这里再重复一下,以确保你有完整的代码:
- /* 设置画布和页面样式 */
- canvas {
- background-color: #ADD8E6; /* 浅蓝色背景 */
- display: block; /* 将画布显示为块状元素 */
- margin: 0 auto; /* 居中对齐 */
- }
- /* 添加基本页面样式 */
- body {
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- height: 100vh;
- margin: 0;
- }
- h1 {
- color: #333;
- }
- #startButton {
- padding: 10px 20px;
- font-size: 16px;
- margin-bottom: 20px;
- background-color: #0000FF; /* 蓝色背景 */
- color: white; /* 白色文字 */
- border: none; /* 无边框 */
- border-radius: 5px; /* 圆角边框 */
- cursor: pointer; /* 鼠标悬停变成指针 */
- }
- #startButton:hover {
- background-color: #0000cc; /* 鼠标悬停时颜色变深 */
- }
- #score {
- margin-top: 20px;
- font-size: 20px;
- color: #333;
- }
复制代码
以上就是将生成的网页文本都改为英文后的代码。如果你还有其他的问题或者需要进一步的帮助,请告诉我! |
|