本文來源電子發(fā)燒友社區(qū),作者:李先生, 帖子地址:https://bbs.elecfans.com/jishu_2299870_1_1.html
Python開發(fā)環(huán)境體驗:2048游戲
前言
查看開發(fā)板部署了Python3.8.2的環(huán)境,可以使用python進行開發(fā)。支持python等腳本開發(fā)的話對應于很多輕量級開發(fā)場景會非常快速便捷,所以我們也簡單測試下使用python 編寫2048小游戲進行測試。
root@okg2l:~# python3 --version
Python 3.8.2
root@okg2l:~#
準備
參考https://bbs.elecfans.com/jishu_2299841_1_1.html開發(fā)環(huán)境搭建
基本的登錄文件傳輸?shù)炔僮鳌?/p>
2048小游戲代碼
vi 2048.py
按鍵i進入編輯模式
復制黏貼如下代碼
# -*- 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 = 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")
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 ("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)
按esc按鍵
按鍵shift+:進入命令模式
輸入wq!保存
運行
python3 2048.py
按鍵w,s,a,d回車分別對應上下左右。
總結
以一個小的程序體驗下python開發(fā)的便捷,開發(fā)板支持的python版本也比較新Python3.8.2,能較好滿足開發(fā)需求。
-
飛凌
+關注
關注
0文章
135瀏覽量
16405 -
開發(fā)板試用
+關注
關注
3文章
301瀏覽量
2341
發(fā)布評論請先 登錄
基于RK處理器,創(chuàng)龍TL3562-MiniEVM開發(fā)板試用體驗

性能強悍!合眾HZ-RK3568開發(fā)板試用體驗

免費丨米爾 STM32MP257開發(fā)板有獎試用

如何安裝模擬器玩nes小游戲-基于米爾瑞芯微RK3576開發(fā)板
【ELF 2學習板試用】ELF2開發(fā)板開箱體驗及基本使用環(huán)境搭建
飛凌嵌入式受邀亮相瑞薩2024工業(yè)技術研討會

【飛凌嵌入式OK3588J-C開發(fā)板體驗】OK3588J-C開發(fā)板開箱評測
追加名額丨米爾瑞芯微RK3576開發(fā)板有獎試用

RZ/G2L高速虛擬串口方案 基于瑞薩RZ/G2L SMARC開發(fā)板的虛擬(Virtual UART)實現(xiàn)方案

評論