简体   繁体   中英

How to get an access to sprite in 2d array?

I want to replace array string 'a' with sprite, thus I display images :

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. 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.

I see in your previous loop you are inserting instances of the Alien class into the array. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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