Python numpy:索引 20 超出轴 0 的范围,大小为 20? [英] Python numpy : index 20 is out of bounds for axis 0 with size 20?

查看:205
本文介绍了Python numpy:索引 20 超出轴 0 的范围,大小为 20?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了与此主题相关的其他已回答问题,它们都涉及嵌套循环轴";但就我而言,我仅使用单个值就使用了内置索引.

I've read other answered questions relating to this topic and they all refer to the nested loops "axis" but in my case, I have used the inbuilt index by just using a single value.

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()

这是错误:
if grid[self.x+self.senses,self.y] == 2 and right:#Right索引错误:索引 21 超出轴 0 的范围,大小为 20

This is the error :
if grid[self.x+self.senses,self.y] == 2 and right:#Right IndexError: index 21 is out of bounds for axis 0 with size 20

推荐答案

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

index 20 is out of bounds for axis 0 with size 20?

当然可以.索引从0开始.因此0到19的索引是有效的,但20是越界的.

Yes of course. The indices start at 0. Therefor the indices from 0 to 19 are valid, but 20 is out of bounds.

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

The last element in the array si addressed with 19 rather than 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:

Alternatively, you can use random.randrange instead of random.randint:

self.x = random.randrange(20)

这篇关于Python numpy:索引 20 超出轴 0 的范围,大小为 20?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆