'IndexError: index ? is out of bounds for axis 0 with size?
import pygame
import pygame_menu
import tkinter as tk
import numpy as np
import sys
import random
root = tk.Tk()
game_started = True
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
SCREENSIZE = WIDTH, HEIGHT = screen_width, screen_height
BLACK = (0, 0, 0)
RED = (255, 0, 0)
WHITE = (255, 255, 255)
GREY = (160, 160, 160)
pygame.init()
map_width = 5
map_height = 5
surface = pygame.display.set_mode((screen_width, screen_height))
def set_difficulty(value, difficulty):
# Do the job here !
value = difficulty - 1
print(value)
pass
def set_players(value, players):
#do the job here!
value = players - 1
print(value)
pass
def set_mapsize(mapvalue, size):
#do the job here!
global map_width
global map_height
if mapvalue == ('Mini 5x5', {('Mini ',1)}):
map_width = 5
map_height = 5
elif mapvalue == ('small 7x7', {('Small ',2)}):
map_width = 7
map_height = 7
elif mapvalue == ('Normal 10x10', {('Normal ',3)}):
map_width = 10
map_height = 10
elif mapvalue == ('Big 14x14', {('Big ',4)}):
map_width = 14
map_height = 14
elif mapvalue == ('Giant 20x20', {('Giant ',5)}):
map_width = 20
map_height = 20
else:
map_width = random.randint(5,20)
map_height = random.randint(5,20)
print('MAPSIZE', mapvalue)
pass
def start_the_game():
# OUR GRID MAP:
cellMAP = np.random.randint(2, size=(map_width, map_height))
# print(cellMAP)
# [[1 0 1 0 0 0 1 0 1 0]
# [0 0 0 1 0 0 0 1 1 0]
# [0 0 1 1 0 0 1 1 1 0]
# [0 0 1 0 0 0 1 0 0 1]
# [0 1 1 0 0 0 0 1 1 0]
# [0 1 0 0 1 0 0 1 1 1]
# [0 1 0 0 1 1 1 1 0 1]
# [1 0 1 0 1 1 1 1 0 1]
# [0 1 0 1 0 0 0 1 0 0]
# [0 1 1 1 0 1 1 0 0 1]]
_VARS = {'surf': False, 'gridWH': 800,
'gridOrigin': (0, 0), 'gridCells': cellMAP.shape[0], 'lineWidth': 2}
def main():
pygame.init()
_VARS['surf'] = pygame.display.set_mode(SCREENSIZE)
while True:
checkEvents()
_VARS['surf'].fill(GREY)
drawSquareGrid(
_VARS['gridOrigin'], _VARS['gridWH'], _VARS['gridCells'])
placeCells()
pygame.display.update()
# NEW METHOD FOR ADDING CELLS :
def placeCells():
# GET CELL DIMENSIONS...
global map_height
cellBorder = map_height
celldimX = celldimY = (_VARS['gridWH']/_VARS['gridCells']) - (cellBorder*2)
# DOUBLE LOOP
for row in range(cellMAP.shape[0]):
for column in range(cellMAP.shape[1]):
# Is the grid cell tiled ?
if(cellMAP[column][row] == 1):
drawSquareCell(
_VARS['gridOrigin'][0] + (celldimY*row)
+ cellBorder + (2*row*cellBorder) + _VARS['lineWidth']/2,
_VARS['gridOrigin'][1] + (celldimX*column)
+ cellBorder + (2*column*cellBorder) + _VARS['lineWidth']/2,
celldimX, celldimY)
# Draw filled rectangle at coordinates
def drawSquareCell(x, y, dimX, dimY):
pygame.draw.rect(
_VARS['surf'], BLACK,
(x, y, dimX, dimY)
)
def drawSquareGrid(origin, gridWH, cells):
CONTAINER_WIDTH_HEIGHT = gridWH
cont_x, cont_y = origin
# DRAW Grid Border:
# TOP lEFT TO RIGHT
pygame.draw.line(
_VARS['surf'], BLACK,
(cont_x, cont_y),
(CONTAINER_WIDTH_HEIGHT + cont_x, cont_y), _VARS['lineWidth'])
# # BOTTOM lEFT TO RIGHT
pygame.draw.line(
_VARS['surf'], BLACK,
(cont_x, CONTAINER_WIDTH_HEIGHT + cont_y),
(CONTAINER_WIDTH_HEIGHT + cont_x,
CONTAINER_WIDTH_HEIGHT + cont_y), _VARS['lineWidth'])
# # LEFT TOP TO BOTTOM
pygame.draw.line(
_VARS['surf'], BLACK,
(cont_x, cont_y),
(cont_x, cont_y + CONTAINER_WIDTH_HEIGHT), _VARS['lineWidth'])
# # RIGHT TOP TO BOTTOM
pygame.draw.line(
_VARS['surf'], BLACK,
(CONTAINER_WIDTH_HEIGHT + cont_x, cont_y),
(CONTAINER_WIDTH_HEIGHT + cont_x,
CONTAINER_WIDTH_HEIGHT + cont_y), _VARS['lineWidth'])
# Get cell size, just one since its a square grid.
cellSize = CONTAINER_WIDTH_HEIGHT/cells
# VERTICAL DIVISIONS: (0,1,2) for grid(3) for example
for x in range(cells):
pygame.draw.line(
_VARS['surf'], BLACK,
(cont_x + (cellSize * x), cont_y),
(cont_x + (cellSize * x), CONTAINER_WIDTH_HEIGHT + cont_y), 2)
# # HORIZONTAl DIVISIONS
pygame.draw.line(
_VARS['surf'], BLACK,
(cont_x, cont_y + (cellSize*x)),
(cont_x + CONTAINER_WIDTH_HEIGHT, cont_y + (cellSize*x)), 2)
def checkEvents():
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if __name__ == '__main__':
main()
print('Game Started')
pass
menu = pygame_menu.Menu('Welcome To BATTLESHIP', screen_width, screen_height,
theme=pygame_menu.themes.THEME_BLUE)
menu.add.text_input('NAME : ', default='Me')
menu.add.selector('Difficulty : ', [('Random',0),('Amature', 1), ('Normal', 2), ('Hardcore', 3), ('Veteran', 4)], onchange=set_difficulty)
menu.add.selector('Players : ', [('Random',0),('Alone', 1), ('2 Players', 2), ('3 Players', 3), ('4 Players', 4)], onchange=set_players)
menu.add.selector('Map Size : ', [('Random',0),('Mini 5x5', {('Mini ',1)}), ('small 7x7', {('Small ',2)}), ('Normal 10x10', {('Normal ',3)}), ('Big 14x14', {('Big ',4)}), ('Giant 20x20', {('Giant ',5)})], onchange=set_mapsize)
menu.add.button('START', start_the_game)
menu.add.button('QUIT', pygame_menu.events.EXIT)
menu.mainloop(surface)
When I run this code and select any map size it keeps giving this error "IndexError: index 9 is out of bounds for axis 0 with size 9" I have no idea how to fix this so I was messing around with the values it did nothing I don't know if this is because of the structure or because of how the statements/varibles is used/set up.
Edit: I forgot to mention but the issue is here
# DOUBLE LOOP
for row in range(cellMAP.shape[0]):
for column in range(cellMAP.shape[1]):
# Is the grid cell tiled ?
if(cellMAP[column][row] == 1):
drawSquareCell(
_VARS['gridOrigin'][0] + (celldimY*row)
+ cellBorder + (2*row*cellBorder) + _VARS['lineWidth']/2,
_VARS['gridOrigin'][1] + (celldimX*column)
+ cellBorder + (2*column*cellBorder) + _VARS['lineWidth']/2,
celldimX, celldimY)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
