CSDN官方2048小游戏通关秘诀及复现
Posted 通信汪的美好生活
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CSDN官方2048小游戏通关秘诀及复现相关的知识,希望对你有一定的参考价值。
目录
一、前言
昨天晚上8点半左右(10月21日),我打开微信看消息,马上我在CSDN官方群里面发现了下图所示的小游戏。主要是下方还有一个分数排行,这立刻就激发了我的直男的好胜心,开始了肝分数,并且在玩了大概20次后发现了玩这个小游戏的诀窍。在把自己的排名刷到30多后,花了一晚上复现了这个小游戏。下面是我自己总结的小游戏通关秘诀以及我自己复现的2048小游戏代码。
二、通关小诀窍
1、游戏规则介绍
官方提供的是一个4*4的方格,并且刚开始会随机生成2和4两个数字,并在随后的移动方块过程中在空白处出现2和4,因为数字的大小是随机的所以要分数高我们就要建立一种全局最优的思维。
2、基于规则的一种相对全局最优方法
我的思路就是过滤处理,把数字比较大的都往同一行挤,这样就有更大的几率生成更大的数字。采用这种策略最起码是5000分朝上,具体操作看下面的视频。
官方2048游戏体验
三、游戏复现
1、编程环境介绍
复现游戏用到的工具:vscode+python3.6+Python 自带的tkinter库
2、tkinter介绍
Tkinter(即 tk interface) 是 Python 标准 GUI 库,简称 “Tk”;从本质上来说,它是对 TCL/TK 工具包的一种 Python 接口封装。Tkinter 是 Python 自带的标准库,因此无须另行安装,它支持跨平台运行,不仅可以在 Windows 平台上运行,还支持在 Linux 和 Mac 平台上运行。我这里就是在Linux环境下运行的。
Tkinter 编写的程序,也称为 GUI 程序,GUI (Graphical User Interface)指的是“图形用户界面”,它是计算机图形学(CG)的一门分支,主要研究如何在计算机中表示图形,以及利用计算机进行图形的计算、处理和显示等相关工作。
3、编程复现游戏效果
我设置的游戏界面和官方基本是一模一样的,就是缺少一个加分特效以及分数显示。键盘的w、a、s、d按键分别代表上滑、左滑、下滑、右滑。点击按键后终端会显示对应的操作。
四、尾声及附录代码
1、尾声
1024程序员节对广大程序员来说真的很有意义,我们程序员的生活可不止单纯枯燥的代码,代码实现以及解决的是现实生活中实际存在的问题,有实际问题作为驱动,必然是烧脑且极富创造力的事情。世界越来越离不开代码、世界越来越需要代码。有码走遍天下、无码寸步难行。世界在被代码改变着、而我们在创造着代码。加油各种改变世界的程序员!!!
2、附录代码
import random
from tkinter import Frame, Label, CENTER
#游戏环境设置
SIZE = 401
GRID_LEN = 4
GRID_PADDING = 10
BACKGROUND_COLOR_GAME = "#92877d"
BACKGROUND_COLOR_CELL_EMPTY = "#9e948a"
BACKGROUND_COLOR_DICT = 2: "#eee4da", 4: "#ede0c8", 8: "#f2b179",
16: "#f59563", 32: "#f67c5f", 64: "#f65e3b",
128: "#edcf72", 256: "#edcc61", 512: "#edc850",
1024: "#edc53f", 2048: "#edc22e",
4096: "#eee4da", 8192: "#edc22e", 16384: "#f2b179",
32768: "#f59563", 65536: "#f67c5f",
CELL_COLOR_DICT = 2: "#776e65", 4: "#776e65", 8: "#f9f6f2", 16: "#f9f6f2",
32: "#f9f6f2", 64: "#f9f6f2", 128: "#f9f6f2",
256: "#f9f6f2", 512: "#f9f6f2", 1024: "#f9f6f2",
2048: "#f9f6f2",
4096: "#776e65", 8192: "#f9f6f2", 16384: "#776e65",
32768: "#776e65", 65536: "#f9f6f2",
FONT = ("Verdana", 40, "bold")
KEY_UP_ALT = "\\'\\\\uf700\\'"
KEY_DOWN_ALT = "\\'\\\\uf701\\'"
KEY_LEFT_ALT = "\\'\\\\uf702\\'"
KEY_RIGHT_ALT = "\\'\\\\uf703\\'"
KEY_UP = "'w'"
KEY_DOWN = "'s'"
KEY_LEFT = "'a'"
KEY_RIGHT = "'d'"
KEY_BACK = "'b'"
KEY_J = "'j'"
KEY_K = "'k'"
KEY_L = "'l'"
KEY_H = "'h'"
def new_game(n):
matrix = []
for i in range(n):
matrix.append([0] * n)
return matrix
def add_two(mat):
a = random.randint(0, len(mat)-1)
b = random.randint(0, len(mat)-1)
while(mat[a][b] != 0):
a = random.randint(0, len(mat)-1)
b = random.randint(0, len(mat)-1)
mat[a][b] = 2
return mat
def game_state(mat):
for i in range(len(mat)):
for j in range(len(mat[0])):
if mat[i][j] == 2048:
return 'win'
for i in range(len(mat)-1):
for j in range(len(mat[0])-1):
if mat[i][j] == mat[i+1][j] or mat[i][j+1] == mat[i][j]:
return 'not over'
for i in range(len(mat)):
for j in range(len(mat[0])):
if mat[i][j] == 0:
return 'not over'
for k in range(len(mat)-1):
if mat[len(mat)-1][k] == mat[len(mat)-1][k+1]:
return 'not over'
for j in range(len(mat)-1):
if mat[j][len(mat)-1] == mat[j+1][len(mat)-1]:
return 'not over'
return 'lose'
def reverse(mat):
new = []
for i in range(len(mat)):
new.append([])
for j in range(len(mat[0])):
new[i].append(mat[i][len(mat[0])-j-1])
return new
def transpose(mat):
new = []
for i in range(len(mat[0])):
new.append([])
for j in range(len(mat)):
new[i].append(mat[j][i])
return new
def cover_up(mat):
new = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
done = False
for i in range(4):
count = 0
for j in range(4):
if mat[i][j] != 0:
new[i][count] = mat[i][j]
if j != count:
done = True
count += 1
return (new, done)
#分数设置
def merge(mat,scores):
done = False
for i in range(4):
for j in range(3):
if mat[i][j] == mat[i][j+1] and mat[i][j] != 0:
mat[i][j] *= 2
mat[i][j+1] = 0
scores += 1
done = True
return (mat, done,scores)
#方向控制
def up(game,scores):
print("up")
# return matrix after shifting up
game = transpose(game)
game, done = cover_up(game)
temp = merge(game,scores)
game = temp[0]
done = done or temp[1]
scores = temp[2]
game = cover_up(game)[0]
game = transpose(game)
return (game, done, scores)
def down(game,scores):
print("down")
game = reverse(transpose(game))
game, done = cover_up(game)
temp = merge(game,scores)
game = temp[0]
done = done or temp[1]
scores = temp[2]
game = cover_up(game)[0]
game = transpose(reverse(game))
return (game, done, scores)
def left(game,scores):
print("left")
# return matrix after shifting left
game, done = cover_up(game)
temp = merge(game,scores)
game = temp[0]
done = done or temp[1]
scores = temp[2]
game = cover_up(game)[0]
return (game, done, scores)
def right(game, scores):
print("right")
# return matrix after shifting right
game = reverse(game)
game, done = cover_up(game)
temp = merge(game, scores)
game = temp[0]
done = done or temp[1]
scores = temp[2]
game = cover_up(game)[0]
game = reverse(game)
return (game, done, scores)
class GameGrid(Frame):
def __init__(self):
Frame.__init__(self)
self.grid()
self.master.title('2048')
self.master.bind("<Key>", self.key_down)
self.scores = 0
# self.gamelogic = gamelogic
self.commands = KEY_UP: up, KEY_DOWN: down,
KEY_LEFT: left, KEY_RIGHT: right,
KEY_UP_ALT: up, KEY_DOWN_ALT: down,
KEY_LEFT_ALT: left, KEY_RIGHT_ALT: right,
KEY_H: left, KEY_L: right,
KEY_K: up, KEY_J: down
self.grid_cells = []
self.init_grid()
self.init_matrix()
self.update_grid_cells()
self.mainloop()
def init_grid(self):
background = Frame(self, bg=BACKGROUND_COLOR_GAME,
width=SIZE, height=SIZE)
background.grid()
for i in range(GRID_LEN):
grid_row = []
for j in range(GRID_LEN):
cell = Frame(background, bg=BACKGROUND_COLOR_CELL_EMPTY,
width=SIZE / GRID_LEN,
height=SIZE / GRID_LEN)
cell.grid(row=i, column=j, padx=GRID_PADDING,
pady=GRID_PADDING)
t = Label(master=cell, text="",
bg=BACKGROUND_COLOR_CELL_EMPTY,
justify=CENTER, font=FONT, width=5, height=2)
t.grid()
grid_row.append(t)
self.grid_cells.append(grid_row)
def gen(self):
return random.randint(0, GRID_LEN - 1)
def init_matrix(self):
self.matrix = new_game(4)
self.history_matrixs = list()
self.matrix = add_two(self.matrix)
self.matrix = add_two(self.matrix)
def update_grid_cells(self):
for i in range(GRID_LEN):
for j in range(GRID_LEN):
new_number = self.matrix[i][j]
if new_number == 0:
self.grid_cells[i][j].configure(
text="", bg=BACKGROUND_COLOR_CELL_EMPTY)
else:
self.grid_cells[i][j].configure(text=str(
new_number), bg=BACKGROUND_COLOR_DICT[new_number],
fg=CELL_COLOR_DICT[new_number])
self.update_idletasks()
def key_down(self, event):
key = repr(event.char)
if key == KEY_BACK and len(self.history_matrixs) > 1:
self.matrix = self.history_matrixs.pop()
self.update_grid_cells()
print('back on step total step:', len(self.history_matrixs))
elif key in self.commands:
self.matrix, done,self.scores = self.commands[repr(event.char)](self.matrix,self.scores)
if done:
self.matrix = add_two(self.matrix)
# record last move
self.history_matrixs.append(self.matrix)
self.update_grid_cells()
done = False
if game_state(self.matrix) == 'win':
self.grid_cells[1][1].configure(
text="You", bg=BACKGROUND_COLOR_CELL_EMPTY)
self.grid_cells[1][2].configure(
text="Win!", bg=BACKGROUND_COLOR_CELL_EMPTY)
self.grid_cells[2][1].configure(
text="scores", bg=BACKGROUND_COLOR_CELL_EMPTY)
self.grid_cells[2][2].configure(
text=self.scores, bg=BACKGROUND_COLOR_CELL_EMPTY)
if game_state(self.matrix) == 'lose':
self.grid_cells[1][1].configure(
text="You", bg=BACKGROUND_COLOR_CELL_EMPTY)
self.grid_cells[1][2].configure(
text="Lose!", bg=BACKGROUND_COLOR_CELL_EMPTY)
self.grid_cells[2][1].configure(
text="scores", bg=BACKGROUND_COLOR_CELL_EMPTY)
self.grid_cells[2][2].configure(
text=self.scores, bg=BACKGROUND_COLOR_CELL_EMPTY)
def generate_next(self):
index = (self.gen(), self.gen())
while self.matrix[index[0]][index[1]] != 0:
index = (self.gen(), self.gen())
self.matrix[index[0]][index[1]] = 2
self.scores += 1
gamegrid = GameGrid()
以上是关于CSDN官方2048小游戏通关秘诀及复现的主要内容,如果未能解决你的问题,请参考以下文章