首先我们先建立一个背景板画布和16个海龟块,这里使用的就是turtle库。每次移动在空白的地方随机出现一块2或4的方块,这里使用random库,在上下左右移动的时候要判断移动是否违规,两个海龟块的数值一样时要进行合并,在所有位置都有方块后要判断能否继续进行下一次移动,不能的话要给出重新开始方法,游戏中需要实时记录下分数,同时与最高得分进行比较同步更新。
先创建一个画布,调试出合适的大小、背景和标题
boundary = turtle.Screen()
boundary.setup(430, 630, 500, 10)
boundary.bgcolor('gray')
boundary.title('2048')
注册图片
#写两个示范一下,其他的就先省略了
boundary.register_shape('2.gif')
boundary.register_shape('4.gif')
再设置16个方块和其他细节的大概位置
allpos = [(-150, 50), (-50, 50), (50, 50), (150, 50),
(-150, -50), (-50, -50), (50, -50), (150, -50),
(-150, -150), (-50, -150), (50, -150), (150, -150),
(-150, -250), (-50, -250), (50, -250), (150, -250)]
再设计出一个背景类用于游戏图片的添加
class Background(turtle.Turtle):
def __init__(self):
super().__init__()
self.penup()
def show_text(self):
self.color('white', 'white')
self.goto(-215, 120)
self.begin_fill()
self.pd()
self.goto(215, 120)
self.goto(215, 110)
self.goto(-215, 110)
self.end_fill()
self.pu()
self.shape('title.gif')
self.goto(-125, 210)
self.stamp()
self.shape('score.gif')
self.goto(125, 245)
self.stamp()
self.shape('top_score.gif')
self.goto(125, 170)
self.stamp()
设计出得分数字的字体和颜色,同理最高分数和游戏提示可以写出
def show_score(self, score):
self.color('white')
self.goto(125, 210)
self.clear()
self.write(f'{score}', align='center', font=("Arial", 20, "bold"))
先随机生成一个数字块
class Block(turtle.Turtle):
def __init__(self):
super().__init__()
self.penup()
def grow(self):
num = random.choice([2, 2, 2, 2, 4])
self.shape(f'{num}.gif')
a = random.choice(allpos)
self.goto(a)
allpos.remove(a)
block_list.append(self)
上下左右移动,最多的移动三格,每次移动一格坐标的x或y加减100,同时生成一个新的数字块
def go_down(self):
self.go(-150, -50, 50, 0, -100, True)
def go_up(self):
self.go(-50, -150, -250, 0, 100, True)
def go_left(self):
self.go(-50, 50, 150, -100, 0, False)
def go_right(self):
self.go(50, -50, -150, 100, 0, False)
def go(self, b1, b2, b3, px, py, c):
global move_time, z_bool
move_time = 0
block_1, block_2, block_3 = [], [], []
for i in block_list:
if c is True:
if i.ycor() == b1:
block_1.append(i)
elif i.ycor() == b2:
block_2.append(i)
elif i.ycor() == b3:
block_3.append(i)
else:
if i.xcor() == b1:
block_1.append(i)
elif i.xcor() == b2:
block_2.append(i)
elif i.xcor() == b3:
block_3.append(i)
for j in block_1:
j.move(j.xcor()+px, j.ycor()+py)
for j in block_2:
for k in range(2):
j.move(j.xcor()+px, j.ycor()+py)
for j in block_3:
for k in range(3):
j.move(j.xcor()+px, j.ycor()+py)
if move_time != 0:
block = Block()
block.grow()
对于游戏能否继续的判断在每次移动时都要进行
def judge():
judge_a = 0
if allpos == []:
for i in block_list:
for j in block_list:
if i.shape() == j.shape() and i.distance(j) == 100:
judge_a += 1
if judge_a == 0:
return False
else:
return True
else:
return True
for k in block_list:
if k.shape() == '2048.gif' and z_bool:
win_lose.show_text('达成2048,继续请按回车键')
z_bool = False
if judge() is False:
win_lose.show_text('游戏结束,重新开始请按空格键')
移动时会产生消除数值变大或者移动无效不产生变化
ef move(self, gox, goy):
global move_time, score, z, top_score
if (gox, goy) in allpos:
allpos.append(self.pos())
self.goto(gox, goy)
allpos.remove((gox, goy))
move_time += 1
else:
for i in block_list:
if i.pos() == (gox, goy) and i.shape() == self.shape():
allpos.append(self.pos())
self.goto(gox, goy)
self.ht()
block_list.remove(self)
z = int(i.shape()[0:-4])
i.shape(f'{z*2}.gif')
move_time += 1
对于游戏重新开始
def init():
global z, z_bool, score, block_list, allpos
z = 0
z_bool = True
score = 0
allpos = [(-150, 50), (-50, 50), (50, 50), (150, 50),
(-150, -50), (-50, -50), (50, -50), (150, -50),
(-150, -150), (-50, -150), (50, -150), (150, -150),
(-150, -250), (-50, -250), (50, -250), (150, -250)]
for i in block_list:
i.clear()
i.ht()
win_lose.clear()
block_list = []
block = Block()
block.grow()
对游戏的上下左右移动和重开继续方法进行监听
boundary.listen()
boundary.onkey(block.go_right, 'Right')
boundary.onkey(block.go_left, 'Left')
boundary.onkey(block.go_up, 'Up')
boundary.onkey(block.go_down, 'Down')
boundary.onkey(win_lose.clear, 'Return')
boundary.onkey(init, 'space')
import turtle
import random
boundary = turtle.Screen()
boundary.setup(430, 630, 500, 10)
boundary.bgcolor('gray')
boundary.title('2048')
boundary.register_shape('2.gif')
boundary.register_shape('4.gif')
boundary.register_shape('8.gif')
boundary.register_shape('16.gif')
boundary.register_shape('32.gif')
boundary.register_shape('64.gif')
boundary.register_shape('128.gif')
boundary.register_shape('256.gif')
boundary.register_shape('512.gif')
boundary.register_shape('1024.gif')
boundary.register_shape('2048.gif')
boundary.register_shape('4096.gif')
boundary.register_shape('8192.gif')
boundary.register_shape('bg.gif')
boundary.register_shape('title.gif')
boundary.register_shape('score.gif')
boundary.register_shape('top_score.gif')
boundary.tracer(0)
class Block(turtle.Turtle):
def __init__(self):
super().__init__()
self.penup()
def grow(self):
num = random.choice([2, 2, 2, 2, 4])
self.shape(f'{num}.gif')
a = random.choice(allpos)
self.goto(a)
allpos.remove(a)
block_list.append(self)
def go_down(self):
self.go(-150, -50, 50, 0, -100, True)
def go_up(self):
self.go(-50, -150, -250, 0, 100, True)
def go_left(self):
self.go(-50, 50, 150, -100, 0, False)
def go_right(self):
self.go(50, -50, -150, 100, 0, False)
def go(self, b1, b2, b3, px, py, c):
global move_time, z_bool
move_time = 0
block_1, block_2, block_3 = [], [], []
for i in block_list:
if c is True:
if i.ycor() == b1:
block_1.append(i)
elif i.ycor() == b2:
block_2.append(i)
elif i.ycor() == b3:
block_3.append(i)
else:
if i.xcor() == b1:
block_1.append(i)
elif i.xcor() == b2:
block_2.append(i)
elif i.xcor() == b3:
block_3.append(i)
for j in block_1:
j.move(j.xcor()+px, j.ycor()+py)
for j in block_2:
for k in range(2):
j.move(j.xcor()+px, j.ycor()+py)
for j in block_3:
for k in range(3):
j.move(j.xcor()+px, j.ycor()+py)
if move_time != 0:
block = Block()
block.grow()
bc_score.show_score(score)
bc_top_score.show_top_score(top_score)
for k in block_list:
if k.shape() == '2048.gif' and z_bool:
win_lose.show_text('达成2048,继续请按回车键')
z_bool = False
if judge() is False:
win_lose.show_text('游戏结束,重新开始请按空格键')
def move(self, gox, goy):
global move_time, score, z, top_score
if (gox, goy) in allpos:
allpos.append(self.pos())
self.goto(gox, goy)
allpos.remove((gox, goy))
move_time += 1
else:
for i in block_list:
if i.pos() == (gox, goy) and i.shape() == self.shape():
allpos.append(self.pos())
self.goto(gox, goy)
self.ht()
block_list.remove(self)
z = int(i.shape()[0:-4])
i.shape(f'{z*2}.gif')
move_time += 1
score = score + z
else:
continue
if score > top_score:
top_score = score
class Background(turtle.Turtle):
def __init__(self):
super().__init__()
self.penup()
def show_text(self):
self.color('white', 'white')
self.goto(-215, 120)
self.begin_fill()
self.pd()
self.goto(215, 120)
self.goto(215, 110)
self.goto(-215, 110)
self.end_fill()
self.pu()
self.shape('title.gif')
self.goto(-125, 210)
self.stamp()
self.shape('score.gif')
self.goto(125, 245)
self.stamp()
self.shape('top_score.gif')
self.goto(125, 170)
self.stamp()
def show_back(self):
for i in allpos:
self.shape('bg.gif')
self.goto(i)
self.stamp()
def show_score(self, score):
self.color('white')
self.goto(125, 210)
self.clear()
self.write(f'{score}', align='center', font=("Arial", 20, "bold"))
def show_top_score(self, top_score):
self.color('white')
self.goto(125, 135)
self.clear()
self.write(f'{top_score}', align='center', font=("Arial", 20, "bold"))
class WinLose(turtle.Turtle):
def __init__(self):
super().__init__()
self.penup()
self.ht()
self.color('blue')
def show_text(self, text):
self.write(f'{text}', align='center', font=("黑体", 20, "bold"))
def judge():
judge_a = 0
if allpos == []:
for i in block_list:
for j in block_list:
if i.shape() == j.shape() and i.distance(j) == 100:
judge_a += 1
if judge_a == 0:
return False
else:
return True
else:
return True
def init():
global z, z_bool, score, block_list, allpos
z = 0
z_bool = True
score = 0
allpos = [(-150, 50), (-50, 50), (50, 50), (150, 50),
(-150, -50), (-50, -50), (50, -50), (150, -50),
(-150, -150), (-50, -150), (50, -150), (150, -150),
(-150, -250), (-50, -250), (50, -250), (150, -250)]
for i in block_list:
i.clear()
i.ht()
win_lose.clear()
block_list = []
block = Block()
block.grow()
z = 0
z_bool = True
score = 0
top_score = 0
block_list = []
allpos = [(-150, 50), (-50, 50), (50, 50), (150, 50),
(-150, -50), (-50, -50), (50, -50), (150, -50),
(-150, -150), (-50, -150), (50, -150), (150, -150),
(-150, -250), (-50, -250), (50, -250), (150, -250)]
bc_title = Background()
bc_score = Background()
bc_top_score = Background()
bc_title.show_text()
bc_title.show_back()
bc_score.ht()
bc_top_score.ht()
bc_score.show_score(score)
bc_top_score.show_top_score(top_score)
block = Block()
block.grow()
move_time = 0
win_lose = WinLose()
boundary.listen()
boundary.onkey(block.go_right, 'Right')
boundary.onkey(block.go_left, 'Left')
boundary.onkey(block.go_up, 'Up')
boundary.onkey(block.go_down, 'Down')
boundary.onkey(win_lose.clear, 'Return')
boundary.onkey(init, 'space')
while True:
boundary.update()
boundary.mainloop()
代码是学习了B站小白叶书的思路后,整个人的思路明朗开阔的情况下编写,我学习的是大佬之前版本的2048,大佬后期又对游戏进行了完善大家可以去观看。游戏目前进行还算顺利,可能在小的细节上还存在可以完善的地方,如果有大佬看出问题欢迎指导交流。