python设计2048小游戏_Python新手实现2048小游戏

Python新手实现2048小游戏

时间:2020-11-18 06:10:41 作者:背锅熊 阅读:62次撤稿申请

触碰 Python 没多久,见到很多人写2048,自身也倒腾了一个,主要是了解Python英语的语法。

程序流程应用Python3 写的,编码150行上下,根据控制面板,键盘按键应用键入标识符仿真模拟。

演试照片

2048.py# -*- coding:UTF-8 -*-#! /usr/bin/python3 import random v = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] def display(v, score): '''显示信息页面''' print('{0:4} {1:4} {2:4} {3:4}'.format(v[0][0], v[0][1], v[0][2], v[0][3])) print('{0:4} {1:4} {2:4} {3:4}'.format(v[1][0], v[1][1], v[1][2], v[1][3])) print('{0:4} {1:4} {2:4} {3:4}'.format(v[2][0], v[2][1], v[2][2], v[2][3])) print('{0:4} {1:4} {2:4} {3:4}'.format(v[3][0], v[3][1], v[3][2], v[3][3]), ' Total score: ', score) def init(v): '''随机分布网格图值''' for i in range(4):v[i] = [random.choice([0, 0, 0, 2, 2, 4]) for x in range(4)] def align(vList, direction): '''两端对齐非零的数据direction == 'left':往左边两端对齐,比如[8,0,0,2]左两端对齐后[8,2,0,0] direction == 'right':往右边两端对齐,比如[8,0,0,2]右两端对齐后[0,0,8,2] '''# 清除目录中的0 for i in range(vList.count(0)):vList.remove(0) # 被清除的0 zeros = [0 for x in range(4 - len(vList))] # 在非0数据的一侧填补0 if direction == 'left':vList.extend(zeros) else:vList[:0] = zerosdef addSame(vList, direction): '''在目录搜索同样且邻近的数据求和, 寻找满足条件的回到True,不然回到False,另外还回到提升的成绩direction == 'left':从右往左边搜索,寻找同样且邻近的2个数据,左边数据翻番,右边数据置0 direction == 'right':从从左往右搜索,寻找同样且邻近的2个数据,右边数据翻番,左边数据置0 ''' score = 0 if direction == 'left':for i in [0, 1, 2]:if vList[i] == vList[i 1] != 0:vList[i] *= 2vList[i 1] = 0score = vList[i]return {'bool':True, 'score':score} else:for i in [3, 2, 1]:if vList[i] == vList[i-1] != 0:vList[i-1] *= 2vList[i] = 0score = vList[i-1]return {'bool':True, 'score':score} return {'bool':False, 'score':score} def handle(vList, direction): '''解决一行(列)中的数据信息,获得最后的这家银行(列)的数据状态值, 回到评分vList: 目录构造,储存了一行(列)中的数据信息 direction: 挪动方位,往上和往左边都应用方位'left',往右边和往下都应用'right' ''' totalScore = 0 align(vList, direction) result = addSame(vList, direction) while result['bool'] == True:totalScore = result['score']align(vList, direction)result = addSame(vList, direction) return totalScoredef operation(v): '''依据挪动方位再次测算引流矩阵状态值,并纪录评分 ''' totalScore = 0 gameOver = False direction = 'left' op = input('operator:') if op in ['a', 'A']: # 往左边挪动direction = 'left'for row in range(4):totalScore = handle(v[row], direction) elif op in ['d', 'D']: # 往右边挪动direction = 'right'for row in range(4):totalScore = handle(v[row], direction) elif op in ['w', 'W']: # 往上挪动direction = 'left'for col in range(4):# 将引流矩阵中一列拷贝到一个目录中随后解决vList = [v[row][col] for row in range(4)]totalScore = handle(vList, direction)# 从解决后的目录中的数据遮盖原先引流矩阵中的值for row in range(4):v[row][col] = vList[row] elif op in ['s', 'S']: # 向下移动direction = 'right'for col in range(4):# 跟上面一样vList = [v[row][col] for row in range(4)]totalScore = handle(vList, direction)for row in range(4):v[row][col] = vList[row] else:print('Invalid input, please enter a charactor in [W, S, A, D] or the lower')return {'gameOver':gameOver, 'score':totalScore}# 统计分析空缺地区数量 N N = 0 for q in v:N = q.count(0) # 不会有剩下的空缺地区时,比赛终止 if N == 0:gameOver = Truereturn {'gameOver':gameOver, 'score':totalScore}# 按2和4出現的概率为3/1来产生随机数2和4 num = random.choice([2, 2, 2, 4])# 产生随机数k,上一步造成的2或4将被填进第k个空缺地区 k = random.randrange(1, N 1) n = 0 for i in range(4):for j in range(4):if v[i][j] == 0:n = 1if n == k:v[i][j] = numbreakreturn {'gameOver':gameOver, 'score':totalScore} init(v)score = 1080rint('Input:W(Up) S(Down) A(Left) D(Right), press CR.')while True: display(v, score) result = operation(v) if result['gameOver'] == True:print('Game Over, You failed!')print('Your total score:', score) else:score = result['score']if score = 2048:print('Game Over, You Win!!!')print('Your total score:', score)

之上上述便是文中给大伙儿共享的所有编码了,期待可以对大伙儿学习培训Python有一定的协助。文章内容来源于:www.seo-7.comwww.sEo-6.comhttp://www.seo-6.com/seoyh/seojichurm/117533.html

(编辑:部分内容来互联网)

顶一下

(0)

0%

踩一下

(0)

0%

版权声明:

1、本文由SEO内容部分来自互联网,保留著作所有权,转载请注明来源,否则谢绝转载;

2、非原创内容会有明确作者及来源标注。3、本文标题:↓↓↓Python新手实现2048小游戏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值