Reputation: 9
I am new to coding and python.
Trying to make a little game and getting this error message:
IndexError: index 10 is out of bounds for axis 0 with size 10.
My question is: How should I fix this and how to respawn a new ice drop after the first one reached the end?
Thanks for your time and help.
Here is my code:
import numpy as np
import cv2
import random
WIDTH = 20
HEIGHT = 10
player_position = 0
ice_position = [0, random.randint(0, WIDTH - 1)]
while True:
#init world
arr = np.zeros((HEIGHT, WIDTH)).astype(np.uint8)
#populate player
arr[-1, player_position] = 255
#populate ice
arr[ice_position[0], ice_position[1]] = 100
#show world
arr = cv2.resize(arr, (WIDTH * 50, HEIGHT * 50), interpolation =
cv2.INTER_NEAREST)
cv2.imshow("game", arr) #show image
k = cv2.waitKey(100) # wait for 100ms
# make the ice fall
ice_position[0] += 1
# player action
if k == ord("d"):
if player_position < WIDTH - 1:
player_position += 1
if k == ord("a"):
if player_position > 0:
player_position -= 1
if k == ord("q"):
exit()
Upvotes: 0
Views: 403
Reputation: 337
First of all, do not use opencv for games, use pygame or similar. Anyway, here is the solution -
...
if ice_position[0] !=10:
arr[ice_position[0], ice_position[1]] = 100
else:
ice_position[0]=0
...
you just needed to add an if else statement to reset to 0 when the snowball reached the end
Upvotes: 0