【EASY EAI Nano开源套件试用体验】开发环境之Python2048小游戏-开源软件新版本选型

本文来源电子发烧友社区,作者:李先生, 帖子地址:https://bbs.elecfans.com/jishu_2308793_1_1.html

前言
开发板具备Python运行环境,这对于一些轻量开发可以直接在开发板上进行脚本开发,这就十分方便。python –version可以看到版本是2.7.16。
[root@EASY-EAI-NANO:/]# python –version
Python 2.7.16
过程
直接SSH或者串口登录,
导入2048.py或者
直接vi编辑
# -*- coding:UTF-8 -*- #!/usr/bin/python2 import random import os, sys v = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] def display(v, score): print (“%4d %4d %4d %4d” % (v[0][0], v[0][1], v[0][2], v[0][3])) print (“%4d %4d %4d %4d” % (v[1][0], v[1][1], v[1][2], v[1][3])) print (“%4d %4d %4d %4d” % (v[2][0], v[2][1], v[2][2], v[2][3])) print (“%4d %4d %4d %4d” % (v[3][0], v[3][1], v[3][2], v[3][3])) print (“Total score: %d” % 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): for i in range(vList.count(0)): vList.remove(0) zeros = [0 for x in range(4 – len(vList))] if direction == left: vList.extend(zeros) else: vList[:0] = zeros def addSame(vList, direction): score = 0 if direction == left: for i in [0, 1, 2]: align(vList, direction) if vList[i] == vList[i+1] != 0: vList[i] *= 2 vList[i+1] = 0 score += vList[i] return {bool:True, score:score} else: for i in [3, 2, 1]: align(vList, direction) if vList[i] == vList[i-1] != 0: vList[i] *= 2 vList[i-1] = 0 score += vList[i] return {bool:True, score:score} return {bool:False, score:score} def handle(vList, direction): totalScore = 0 align(vList, direction) result = addSame(vList, direction) while result[bool] == True: totalScore += result[score] align(vList, direction) result = addSame(vList, direction) return totalScore def operation(v): totalScore = 0 gameOver = False direction = left op = raw_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 raw_input ,please enter a charactor in [W,S,A,D] or the lower”) gameOver = True return {gameOver:gameOver,score:totalScore} N = 0 for q in v: N += q.count(0) if N == 0: gameOver = True return {gameover:gameOver,score:totalScore} num = random.choice([2,2,2,4]) k = random.randrange(1, N+1) n = 0 for i in range(4): for j in range(4): if v[i][j] == 0: n += 1 if n == k: v[i][j] = num break return {gameOver:gameOver, score:totalScore} init(v) score = 0 print (“raw_input :W(Up) S(Down) A(Left) D(Right), press.”) while True: os.system(“clear”) display(v, score) result = operation(v) print (result) if result[gameOver] == True: print (“Game Over, You failed!”) print (“Your total score %d” % (score)) sys.exit(1) else: score += result[score] if score >= 2048: print (“Game Over, You Win!!!”) print (“Your total score: %d” % (score)) sys.exit(0)
复制代码
添加可执行权限
chmod +x 2048.py

运行

python 2048.py
分别用wsad控制上下左右移动
【EASY EAI Nano开源套件试用体验】开发环境之Python2048小游戏-开源软件新版本选型
总结
开发板提供了Python环境,直接开发板上进行Python开发十分便捷,这对一些轻量开发场景时非常方便。

免责声明:文章内容来自互联网,本站不对其真实性负责,也不承担任何法律责任,如有侵权等情况,请与本站联系删除。
转载请注明出处:【EASY EAI Nano开源套件试用体验】开发环境之Python2048小游戏-开源软件新版本选型 https://www.yhzz.com.cn/a/6902.html

上一篇 2023-04-17 10:34:34
下一篇 2023-04-17 10:41:08

相关推荐

联系云恒

在线留言: 我要留言
客服热线:400-600-0310
工作时间:周一至周六,08:30-17:30,节假日休息。