Python實作小游戲2048
- 思路分析
- 模塊劃分
- 界面規劃
- 功能實作
- 源代碼
- 運行截圖
- 總結
思路分析
首先我們先建立一個背景板畫布和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,大佬后期又對游戲進行了完善大家可以去觀看,游戲目前進行還算順利,可能在小的細節上還存在可以完善的地方,如果有大佬看出問題歡迎指導交流,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/245217.html
標籤:python
