一、遊戲描述
這裡省略了(估計來看的人都玩過2048遊戲)
二、遊戲設計
首先将遊戲分解成若幹個局部一一實作
(1)輸入規則:asdw分别代表左下由上,r為reset,q為退出,其他不執行。
(2)輸出界面:
def display():
for i in range(4):
print('+----+----+----+----+')
for j in range(4):
print('| %3s'%(notzero(matix[i][j])),end = '')
print('| ')
print('+----+----+----+----+')
(3)初始化
(4)上移
(5)下移
(6)左移
(7)右移
(8)判斷遊戲是否結束
三、代碼
import random
#score是一個全局變量,記錄分數
score = 0
# 紀錄遊戲的分數
matix = [[0 for i in range(4)] for i in range(4)]
# 初始化生成一個4*4的清單
def notzero(s):
return s if s != 0 else ''
def display():
for i in range(4):
print('+----+----+----+----+')
for j in range(4):
print('| %3s'%(notzero(matix[i][j])),end = '')
print('| ')
print('+----+----+----+----+')
def init():
# 初始化矩陣
initNumFlag = 0
while 1:
k = 2 if random.randrange(0, 10) > 1 else 4
# 随機生成 2 或 4
s = divmod(random.randrange(0, 16), 4)# 生成矩陣初始化的下标
if matix[s[0]][s[1]] == 0:
# 隻有當其值不為0的時候才指派,避免第二個值重複
matix[s[0]][s[1]] = k
initNumFlag += 1
if initNumFlag == 2:
break
display()
def addRandomNum():
#處理完移動後添加一個新的随機數
while 1:
k = 2 if random.randrange(0, 10) > 1 else 4
s = divmod(random.randrange(0, 16), 4)
#函數把除數和餘數運算結果結合起來,傳回一個包含商和餘數的元組(a // b, a % b)。
if matix[s[0]][s[1]] == 0:
matix[s[0]][s[1]] = k
break
display()
def check():
#檢查遊戲是否結束
for i in range(4):
for j in range(3):
if matix[i][j] == 0 or matix[i][j] == matix[i][j + 1] or matix[j][i] == matix[j + 1][i]:
return True
# true就是沒有結束
else:
return False
def moveRight():
# 向右移動處理函數
global score
for i in range(4):
for j in range(3, 0, -1):
for k in range(j - 1, -1, -1):
if matix[i][k] > 0:
if matix[i][j] == 0:
matix[i][j] = matix[i][k]
matix[i][k] = 0
elif matix[i][j] == matix[i][k]:
matix[i][j] *= 2
score += matix[i][j]
#将目前數作為score加上
matix[i][k] = 0
break
addRandomNum()
def moveUp():
global score
for i in range(4):
for j in range(3):
for k in range(j + 1, 4):
if matix[k][i] > 0:
if matix[j][i] == 0:
matix[j][i] = matix[k][i]
matix[k][i] = 0
elif matix[k][i] == matix[j][i]:
matix[j][i] *= 2
score += matix[j][i]
matix[k][i] = 0
break
addRandomNum()
def moveDown():
global score
for i in range(4):
for j in range(3, 0, -1):
for k in range(j - 1, -1, -1):
if matix[k][i] > 0:
if matix[j][i] == 0:
matix[j][i] = matix[k][i]
matix[k][i] = 0
elif matix[j][i] == matix[k][i]:
matix[j][i] *= 2
score += matix[j][i]
matix[k][i] = 0
break
addRandomNum()
def moveLeft():
global score
for i in range(4):
for j in range(3):
for k in range(1 + j, 4):
if matix[i][k] > 0:
if matix[i][j] == 0:
matix[i][j] = matix[i][k]
matix[i][k] = 0
elif matix[i][j] == matix[i][k]:
matix[i][j] *= 2
score += matix[i][j]
matix[i][k] = 0
break
addRandomNum()
def main():
print(" \033[33;1mWelcome to the Game of 2048!\033[0m")
flag = True
init()
while flag:
#循環的标志
print(' \033[33;1m You Score:%s' % (score))
d = input(' (↑:w) (↓:s) (←:a) (→:d),q(uit) :\033[0m')
#不斷處理使用者輸入
if d == 'a':
moveLeft()
if not check():
print('遊戲結束')
flag = False
#遊戲結束的話直接退出
elif d == 's':
moveDown()
if not check():
print('遊戲結束')
flag = False
elif d == 'w':
moveUp()
if not check():
print('遊戲結束')
flag = False
elif d == 'd':
moveRight()
if not check():
print('遊戲結束')
flag = False
elif d == 'q':
# 退出
break
else:
# 對使用者的其他輸入不做處理
pass
if __name__ == '__main__':
main()