看着表弟在玩2048游戏,我忍不住用python写了一个,其实很简单

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(“”, 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)

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Python开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注:Python)

4496243)]

[外链图片转存中…(img-D4sSKMZd-1713804496244)]

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注:Python)

  • 16
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值