本文來源電子發燒友社區,作者:李先生, 帖子地址:https://bbs.elecfans.com/jishu_2308793_1_1.html
前言
過程
復制代碼
前言
[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控制上下左右移動
開發板提供了Python環境,直接開發板上進行Python開發十分便捷,這對一些輕量開發場景時非常方便。
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。
舉報投訴
-
開發板
+關注
關注
26文章
6291瀏覽量
118149 -
EASY-EAI靈眸科技
+關注
關注
4文章
67瀏覽量
3621
發布評論請先 登錄
相關推薦
熱點推薦
【EASY EAI Nano-TB(RV1126B)開發板試用】介紹、系統安裝
【EASY EAI Nano-TB(RV1126B)開發板試用】介紹、系統安裝
本文介紹了靈眸科技 EA
發表于 12-23 18:05
【EASY EAI Nano-TB(RV1126B)開發板試用】+1、開箱上電
EASY EAI Nano-TB(RV1126B)開發板開箱報告:
1 開箱體驗:精致包裝與豐富配件
EASY
發表于 11-19 21:39
EASY EAI Nano-TB(RV1126B)開發板試用】1、初識
非常感謝電子發燒友以及靈眸科技給了我這次試用EASY EAI Nano-TB(RV1126B)開發板的機會,星期四確認了我的
發表于 11-16 11:15
【EASY EAI Nano-TB(RV1126B)開發板試用】命令行功能測試-shell腳本進行IO控制-燈閃
接上文【EASY EAI Nano-TB(RV1126B)開發板試用】開箱測評
https://pan.baidu.com/s/15pnbJ
發表于 11-01 21:26
【EASY EAI Nano-TB(RV1126B)開發板試用】開箱測評
。
。
開發板卡
| EASY EAI Nano-TB(RV1126B)開發套件是靈眸科技研發的一款應用于AIoTl領域的核心板,并配有
發表于 11-01 11:05
【EASY EAI Nano-TB(RV1126B)開發板試用】+初識篇
EASY EAI Nano-TB(RV1126B)開發套件是靈眸科技研發的一款應用于AIoTl領域的核心板,并配有十分豐富的外設資源,以此為基礎便于快速進入學習和
發表于 10-25 22:06
【免費試用】EASY EAI Nano-TB(RV1126B)開發套件評測
EASY-EAI-Nano-TB是靈眸科技研發的一款應用于AIoT領域的AIOT主板。核心板基于瑞芯微的RV1126B處理器設計,并引入了新一代完全基于硬件的最大12M像素ISP(圖像信號處理器
【作品合集】靈眸科技EASY EAI Orin Nano(RK3576)開發板測評
板免費試用
作者:華仔stm32【EASY EAI Orin Nano(RK3576)開發板試用
發表于 09-09 09:59
【EASY EAI Orin Nano開發板試用體驗】EASY-EAI-Toolkit人臉識別
、編譯與測試
1.組件人臉識別代碼如下
2.開發板掛載服務器,編譯
掛載上服務器后,切換到EASY-EAI-Toolkit-3576/Demos/algorithm-face_detect/路徑下
發表于 07-20 14:40
【EASY EAI Orin Nano開發板試用體驗】EASY-EAI-Toolkit初體驗
本次EASY EAI Orin Nano開發板試用體驗主要是超級想體驗靈眸的EASY-EAI-T
發表于 06-28 21:21
【EASY EAI Orin Nano開發板試用體驗】人臉識別體驗
| 10.92 MiB/s, 完成.
處理 delta 中: 100% (77/77), 完成.
2、adb登錄開發板,并加載nfs
developer@EASY-EAI-Develop:~/nfsroot
發表于 06-26 06:54
【EASY EAI Orin Nano開發板試用體驗】--開發板開箱了解
感謝靈眸科技與論壇提供的開發板與平臺。EASY EAI Orin-Nano開發板實物如下:
EASY
發表于 06-25 23:32
【EASY EAI Orin Nano開發板試用體驗】安裝nfs服務器
/developer/nfsroot *
developer@EASY-EAI-Develop:~$
測試:
使用typec連上開發板后,執行adb shell登錄開發板。
在Desktop中創建nfs 然后
發表于 06-22 17:32
【EASY EAI Orin Nano開發板試用體驗】開發環境準備
【前言】
首先感謝電子發燒友論壇以及靈眸科技給示了我這次非常難得的【EASY EAI Orin Nano開發板試用體驗】的機會,在進行評測之
發表于 06-22 16:42
【EASY EAI Nano開源套件試用體驗】開發環境之Python2048小游戲
評論