2048相信大家都玩过,不多说,直接上代码
import random
import os
class G2048(object):
def __init__(self, row=4):
self.n = None
self.row = row
self.qiPan = []
def qiPanInit(self):
for i in range(self.row):
temp = []
for j in range(self.row):
temp.append('-')
self.qiPan.append(temp)
self.qiPan[0][0] = '2'
def run(self):
self.qiPanInit()
while 1:
self.printQipan()
m = self.getInput()
self.giveOneNumber()
self.iswinner()
if self.isloser() or m:
break
os.system('cls')
return 1
def giveOneNumber(self):
kong = []
for i in range(0, self.row):
for j in range(0, self.row):
if self.qiPan[i][j] == '-':
kong.append((i, j))
if len(kong) == 0:
return
num = random.randint(0, len(kong) - 1)
self.qiPan[kong[num][0]][kong[num][1]] = '2'
return
def printQipan(self):
print("|------+------+------+------|")
for j in range(0, self.row):
print("|", end="")
for i in range(0, self.row - 1):
print("-%4s-+" % self.qiPan[j][i], end="")
print("-%4s-|" % self.qiPan[j][self.row - 1])
print("|------+------+------+------|")
def getInput(self):
self.n = input('上下左右 WSAD--退出 Z-----------》:')
if len(self.n) > 0:
self.n = self.n[-1]
if self.n in ['w', 'W']:
self.Up()
return
if self.n in ['s', 'S']:
self.Down()
return
if self.n in ['a', 'A']:
self.left()
return
if self.n in ['d', 'D']:
self.right()
return
if self.n in ['z', 'Z']:
return 1
else:
print('输入不符合规范,请重新输入!')
self.getInput()
def iswinner(self):
for i in self.qiPan:
for j in i:
if j == '2048':
print("您已经达成2048成就")
return
def isloser(self):
for i in range(0, self.row - 1):
for j in range(0, self.row):
if self.qiPan[i][j] == self.qiPan[i + 1][j]:
return False
for i in range(0, self.row - 1):
for j in range(0, self.row):
if self.qiPan[j][i] == self.qiPan[j][i + 1]:
return False
print("您已经无路可走")
return True
def Up(self):
temp = []
for i in range(0, self.row):
for j in range(0, self.row):
if self.qiPan[j][i] != '-':
temp.append(self.qiPan[j][i])
if len(temp) == 0:
continue
temp = self.del_temp(temp)
for m in range(0, len(temp)):
self.qiPan[m][i] = temp[m]
for m in range(len(temp), self.row):
self.qiPan[m][i] = '-'
temp = []
def Down(self):
temp = []
for i in range(0, self.row):
for j in range(self.row - 1, -1, -1):
if self.qiPan[j][i] != '-':
temp.append(self.qiPan[j][i])
if len(temp) == 0:
continue
temp = self.del_temp(temp)
for m in range(self.row - 1, self.row - len(temp) - 1, -1):
self.qiPan[m][i] = temp[self.row - m - 1]
for m in range(self.row - len(temp) - 1, -1, -1):
self.qiPan[m][i] = '-'
temp = []
def left(self):
temp = []
for i in range(0, self.row):
for j in range(0, self.row):
if self.qiPan[i][j] != '-':
temp.append(self.qiPan[i][j])
if len(temp) == 0:
continue
temp = self.del_temp(temp)
for m in range(0, len(temp)):
self.qiPan[i][m] = temp[m]
for m in range(len(temp), self.row):
self.qiPan[i][m] = '-'
temp = []
def right(self):
temp = []
for i in range(0, self.row):
for j in range(self.row - 1, -1, -1):
if self.qiPan[i][j] != '-':
temp.append(self.qiPan[i][j])
if len(temp) == 0:
continue
temp = self.del_temp(temp)
for m in range(self.row - 1, self.row - len(temp) - 1, -1):
self.qiPan[i][m] = temp[self.row - m - 1]
for m in range(self.row - len(temp) - 1, -1, -1):
self.qiPan[i][m] = '-'
temp = []
def del_temp(self, temp):
i = 1
while i < len(temp):
if temp[i] == temp[i - 1]:
temp[i - 1] = str(int(temp[i - 1]) * 2)
del temp[i]
i -= 1
i += 1
return temp
再写一个游戏的调用界面
from G2048 import G2048
import os
class Game(object):
def __init__(self):
self.n = None
def run(self):
while 1:
self.select()
self.getInput()
def select(self):
print("|------+------+------+------|")
print("|A.2048")
print("|B.贪吃蛇")
print("|Z.退出游戏")
print("|------+------+------+------|")
def getInput(self):
self.n = input('请选择游戏-------------》:')
if len(self.n) > 0:
self.n = self.n[-1]
if self.n in ['a', 'A']:
my = G2048()
my.run()
return -1
if self.n in ['b', 'B']:
print("还没有开发,敬请期待")
return -1
if self.n in ['z', 'Z']:
exit()
else:
print('输入不符合规范,请重新输入!')
self.getInput()
if __name__ == '__main__':
mygame = Game()
mygame.run()
后期还想开发一下贪吃蛇什么的,恢复经典,练习编程能力
打包游戏
安装相应的pyinstaller库
pip install pyinstaller
打包命令 -F 主程序 -p 依赖
pyinstaller -F game.py -p G2048.py
|