I'm making a game involving powerups using python. There is a random chance of one spawning and it blits its image on the screen. it picks a random powerup from a list once the player's body (The captain in this scenario) collides with it. I'm checking the collision with a rectangle at the position of the image (Same size) and attempting to delete the rectangle, and the image once a collision happens. How can I do this? This is my attempt so far, however, I keep receiving the error: "List assignment index out of range" and it highlights the line: del PowerYList[Spot]. Can someone tell me what I'm doing wrong?
if random.randint(1,2500) == 2500:
PowerY = random.randint(125,585)
PowerRect = Rect(930, PowerY, 50,50)
PowerYList.append(PowerY)
PowerRects.append(PowerRect)
for X in PowerYList:
Screen.blit(PowerUp, (930, X))
Radius = 0
for X in PowerRects:
if X.colliderect(Rect(942, CaptainY, 15, 30)) == True:
Power = random.choice(PowerUps)
Power = "Nuke"
Spot = PowerRects.index(X)
del X
del PowerYList[Spot]
if Power == "Health":
if Health <= 85:
Health += 15
else:
Health += 100-Health
elif Power == "Nuke":
Radius = 0
for Y in range(1,50):
draw.circle(Screen, ORANGE, (500, 350), Radius+50)
draw.circle(Screen, RED, (500,350), Radius)
Radius += 5
Gigabits = []
Gigabits_Heads = []
Gigabit_Health = []
S_Gigabits = []
S_Gigabits_Heads = []
S_Gigabit_Health = []
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…