It looks good!
You're right about the comment, thanks for catching that. It is a dict.
You're right about the comment, thanks for catching that. It is a dict.
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
sprite_1 = (...) # Some 8x8 sprite data
sprite_invader_a = (...) # Some other 8x8 sprite data
inv = [] # A Python list
# sprite number, x, y
inv.append({'s':sprite_invader_a, 'x':40, 'y':28})
inv.append({'s':sprite_1, 'x':50, 'y':28})
inv.append({'s':sprite_1, 'x':60, 'y':28})
inv.append({'s':sprite_1, 'x':70, 'y':28})
inv.append({'s':sprite_1, 'x':80, 'y':28})
while(1):
thumby.display.fill(0)
for invader in inv:
thumby.display.blit(invader['s'],invader['x'], invader['y'], 8, 8, 0)
# By index:
del inv[0] # removes invader_a
del inv[0] # removes {'s':sprite_1, 'x':50, 'y':28}
# By value:
inv.remove({'s':sprite_invader_a, 'x':40, 'y':28}) # the whole dict needs to be passed in, which might not make sense for you
# This probably won't do what you expect (deleting all elements) because the indices change as you delete elements:
for invader_i in range(len(inv)):
del inv[invader_i]
sprite_1 = (...) # Some 8x8 sprite data
sprite_invader_a = (...) # Some other 8x8 sprite data
inv = {} # A Python dict
# sprite number, x, y
inv[0] = {'s':sprite_invader_a, 'x':40, 'y':28} # The key/id (0) can be any immutable object. Usually it's an integer as shown here or a string like 'a', 'b', 'c'
inv[1] = {'s':sprite_1, 'x':50, 'y':28}
inv[2] = {'s':sprite_1, 'x':60, 'y':28}
inv[3] = {'s':sprite_1, 'x':70, 'y':28}
inv[4] = {'s':sprite_1, 'x':80, 'y':28}
while(1):
thumby.display.fill(0)
for inv_id in inv:
invader = inv[inv_id]
thumby.display.blit(invader['s'],invader['x'], invader['y'], 8, 8, 0)
# Removing an object:
del inv[3] # Removes {'s':sprite_1, 'x':70, 'y':28}
del inv[4] # Removes {'s':sprite_1, 'x':80, 'y':28}
del inv[0] # Removes {'s':sprite_invader_a, 'x':40, 'y':28}
inv = [None] * 5 # a List
inv[0] = {'s':sprite_invader_a, 'x':40, 'y':28}
inv[1] = {'s':sprite_1, 'x':50, 'y':28}
inv[2] = {'s':sprite_1, 'x':60, 'y':28}
inv[3] = {'s':sprite_1, 'x':70, 'y':28}
inv[4] = {'s':sprite_1, 'x':80, 'y':28}
# Removal:
inv[3] = None
inv = [ # List
{'s':sprite_invader_a, 'x':40, 'y':28},
{'s':sprite_1, 'x':50, 'y':28},
{'s':sprite_1, 'x':60, 'y':28},
{'s':sprite_1, 'x':70, 'y':28},
{'s':sprite_1, 'x':80, 'y':28}
]
# Removal:
inv[3] = None
import random
import time
l = [0,1,2,3]
for i in range(20):
r = random.choice(l)
print(f"{i}: {r}")
# Outputs:
# 0: 1
# 1: 0
# 2: 1
# 3: 3
# 4: 0
# 5: 3
# 6: 3
# 7: 3
# 8: 1
# 9: 1
# 10: 3
# 11: 3
# 12: 3
# 13: 1
# 14: 1
# 15: 2
# 16: 1
# 17: 2
# 18: 3
# 19: 1
import random
import time
random.seed(time.ticks_us()) # Relies on minor inconsistencies to differ a little each time
l = [0,1,2,3]
for i in range(20):
r = random.choice(l)
print(f"{i}: {r}")