1678 views|2 replies

501

Posts

4

Resources
The OP
 

Mir MYC-YT507 Development Board Review: Development Environment Part 2: Python 2048 Game [Copy link]

 This post was last edited by qinyunti on 2022-7-25 22:54

2048

Preface

The official image of this development board comes with a python2.7 environment, so you can use python for development. If you support python and other script development, it will be very fast and convenient for many lightweight development scenarios, so we also simply test it by using python to write a 2048 game for testing.

Prepare

Refer to "Mil MYC-YT507 Development Board Evaluation: Development Environment Part 1: Linux C Key Control LED" to install the cross-compilation environment.

And basic login file transfer and other operations.

2048 Mini Game

Code 2048.py

# -*- 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 = [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 == vList[i+1] != 0:

vList *= 2

vList[i+1] = 0

score += vList

return {'bool':True, 'score':score}

else:

for i in [3, 2, 1]:

align(vList, direction)

if vList == vList[i-1] != 0:

vList *= 2

vList[i-1] = 0

score += vList

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 on 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 on 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.randomrange(1, N+1)

n = 0

for i in range(4):

for j in range(4):

if v[j] == 0:

n += 1

if n == k:

v [j] = num

break

return {'gameOver':gameOver, 'score':totalScore}

init(v)

score = 0

print "Input:W(Up) S(Down) A(Left) D(Right), press <CR>."

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)



The w, s, a, and d keys correspond to up, down, left, and right respectively.

Summarize

It is very convenient to support Python lightweight development. It would be more convenient if it could support pip installation libraries and support tkinter and other graphical interface development.

2048.py

3.24 KB, downloads: 2

2048.mp4

1.83 MB, downloads: 1

This post is from Domestic Chip Exchange

Latest reply

I have installed the tkinter environment. It is supported. But pyqt and pyinside2 are not supported.  Details Published on 2022-7-24 16:16
 
 

6818

Posts

11

Resources
2
 
I have installed the tkinter environment. It is supported. But pyqt and pyinside2 are not supported.
This post is from Domestic Chip Exchange

Comments

I also tried to install it manually, but it said that there was a problem with hashlib during setup, which has not been solved yet. You can share it for learning, and the demonstration effect will be better if there is an interface.  Details Published on 2022-7-24 21:55
 
 
 

501

Posts

4

Resources
3
 
lugl4313820 posted on 2022-7-24 16:16 I have installed the tkinter environment. It can be supported. But pyqt and pyinside2 are not fixed.

I also tried to install it manually, but it said that there was a problem with hashlib during setup, which has not been solved yet. You can share it for learning, and the demonstration effect will be better if there is an interface.

This post is from Domestic Chip Exchange
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list