python - 将元组转换为列表并返回

我目前正在为 pygame 中的游戏制作 map 编辑器,使用平铺 map 。 该级别由以下结构中的 block 组成(尽管要大得多):

level1 = (
         (1,1,1,1,1,1)
         (1,0,0,0,0,1)
         (1,0,0,0,0,1)
         (1,0,0,0,0,1)
         (1,0,0,0,0,1)
         (1,1,1,1,1,1))

其中“1”是一堵墙的方 block ,“0”是一个空的方 block 。

以下代码基本上是处理 block 类型变化的代码:

clicked = pygame.mouse.get_pressed()
if clicked[0] == 1:
    currLevel[((mousey+cameraY)/60)][((mousex+cameraX)/60)] = 1

但由于关卡存储在元组中,我无法更改不同 block 的值。如何轻松更改关卡中的不同值?

最佳答案

将元组转换为列表:

>>> t = ('my', 'name', 'is', 'mr', 'tuple')
>>> t
('my', 'name', 'is', 'mr', 'tuple')
>>> list(t)
['my', 'name', 'is', 'mr', 'tuple']

将列表转换为元组:

>>> l = ['my', 'name', 'is', 'mr', 'list']
>>> l
['my', 'name', 'is', 'mr', 'list']
>>> tuple(l)
('my', 'name', 'is', 'mr', 'list')

https://stackoverflow.com/questions/16296643/

相关文章:

linux - 在 Linux 脚本中隐藏终端上的用户输入

python - 如何在使用 Python 插入 MySQL 数据库后获取 "id"?

python - 在 SQLAlchemy 中使用 OR

python - Windows Scipy 安装 : No Lapack/Blas Resourc

python - 为什么在 Python 3 中 x**4.0 比 x**4 快?

linux - 如何删除具有特定名称的文件夹

linux - Mac 和 Linux 上的文本文件中的递归搜索和替换

linux - 没有行号的 Bash 历史记录

linux - 如何在 Bash 中运行超时的进程?

python - pandas 获取不在其他数据框中的行