我也没有想到,只是我有一个点子而已,看能不能实现,交给了我的想法给deepseek之后,很快它就帮我实现了,并且所写的代码十分的干净。
这个英语单词消消乐,可以让孩子玩起来,也是乐此不疲。
主要使用了Python和flask,下面就是完整的代码。
以下是使用Flask实现的完整代码方案:
1. 新建项目文件夹结构:
```
word-game/
├── static/
│ └── style.css
├── templates/
│ └── index.html
└── app.py
```
2. `app.py` 内容:
```python
from flask import Flask, render_template, jsonify
import random
app = Flask(__name__)
words = [
{"en": "apple", "cn": "苹果"},
{"en": "water", "cn": "水"},
{"en": "tea", "cn": "茶"},
{"en": "desk", "cn": "书桌"},
{"en": "sky", "cn": "天空"},
{"en": "book", "cn": "书"},
{"en": "computer", "cn": "电脑"},
{"en": "sun", "cn": "太阳"},
{"en": "moon", "cn": "月亮"},
{"en": "flower", "cn": "花"}
]
@app.route('/')
def index():
return render_template('index.html')
@app.route('/get_words')
def get_words():
duplicated = words * 2
random.shuffle(duplicated)
return jsonify(duplicated)
if __name__ == '__main__':
app.run(debug=True)
```
3. `templates/index.html` 内容:
```html
<!DOCTYPE html>
<html>
<head>
<title>单词消消乐</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<h1 id="title">单词消消乐</h1>
<div id="gameArea"></div>
<button id="resetBtn">重置游戏</button>
<script>
let firstClick = null;
let currentWords = [];
function createBubble(word, type, position) {
const bubble = document.createElement('div');
bubble.className = `bubble ${type}`;
bubble.textContent = type === 'english' ? word.en : word.cn;
bubble.style.left = position.x + 'px';
bubble.style.top = position.y + 'px';
bubble.dataset.id = word.en;
bubble.addEventListener('click', handleClick);
return bubble;
}
function generatePositions() {
const positions = [];
for(let i=0; i<currentWords.length; i++) {
positions.push({
x: Math.random()*700,
y: Math.random()*400
});
}
return positions.sort(() => Math.random() - 0.5);
}
function handleClick(e) {
const bubble = e.target;
if(!firstClick) {
firstClick = bubble;
bubble.style.transform += ' scale(1.1)';
} else {
const isMatch = firstClick.dataset.id === bubble.dataset.id;
if(isMatch) {
firstClick.remove();
bubble.remove();
}
firstClick.style.transform = firstClick.style.transform.replace(' scale(1.1)', '');
firstClick = null;
}
}
async function initGame() {
const response = await fetch('/get_words');
currentWords = await response.json();
const gameArea = document.getElementById('gameArea');
gameArea.innerHTML = '';
const positions = generatePositions();
currentWords.forEach((word, i) => {
const type = i % 2 === 0 ? 'english' : 'chinese';
gameArea.appendChild(createBubble(word, type, positions[i]));
});
}
document.getElementById('resetBtn').addEventListener('click', initGame);
initGame();
</script>
</body>
</html>
```
4. `static/style.css` 内容:
```css
body {
background-color: white;
font-family: Arial;
}
#title {
text-align: center;
font-size: 36px;
color: #ff69b4;
margin: 20px;
}
#gameArea {
width: 800px;
height: 500px;
margin: 0 auto;
position: relative;
}
.bubble {
width: 100px;
height: 100px;
border-radius: 50%;
position: absolute;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: all 0.3s;
box-shadow: 0 10px 15px rgba(0,0,0,0.2);
transform: perspective(500px) rotateX(15deg);
}
.english {
background: linear-gradient(145deg, #ff69b4, #ff1493);
color: white;
}
.chinese {
background: linear-gradient(145deg, #ffb6c1, #ff69b4);
color: #333;
}
#resetBtn {
display: block;
margin: 20px auto;
padding: 10px 30px;
background-color: #ff69b4;
color: white;
border: none;
border-radius: 25px;
cursor: pointer;
}
```
运行步骤:
1. 安装依赖:`pip install flask`
2. 在项目目录运行:`python app.py`
3. 访问 http://localhost:5000
实现特点:
1. 后端使用Flask提供REST API获取单词数据
2. 前端通过fetch API获取随机排序的单词列表
3. 每次重置游戏都会从服务器获取新的随机顺序
4. 保持了原有游戏的所有视觉和交互特性
5. 使用Flask静态文件服务加载CSS样式
游戏操作与之前完全一致,但现在可以通过Flask后端轻松扩展功能(如用户系统、积分记录等)。