将元组转换为列表并返回 [英] Convert tuple to list and back
本文介绍了将元组转换为列表并返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在使用平铺地图为pygame中的游戏开发地图编辑器. 该级别由以下结构的块构成(尽管更大):
I'm currently working on a map editor for a game in pygame, using tile maps. The level is built up out of blocks in the following structure (though much larger):
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"是一块墙壁,而"0"是一块空的空气.
where "1" is a block that's a wall and "0" is a block that's empty air.
以下代码基本上是处理块类型更改的代码:
The following code is basically the one handling the change of block type:
clicked = pygame.mouse.get_pressed()
if clicked[0] == 1:
currLevel[((mousey+cameraY)/60)][((mousex+cameraX)/60)] = 1
但是由于级别存储在元组中,所以我无法更改不同块的值.如何轻松地在关卡中更改不同的值?
But since the level is stored in a tuple, I'm unable to change the values of the different blocks. How do I go about changing the different values in the level in an easy manner?
推荐答案
将元组转换为列表:
>>> 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')
这篇关于将元组转换为列表并返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文