gpt4 book ai didi

Python和矩阵,移动列&行

转载 作者:行者123 更新时间:2023-11-28 18:11:59 25 4
gpt4 key购买 nike

我的 python 脚本有点问题。

实际上,我正在研究真正的 8x8 LED 矩阵,我想绘制带有动画的文本(从右到左、从左到右、从上到下、从下到上)。

比如我要写E字母。所以我创建了 E 矩阵:

matrix =[
"1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1",
"1","1","0","0","0","0","0","0",
"1","1","1","1","1","0","0","0",
"1","1","1","1","1","0","0","0",
"1","1","0","0","0","0","0","0",
"1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1"]

Like this

但是现在,想象一下我想移动我的 E 来制作动画。如果我将 E 向左移动,第八列将是空的,1 秒后,第七列也是如此......当我的矩阵完全为空时,我的 E 字母将再次从正确的位置开始......

你知道我怎样才能用数组、矩阵或其他什么来做到这一点吗?

谢谢

编辑,代码测试 https://www.jdoodle.com/python3-programming-online :

matrix = [  [0, 0, 0, 1, 0], 
[0, 0, 0, 1, 0],
[0, 0, 0, 1, 0],
[0, 0, 0, 1, 0],
[0, 0, 0, 1, 0]]

print(matrix)

def shift(my2dArray):
for _ in range(5): # shift to the left 8 times - brute force
for row in my2dArray:
row.pop(0) # removes first element of each row
row.append("0") # add "0" to the end of the row
# code to update display here and sleep for a bit

shift(matrix)

print(matrix)

最佳答案

(如 s3n0 所建议的)首先,您要声明一个二维数组

 my2dArray = [["1","1","1","1","1","1","1","1"],
["1","1","1","1","1","1","1","1"],
["1","1","0","0","0","0","0","0"],
["1","1","1","1","1","0","0","0"],
["1","1","1","1","1","0","0","0"],
["1","1","0","0","0","0","0","0"],
["1","1","1","1","1","1","1","1"],
["1","1","1","1","1","1","1","1"]]

然后,为了一个简单的算法,您可以执行以下操作...(假设一个 8x8 网格):

def shift_left(my2dArray):
for _ in range(8): # shift to the left 8 times - brute force
for row in my2dArray:
row.pop(0) # removes first element of each row
row.append("0") # add "0" to the end of the row
# code to update display here and sleep for a bit

这是一个将显示向左移动的示例,我将把其余的(其他方向)留给您自己解决,因为它们与该算法非常相似。

要上下移动,只需在算法中调用上一层的列表函数即可:

def shift_up(my2dArray):
for _ in range(8): # shift to the left 8 times - brute force
my2dArray.pop(0) # removes first element of each column
my2dArray.append(["0","0","0","0","0","0","0","0"]) # add "0" to the end of the columns
# code to update display here and sleep for a bit

关于Python和矩阵,移动列&行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50498134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com