首页 > 解决方案 > Python numpy:索引20超出轴0的范围,大小为20?

问题描述

我已经阅读了与该主题相关的其他已回答问题,它们都指的是嵌套循环“轴”,但在我的情况下,我只使用单个值来使用内置索引。

import random, pygame, numpy


#window setup aswell as global variable
pygame.init()
screen_size = 400
global window
window = pygame.display.set_mode((screen_size,screen_size))
pygame.display.set_caption("evolution simulator")
window.fill((255,255,255))
pygame.display.update()

def draw_Window():
    global grid
    blockSize = 20
    # grid = []
    for row in range(20):#NO IT ISNT
        for column in range(20):
            grid = numpy.zeros((20,20))
            rect = pygame.Rect(row*blockSize, column*blockSize,blockSize, blockSize)
            pygame.draw.rect(window,(0,0,0), rect, 1)

#0 EMPTY
#1 spurgs
#2 FOOD




class spurgs:
    def __init__(self,age,hunger,speed,gender,x,y,race):
        # 1 for male, 2 for female
        self.hunger = hunger
        self.speed = speed
        self.gender = gender
        self.age = age
        self.race = race
        self.x = x
        self.y = y
        self.size = 8
        self.death = False


    def update(self):
        if self.death == False:
            self.age = self.age + 1
            self.draw()
        if self.age <= 100:
            coin_flip = random.randint(1,2)
            if coin_flip == 1:
                self.death == True
        if grid[self.x,self.y] == 0:
            grid[self.x,self.y] = 1
        self.sense()
#MATRIX



#SURVIVAL ELEMENTS
    def eat(self):
        self.hunger +=1
    def breed(self,mate):
        # In the generation class, make a list for the next generation and append baby to the list
        pass
# SENSE
    def sense(self):
        self.senses = 1

        if grid[self.x,self.y] == grid[20,self.y]:
            right = False
        else:
            right = True
        if grid[self.x+self.senses,self.y] == 2  and right:#Right
            self.move("right")



        if grid[self.x,self.y] == grid[0,self.y]:
            left = False
        else:
            left = True
        if grid[self.x-self.senses,self.y] == 2 and left:#left
            self.move("left")


        if grid[self.x,self.y] == grid[self.x,20]:
            down = False
        else:
            down = True
        if grid[self.x,self.y-self.senses] ==2 and up:
            self.move("down")

        if grid[self.x,self.y] == grid[self.x,0]:
            up = False
        else:
            up = True
        if grid[self.x,self.senses+self.y] == 2:
            self.move("up")
#MOVEMENT
    def move(self,dir):
        if self.death == False:
            if dir == "right":
                self.x += self.senses
                self.update()
        if self.death == False:
            if dir == "left":
                self.x -= self.senses
                self.update()
        if self.death == False:
            if dir == "up":
                self.y += self.senses
                self.update()
        if self.death == False:
            if dir == "down":
                self.y -= self.senses
                self.update()

#DRAWING
    def draw(self):
        # x =1 , y =4
        #for grid system
        if self.death ==False:
            #{Pixel Converter}
            self.d_x = self.x * 20 - 10
            self.d_y = self.y * 20 - 10
            pygame.draw.circle(window,self.race,(self.d_x,self.d_y),self.size)
# GENERATION class
class generation:
    def __init__(self,size):
        self.size = size + 1
        self.currentGen = []
    def makeGeneration(self):
        for i in range(self.size):

            self.seed_mutations = random.randint(0,20)
            self.x = random.randint(0,20)
            self.y = random.randint(0,20)
            self.gender = random.randint(1,2)
#Race chooser

            race = random.randint (1,4)
            if race == 1:
                self.color_race = (0, 110, 255)
            elif race == 2:
                self.color_race = (255, 234, 0)
            if race == 3:
                self.color_race = (132, 0, 255)
            if race == 4:
                self.color_race = (242, 53, 53)


            self.currentGen.append(spurgs(1,self.seed_mutations,self.seed_mutations,self.gender,self.x,self.y,self.color_race))
    def update(self):
        for s in self.currentGen:
            s.update()
class food:
    def __init__(self,type):
        self.type = type
        if self.type == "m":
            self.hunger_val = 2
        elif self.type == "a":
            self.hunger_val = 1
        self.eaten = False
        self.x = random.randint(0,20)
        self.y = random.randint(0,20)

    def draw(self):
        if self.eaten == False:
            x = self.x * 20 - 10
            y = self.y * 20 - 10
            if self.type =="m":
                pygame.draw.circle(window,(10,255,50),(x,y),self.hunger_val*5)
            elif self.type == "a":
                pygame.draw.circle(window,(0,0,0),(x,y),self.hunger_val*5)
    def update(self):
        if self.eaten == False:
            self.draw()
        if grid[self.x,self.y] == 0:
            grid[self.x,self.y] = 2
        elif grid[self.x,self.y] == 1:
            grid[self.x,self.y] = 1
            self.eaten = False



a = food("m")
a.draw()














gen_1 = generation(8)
gen_1.makeGeneration()







run = True

while run:
    #WINDOW

    window.fill((255,255,255))
    draw_Window()
    #QUIT CONDITION
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            print("closing the loop")
            run = False


    #TESTING
    gen_1.update()
    a.update()
    print("getting here")
    #FINAL LOOP
    pygame.display.update()
    pygame.time.delay(1000)

pygame.quit()

这是错误:
如果 grid[self.x+self.senses,self.y] == 2 and right:#Right IndexError: index 21 is out of bounds for axis 0 with size 20

标签: pythonnumpypygame

解决方案


对于大小为 20 的轴 0,索引 20 是否超出范围?

是的当然。索引从 0 开始。因此从 0 到 19 的索引是有效的,但 20 是超出范围的。

数组 si 中的最后一个元素用 19 而不是 20 寻址:

if grid[self.x,self.y] == grid[20,self.y]:

if grid[self.x,self.y] == grid[19,self.y]:

if grid[self.x,self.y] == grid[self.x,20]:

if grid[self.x,self.y] == grid[self.x,19]:

self.x = random.randint(0,20)

self.x = random.randint(0,19)

或者,您可以使用random.randrange代替random.randint

self.x = random.randrange(20)

推荐阅读