From 4c372f09db2c3365578cc6ce521f4215d62709bb Mon Sep 17 00:00:00 2001 From: ll <396463537@qq.com> Date: Fri, 13 Mar 2026 15:06:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8flask=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __pycache__/app.cpython-314.pyc | Bin 0 -> 6965 bytes app.py | 160 +++++++++++++++++++++ templates/index.html | 238 ++++++++++++++++++++++++++++++++ 3 files changed, 398 insertions(+) create mode 100644 __pycache__/app.cpython-314.pyc create mode 100644 app.py create mode 100644 templates/index.html diff --git a/__pycache__/app.cpython-314.pyc b/__pycache__/app.cpython-314.pyc new file mode 100644 index 0000000000000000000000000000000000000000..2194920645fdb37fb6e5f0e630a2f9314ae816fb GIT binary patch literal 6965 zcmdT}U2s#!72dt~O1iRSS%1csZTy3aDi5S*JJCylU1u@ob)CFNcj z1DVb!6Q*$pP=!KJ8tCYS8T!=B^rq}lbgKHbqJD!%d%yj%FLuWdDYR_5ycqQ8e zo-*B;d+%!ZoU?n*ch2tF)plEb9f9(z-~2Xq+eyej@JG2AN|h%EC?RR0kQ5mu3LT^= z3SVJR7!Xt9fRvI33@O8aF=ZSurA!^SV1H7$@ z5#IHR3EnnEhIfNvhPVBsdDx4z)kUc*%Tz2)q_@2J7DR8FT~tsW+(6cqFQZQqjreL^u`IO{cX)Joe#v-K0i8nu=;E z-K0e|EtZJuZWI_A2Z13iI;KWbp)=9*WBim5KM|fM-@+?Rf~9#8GHNed(oRm0DPbo( zexObQzHbUcCPK`log`_J1WVQ55ot?VZ|7tKEoj#rTAQ(wZeGpCrljl>X67d(%aKUggxifiQmtyf)N_7B>6&`IE zUTL!EY0s*+JahHluRWPVUxdH*r2Y5my^D^fn;Wlfyfu|mzBzxlYtFGF{YJj`<$Kc0 zX@4es<+WMKll9*X-+67eZFk<{NFV#pV-vA$))0rQmP0OStYS@Qsh~3f_D++X1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AtD^t|I@8Ar24b$eV!T9`&BV36U}cSJJIuZ+@+ybZzc9T4HgKxEgN8&{aNVRgu zAF%u?SrVTR)XFNY1aG5i+dV9m)Xerv)1}jeK3N8gJiTXP9d<>6i-{HKVf^nIid{q$5X5V}B8)f# zhEA=fa#;hlA4o32qoJ#UvdwMZ7%%qE49rT6`KD&5_0LM~MT_(L!8whNM*7onz06L2Ie!i+~QwkN94}k0+h57y1#!!_fyZ~3$z8QeHLne+(K!~W22cG3mb@F_{{T(XXd3(+m{7O zeNRLp*b7*L6sOd;Ker(#r#*Ajw=9UX8%eemuvVxmlkCj~vI9ucy)0OZBs~SJ721eo eow+bCdh(L#v;I%|Gn>=>_ob#~kw`5p;{OLCL&_Ea literal 0 HcmV?d00001 diff --git a/app.py b/app.py new file mode 100644 index 0000000..e3dd321 --- /dev/null +++ b/app.py @@ -0,0 +1,160 @@ +#!/usr/bin/env python +# encoding: utf-8 +""" +2048 Game with Flask GUI +""" +import random +from flask import Flask, render_template, jsonify, request, session + +app = Flask(__name__) +app.secret_key = '2048_game_secret_key' + +def init(): + matrix = [0 for i in range(16)] + random_lst = random.sample(range(16), 2) + matrix[random_lst[0]] = matrix[random_lst[1]] = 2 + return matrix + +def move(matrix, direction): + mergedList = [] + score = 0 + if direction == 'w': + for i in range(16): + j = i + while j - 4 >= 0: + if matrix[j-4] == 0: + matrix[j-4] = matrix[j] + matrix[j] = 0 + elif matrix[j-4] == matrix[j] and j not in mergedList and j-4 not in mergedList: + matrix[j-4] *= 2 + score += matrix[j-4] + matrix[j] = 0 + mergedList.append(j-4) + mergedList.append(j) + j -= 4 + elif direction == 's': + for i in range(15, -1, -1): + j = i + while j + 4 < 16: + if matrix[j+4] == 0: + matrix[j+4] = matrix[j] + matrix[j] = 0 + elif matrix[j+4] == matrix[j] and j not in mergedList and j+4 not in mergedList: + matrix[j+4] *= 2 + score += matrix[j+4] + matrix[j] = 0 + mergedList.append(j) + mergedList.append(j+4) + j += 4 + elif direction == 'a': + for i in range(16): + j = i + while j % 4 != 0: + if matrix[j-1] == 0: + matrix[j-1] = matrix[j] + matrix[j] = 0 + elif matrix[j-1] == matrix[j] and j not in mergedList and j-1 not in mergedList: + matrix[j-1] *= 2 + score += matrix[j-1] + matrix[j] = 0 + mergedList.append(j-1) + mergedList.append(j) + j -= 1 + else: + for i in range(15, -1, -1): + j = i + while j % 4 != 3: + if matrix[j+1] == 0: + matrix[j+1] = matrix[j] + matrix[j] = 0 + elif matrix[j+1] == matrix[j] and j not in mergedList and j+1 not in mergedList: + matrix[j+1] *= 2 + score += matrix[j+1] + matrix[j] = 0 + mergedList.append(j) + mergedList.append(j+1) + j += 1 + return matrix, score + +def insert(matrix): + getZeroIndex = [] + for i in range(16): + if matrix[i] == 0: + getZeroIndex.append(i) + if not getZeroIndex: + return matrix + + randomZeroIndex = random.choice(getZeroIndex) + max_num = max(matrix) + + if max_num > 128: + candidates = [2, 4, 8, 16, 32] + weights = [60, 25, 10, 4, 1] + else: + candidates = [2, 4, 8] + weights = [70, 25, 5] + + total = sum(weights) + r = random.randint(1, total) + for num, w in zip(candidates, weights): + if r <= w: + matrix[randomZeroIndex] = num + break + r -= w + + return matrix + +def isOver(matrix): + if 0 in matrix: + return False + else: + for i in range(16): + if i % 4 != 3: + if matrix[i] == matrix[i+1]: + return False + if i < 12: + if matrix[i] == matrix[i+4]: + return False + return True + +def has_2048(matrix): + return 2048 in matrix + +def get_game_state(): + if 'matrix' not in session: + session['matrix'] = init() + session['score'] = 0 + return { + 'matrix': session['matrix'], + 'score': session['score'], + 'game_over': isOver(session['matrix']), + 'win': has_2048(session['matrix']) + } + +@app.route('/') +def index(): + return render_template('index.html') + +@app.route('/new_game', methods=['POST']) +def new_game(): + session['matrix'] = init() + session['score'] = 0 + return jsonify(get_game_state()) + +@app.route('/move', methods=['POST']) +def make_move(): + data = request.get_json() + direction = data.get('direction') + + old_matrix = list(session['matrix']) + matrix, add_score = move(session['matrix'], direction) + + if matrix != old_matrix: + insert(matrix) + session['score'] += add_score + + session['matrix'] = matrix + return jsonify(get_game_state()) + +if __name__ == '__main__': + app.run(debug=True) diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..6c246f6 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,238 @@ + + + + + + 2048 Game + + + +

2048

+
+ 分数: 0 +
+
+ {% for i in range(4) %} + {% for j in range(4) %} +
+ {% endfor %} + {% endfor %} +
+ +
+
+ 使用方向键移动方块
+ 合并相同数字获得更高分数! +
+ + + +