|
|
当然可以!以下是一个简单的示例,一个点击按钮来增加得分的小游戏。你可以复制以下代码,并把它们分别保存为index.html、style.css和script.js:
**index.html**
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>简单小游戏</title>
- <link rel="stylesheet" href="style.css">
- </head>
- <body>
- <div class="container">
- <h1>简单小游戏</h1>
- <button id="startBtn">开始游戏</button>
- <p>得分:<span id="score">0</span></p>
- </div>
- <script src="script.js"></script>
- </body>
- </html>
复制代码
**style.css**
- body {
- font-family: Arial, sans-serif;
- text-align: center;
- background-color: #f3f3f3;
- }
- .container {
- margin-top: 100px;
- }
- h1 {
- color: #333;
- }
- button {
- padding: 10px 20px;
- font-size: 16px;
- background-color: #4CAF50;
- color: white;
- border: none;
- cursor: pointer;
- }
- button:hover {
- background-color: #45a049;
- }
- p {
- font-size: 18px;
- color: #333;
- }
复制代码
**script.js**
- let score = 0;
- document.getElementById("startBtn").addEventListener("click", function() {
- score++;
- document.getElementById("score").textContent = score;
- });
复制代码
以上代码创建了一个简单的页面,包括一个标题、一个按钮和一个显示得分的地方。点击按钮可以增加得分。记得把这三个文件放在同一个文件夹内,然后用浏览器打开index.html,就能看到这个小游戏啦!希望对你有帮助哦!如果有任何问题,请随时问我哦!祝玩得开心!???? |
|