Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 索引器:列表索引超出范围,蛇游戏 “” 创建于2016年3月13日 蠕虫游戏 @作者:海 ''' 导入pygame 随机输入 从pygame导入*## 类蠕虫: def u u初始(自,表面): self.surface=曲面 self.x=surface.get_width()/2 self.y=surface.get_height()/2 self.length=1 自我成长到=50 self.vx=0 self.vy=-1 self.body=[] self.crash=False self.color=255、255、0 def钥匙事件(自身、事件): “”“处理影响蠕虫的键盘事件。”“” 如果event.key==pygame.K_UP和self.vy!=1: self.vx=0 self.vy=-1 elif event.key==pygame.K_RIGHT and self.vx!=-1: self.vx=1 self.vy=0 elif event.key==pygame.K_DOWN and self.vy!=-1: self.vx=0 self.vy=1 elif event.key==pygame.K_LEFT和self.vx!=1: self.vx=-1 self.vy=0 def移动(自我): “移动蠕虫” self.x+=self.vx#将vx添加到x self.y+=self.vy 如果self.body中有(self.x,self.y): self.crash=True self.body.insert(0,(self.x,self.y)) #将蠕虫更改为正确的大小,使其能够成长 如果self.grow_>self.length: 自我长度+=1 #如果主体长度超过长度,则弹出 如果len(self.body)>self.length: self.body.pop() """ def牵引(自): self.surface.set_在((int(self.x),int(self.y)),(255,255,255)) self.surface.set_在((int(self.last[0]),int(self.last[1]),(0,0,0)) """ def位置(自身): 返回self.x,self.y def eat(self): 自我成长到+=25 def牵引(自): x、 y=自我主体[0] self.surface.set_在((int(x),int(y)),self.color) x、 y=自身身体[-1] self.surface.set_在((int(x),int(y)),(0,0,0)) #对于self.body中的x,y: #self.surface.set_在((int(x),int(y)),self.color) #pygame.draw.rect(self.surface,self.color,(self.x,self.y,6,6),0) #虫头 类别食物: def u u初始(自,表面): self.surface=曲面 self.x=random.randint(0,surface.get_width()) self.y=random.randint(0,surface.get_height()) self.color=255255 def牵引(自): self.surface.set_在((int(self.x)、int(self.y))、self.color) pygame.draw.rect(self.surface,self.color,(self.x,self.y,6,6),0) def位置(自身): 返回self.x,self.y “检查虫子是否吃了这种食物” def检查(自身、x、y): 如果xself.x+6: 返回错误 如果yself.y+6: 返回错误 其他: 返回真值 def擦除(自): pygame.draw.rect(self.surface,(0,0,0),(int(self.x),int(self.y),6,6,0) w=h=500 screen=pygame.display.set_模式((w,h)) clock=pygame.time.clock() pygame.mixer.init() chomp=pygame.mixer.Sound(“bow.wav”) 分数=0 worm=worm(屏幕)### 食物=食物(屏幕) 运行=真 运行时: #screen.fill((0,0,0))在worm draw()中优化 worm.draw() 食物 worm.move() 如果worm.x=w-1或worm.y=h-1,则: 打印(“U丢失”) 运行=错误 elif食品检查(worm.x、worm.y): 虫子吃 食物 吃,玩 分数+=1 打印(“分数为:%d”%Score) 食物=食物(屏幕) 对于pygame.event.get()中的事件: 如果event.type==pygame.QUIT:#按X 运行=错误 elif event.type==pygame.KEYDOWN:#按键盘时 worm.key_事件(事件) pygame.display.flip() 时钟滴答(100)_Python_Arrays_Indexing_Pygame - Fatal编程技术网

Python 索引器:列表索引超出范围,蛇游戏 “” 创建于2016年3月13日 蠕虫游戏 @作者:海 ''' 导入pygame 随机输入 从pygame导入*## 类蠕虫: def u u初始(自,表面): self.surface=曲面 self.x=surface.get_width()/2 self.y=surface.get_height()/2 self.length=1 自我成长到=50 self.vx=0 self.vy=-1 self.body=[] self.crash=False self.color=255、255、0 def钥匙事件(自身、事件): “”“处理影响蠕虫的键盘事件。”“” 如果event.key==pygame.K_UP和self.vy!=1: self.vx=0 self.vy=-1 elif event.key==pygame.K_RIGHT and self.vx!=-1: self.vx=1 self.vy=0 elif event.key==pygame.K_DOWN and self.vy!=-1: self.vx=0 self.vy=1 elif event.key==pygame.K_LEFT和self.vx!=1: self.vx=-1 self.vy=0 def移动(自我): “移动蠕虫” self.x+=self.vx#将vx添加到x self.y+=self.vy 如果self.body中有(self.x,self.y): self.crash=True self.body.insert(0,(self.x,self.y)) #将蠕虫更改为正确的大小,使其能够成长 如果self.grow_>self.length: 自我长度+=1 #如果主体长度超过长度,则弹出 如果len(self.body)>self.length: self.body.pop() """ def牵引(自): self.surface.set_在((int(self.x),int(self.y)),(255,255,255)) self.surface.set_在((int(self.last[0]),int(self.last[1]),(0,0,0)) """ def位置(自身): 返回self.x,self.y def eat(self): 自我成长到+=25 def牵引(自): x、 y=自我主体[0] self.surface.set_在((int(x),int(y)),self.color) x、 y=自身身体[-1] self.surface.set_在((int(x),int(y)),(0,0,0)) #对于self.body中的x,y: #self.surface.set_在((int(x),int(y)),self.color) #pygame.draw.rect(self.surface,self.color,(self.x,self.y,6,6),0) #虫头 类别食物: def u u初始(自,表面): self.surface=曲面 self.x=random.randint(0,surface.get_width()) self.y=random.randint(0,surface.get_height()) self.color=255255 def牵引(自): self.surface.set_在((int(self.x)、int(self.y))、self.color) pygame.draw.rect(self.surface,self.color,(self.x,self.y,6,6),0) def位置(自身): 返回self.x,self.y “检查虫子是否吃了这种食物” def检查(自身、x、y): 如果xself.x+6: 返回错误 如果yself.y+6: 返回错误 其他: 返回真值 def擦除(自): pygame.draw.rect(self.surface,(0,0,0),(int(self.x),int(self.y),6,6,0) w=h=500 screen=pygame.display.set_模式((w,h)) clock=pygame.time.clock() pygame.mixer.init() chomp=pygame.mixer.Sound(“bow.wav”) 分数=0 worm=worm(屏幕)### 食物=食物(屏幕) 运行=真 运行时: #screen.fill((0,0,0))在worm draw()中优化 worm.draw() 食物 worm.move() 如果worm.x=w-1或worm.y=h-1,则: 打印(“U丢失”) 运行=错误 elif食品检查(worm.x、worm.y): 虫子吃 食物 吃,玩 分数+=1 打印(“分数为:%d”%Score) 食物=食物(屏幕) 对于pygame.event.get()中的事件: 如果event.type==pygame.QUIT:#按X 运行=错误 elif event.type==pygame.KEYDOWN:#按键盘时 worm.key_事件(事件) pygame.display.flip() 时钟滴答(100)

Python 索引器:列表索引超出范围,蛇游戏 “” 创建于2016年3月13日 蠕虫游戏 @作者:海 ''' 导入pygame 随机输入 从pygame导入*## 类蠕虫: def u u初始(自,表面): self.surface=曲面 self.x=surface.get_width()/2 self.y=surface.get_height()/2 self.length=1 自我成长到=50 self.vx=0 self.vy=-1 self.body=[] self.crash=False self.color=255、255、0 def钥匙事件(自身、事件): “”“处理影响蠕虫的键盘事件。”“” 如果event.key==pygame.K_UP和self.vy!=1: self.vx=0 self.vy=-1 elif event.key==pygame.K_RIGHT and self.vx!=-1: self.vx=1 self.vy=0 elif event.key==pygame.K_DOWN and self.vy!=-1: self.vx=0 self.vy=1 elif event.key==pygame.K_LEFT和self.vx!=1: self.vx=-1 self.vy=0 def移动(自我): “移动蠕虫” self.x+=self.vx#将vx添加到x self.y+=self.vy 如果self.body中有(self.x,self.y): self.crash=True self.body.insert(0,(self.x,self.y)) #将蠕虫更改为正确的大小,使其能够成长 如果self.grow_>self.length: 自我长度+=1 #如果主体长度超过长度,则弹出 如果len(self.body)>self.length: self.body.pop() """ def牵引(自): self.surface.set_在((int(self.x),int(self.y)),(255,255,255)) self.surface.set_在((int(self.last[0]),int(self.last[1]),(0,0,0)) """ def位置(自身): 返回self.x,self.y def eat(self): 自我成长到+=25 def牵引(自): x、 y=自我主体[0] self.surface.set_在((int(x),int(y)),self.color) x、 y=自身身体[-1] self.surface.set_在((int(x),int(y)),(0,0,0)) #对于self.body中的x,y: #self.surface.set_在((int(x),int(y)),self.color) #pygame.draw.rect(self.surface,self.color,(self.x,self.y,6,6),0) #虫头 类别食物: def u u初始(自,表面): self.surface=曲面 self.x=random.randint(0,surface.get_width()) self.y=random.randint(0,surface.get_height()) self.color=255255 def牵引(自): self.surface.set_在((int(self.x)、int(self.y))、self.color) pygame.draw.rect(self.surface,self.color,(self.x,self.y,6,6),0) def位置(自身): 返回self.x,self.y “检查虫子是否吃了这种食物” def检查(自身、x、y): 如果xself.x+6: 返回错误 如果yself.y+6: 返回错误 其他: 返回真值 def擦除(自): pygame.draw.rect(self.surface,(0,0,0),(int(self.x),int(self.y),6,6,0) w=h=500 screen=pygame.display.set_模式((w,h)) clock=pygame.time.clock() pygame.mixer.init() chomp=pygame.mixer.Sound(“bow.wav”) 分数=0 worm=worm(屏幕)### 食物=食物(屏幕) 运行=真 运行时: #screen.fill((0,0,0))在worm draw()中优化 worm.draw() 食物 worm.move() 如果worm.x=w-1或worm.y=h-1,则: 打印(“U丢失”) 运行=错误 elif食品检查(worm.x、worm.y): 虫子吃 食物 吃,玩 分数+=1 打印(“分数为:%d”%Score) 食物=食物(屏幕) 对于pygame.event.get()中的事件: 如果event.type==pygame.QUIT:#按X 运行=错误 elif event.type==pygame.KEYDOWN:#按键盘时 worm.key_事件(事件) pygame.display.flip() 时钟滴答(100),python,arrays,indexing,pygame,Python,Arrays,Indexing,Pygame,你好,我出错了 x、 y=自我主体[0] 索引器:列表索引超出范围 我正在做这个教程: 我对python非常陌生,请帮助我问题是,在类的\uuuu init\uuuu函数中,您定义了self.body=[]。这意味着该列表包含0项 稍后在代码中,您将引用self.body[0]和self.body[1],这是列表中的第一项和第二项。由于这些不存在,因此会引发错误。要解决此问题,您需要使用两项初始化self.body。根据您正在执行的操作顺序,您的body列表实际上仍然为空。遵循代码的逻辑,您将

你好,我出错了 x、 y=自我主体[0] 索引器:列表索引超出范围 我正在做这个教程:


我对python非常陌生,请帮助我

问题是,在类的
\uuuu init\uuuu
函数中,您定义了
self.body=[]
。这意味着该列表包含0项


稍后在代码中,您将引用
self.body[0]
self.body[1]
,这是列表中的第一项和第二项。由于这些不存在,因此会引发错误。要解决此问题,您需要使用两项初始化self.body。

根据您正在执行的操作顺序,您的
body
列表实际上仍然为空。遵循代码的逻辑,您将看到

首先,请在此处实例化您的蠕虫:

'''
Created on 13.3.2016
worm game
@author: Hai
'''
import pygame
import random
from pygame import * ##

class Worm:
    def __init__(self, surface):
        self.surface = surface
        self.x = surface.get_width() / 2
        self.y = surface.get_height() / 2
        self.length = 1
        self.grow_to = 50
        self.vx = 0
        self.vy = -1
        self.body = []
        self.crashed = False
        self.color = 255, 255, 0

    def key_event(self, event):
        """ Handle keyboard events that affect the worm. """
        if event.key == pygame.K_UP and self.vy != 1:
            self.vx = 0
            self.vy = -1
        elif event.key == pygame.K_RIGHT and self.vx != -1:
            self.vx = 1
            self.vy = 0
        elif event.key == pygame.K_DOWN and self.vy != -1:
            self.vx = 0
            self.vy = 1
        elif event.key == pygame.K_LEFT and self.vx != 1:
            self.vx = -1
            self.vy = 0

    def move(self):
        """ Moving worm """
        self.x += self.vx # add vx to x
        self.y += self.vy

        if (self.x, self.y) in self.body:
            self.crashed = True

        self.body.insert(0, (self.x, self.y))

        # Changes worm to right size so it is grow_to
        if self.grow_to > self.length: 
            self.length += 1

        # If body is longer than length then pop
        if len(self.body) > self.length:
            self.body.pop()
    """            
    def draw(self):

        self.surface.set_at((int(self.x), int(self.y)), (255, 255, 255))
        self.surface.set_at((int(self.last[0]),int(self.last[1])), (0,0,0))
    """
    def position(self):
        return self.x, self.y

    def eat(self):
        self.grow_to += 25

    def draw(self):

        x, y = self.body[0]
        self.surface.set_at((int(x), int(y)), self.color)
        x, y = self.body[-1]
        self.surface.set_at((int(x), int(y)), (0, 0, 0))

    #    for x, y in self.body:
    #        self.surface.set_at((int(x), int(y)), self.color)
    #        pygame.draw.rect(self.surface, self.color, (self.x, self.y, 6, 6), 0)
    # worm's head


class Food:
    def __init__(self, surface):
            self.surface = surface
            self.x = random.randint(0, surface.get_width())
            self.y = random.randint(0, surface.get_height())
            self.color = 255,255,255

    def draw(self):
        self.surface.set_at((int(self.x),int(self.y)), self.color)
        pygame.draw.rect(self.surface, self.color, (self.x, self.y, 6, 6), 0)

    def position(self):
        return self.x, self.y

    """ Check if worm have eaten this food """
    def check(self, x, y):
        if x < self.x or x > self.x + 6:
            return False
        elif y < self.y or y > self.y + 6:
            return False
        else:
            return True

    def erase(self):
        pygame.draw.rect(self.surface, (0,0,0), (int(self.x), int(self.y), 6, 6), 0)        



w = h = 500

screen = pygame.display.set_mode((w, h))
clock = pygame.time.Clock()

pygame.mixer.init()
chomp = pygame.mixer.Sound("bow.wav")

score = 0
worm = Worm(screen) ###
food = Food(screen)
running = True

while running:
    # screen.fill((0, 0, 0)) optimized in worm draw()
    worm.draw()
    food.draw()
    worm.move()


    if worm.crashed or worm.x <= 0 or worm.x >= w-1 or worm.y <= 0 or worm.y >= h-1:
        print("U lose")
        running = False
    elif food.check(worm.x, worm.y):
        worm.eat()
        food.erase()
        chomp.play()
        score += 1
        print("Score is: %d" % score)
        food = Food(screen)


    for event in pygame.event.get():
        if event.type == pygame.QUIT: # Pressed X
            running = False
        elif event.type == pygame.KEYDOWN: # When pressing keyboard
            worm.key_event(event)

    pygame.display.flip()
    clock.tick(100)
因此,如果查看
Worm
\uuuu init\uuuu
,您将
self.body
设置为空列表
[]

如果您遵循从那时起直到实际调用的所有代码:

worm = Worm(screen) ###
实际上,您从未在
正文
列表中插入任何内容。因此,当您尝试在
draw
方法中访问身体列表时:

worm.draw()
你肯定会得到一个索引超出范围的错误,因为没有什么可访问的

要纠正此问题,您需要提供如下默认设置:

error x, y = self.body[0]
或者,在draw方法中确定如何使用以下条件处理空列表:

# arbitrary data
self.body = [0, 0]

在蠕虫的
\uuu init\uu
函数中,您可以将
self.body[]
设置为
self.body[(0,0)]
,这是有效的。出现此错误的原因是您在开始时定义了一个空列表,之后您试图通过传递索引(
self.body[0]
)从列表中获取一个项目。但是,由于列表为空,Python无法获取所请求的值

希望这有帮助

if self.body:
    # do things