写一个带登陆系统的对对碰游戏
时间: 2023-12-10 15:37:10 浏览: 57
制作登陆系统
好的,这里给出一个简单的带登陆系统的对对碰游戏的示例代码,使用的是Python Flask框架和SQLite数据库:
```python
from flask import Flask, render_template, request, redirect, url_for, session
import sqlite3
import random
app = Flask(__name__)
app.secret_key = 'secret_key'
# 连接数据库
conn = sqlite3.connect('users.db')
c = conn.cursor()
# 创建用户表
c.execute('''CREATE TABLE IF NOT EXISTS users
(username TEXT PRIMARY KEY, password TEXT)''')
# 创建游戏记录表
c.execute('''CREATE TABLE IF NOT EXISTS records
(username TEXT, score INTEGER)''')
conn.commit()
# 关闭数据库连接
conn.close()
# 登陆页面
@app.route('/', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
# 连接数据库
conn = sqlite3.connect('users.db')
c = conn.cursor()
# 查找用户
c.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password))
user = c.fetchone()
# 关闭数据库连接
conn.close()
if user:
session['username'] = username
return redirect(url_for('game'))
else:
return render_template('login.html', error='用户名或密码错误')
else:
return render_template('login.html')
# 注册页面
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
# 连接数据库
conn = sqlite3.connect('users.db')
c = conn.cursor()
# 查找用户
c.execute("SELECT * FROM users WHERE username=?", (username,))
user = c.fetchone()
if user:
# 用户名已存在
return render_template('register.html', error='用户名已存在')
else:
# 创建新用户
c.execute("INSERT INTO users VALUES (?, ?)", (username, password))
conn.commit()
# 关闭数据库连接
conn.close()
session['username'] = username
return redirect(url_for('game'))
else:
return render_template('register.html')
# 游戏页面
@app.route('/game')
def game():
if 'username' in session:
# 随机生成两个数字
num1 = random.randint(1, 10)
num2 = random.randint(1, 10)
# 计算正确答案
answer = num1 + num2
# 保存正确答案到session
session['answer'] = answer
return render_template('game.html', num1=num1, num2=num2)
else:
return redirect(url_for('login'))
# 提交答案
@app.route('/submit', methods=['POST'])
def submit():
if 'username' in session:
answer = session['answer']
user_answer = int(request.form['answer'])
if user_answer == answer:
# 答案正确,增加得分
conn = sqlite3.connect('users.db')
c = conn.cursor()
c.execute("INSERT INTO records VALUES (?, ?)", (session['username'], 1))
conn.commit()
conn.close()
return redirect(url_for('game'))
else:
# 答案错误,游戏结束
conn = sqlite3.connect('users.db')
c = conn.cursor()
c.execute("INSERT INTO records VALUES (?, ?)", (session['username'], 0))
conn.commit()
conn.close()
return render_template('gameover.html')
else:
return redirect(url_for('login'))
# 分数排行榜页面
@app.route('/leaderboard')
def leaderboard():
# 连接数据库
conn = sqlite3.connect('users.db')
c = conn.cursor()
# 查询每个用户的得分总和
c.execute("SELECT username, SUM(score) FROM records GROUP BY username ORDER BY SUM(score) DESC")
leaderboard = c.fetchall()
# 关闭数据库连接
conn.close()
return render_template('leaderboard.html', leaderboard=leaderboard)
if __name__ == '__main__':
app.run(debug=True)
```
这个游戏的玩法很简单,每次会随机生成两个数字,然后用户需要输入这两个数字的和,如果答案正确,则得分加一,否则游戏结束。游戏结束后会记录用户的得分,并且会显示分数排行榜,按照得分从高到低排名。
注意,这只是一个简单的示例代码,实际的游戏可能需要更复杂的逻辑和更完善的安全性措施。
阅读全文