简体   繁体   English

如何获得二维数组中的精灵?

[英]How to get an access to sprite in 2d array?

I want to replace array string 'a' with sprite, thus I display images : 我想用精灵替换数组字符串'a',因此我显示图像:

arr = ['a','a','a','a']
for i in arr:
    if i == 'p':
        arr[i] = Alien("Alien.png", (20,40))
print(arr[1])                  
# give  <Alien sprite(in 0 groups)>

Next I tried to figure out how to get access to each image to give a motion for every image after replace in array, but code doesn't work as I expected. 接下来,我试图找出如何访问每个图像以在阵列中替换后为每个图像提供运动,但是代码无法按我预期的那样工作。 I don't know how to operate each image apart? 我不知道如何分开操作每个图像?

for j in arr:
    if j == '<Alien sprite(in 0 groups)>':
        j.move(9,11)

In the second loop, I don't think j == .. would work in you favour. 在第二个循环中,我认为j == ..对您不利。 When you print the object in the python prompt it would print <PawnBlue sprite(in 0 groups)> , however checking the string equality as you have wouldn't be right way to check if that particular array index contains an image or not. 当您在python提示符下打印对象时,它将打印<PawnBlue sprite(in 0 groups)> ,但是按照您的检查字符串是否相等,并不是检查特定数组索引是否包含图像的正确方法。

I see in your previous loop you are inserting instances of the Alien class into the array. 我在上一个循环中看到,您正在将Alien类的实例插入数组。 Assuming those are the images that you are talking about, in the second loop change the if statement to if type(j) == Alien : This should work. 假设这些是您要讨论的图像,在第二个循环中,将if语句更改为if type(j) == Alien :这应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM