如果self.anim_index>;=len(imager_列表):未绑定LocalError:局部变量';成像仪列表';赋值前引用python错误

如果self.anim_index>;=len(imager_列表):未绑定LocalError:局部变量';成像仪列表';赋值前引用python错误,python,pygame,Python,Pygame,所以我得到了这个错误,它一直说我的东西在赋值之前被引用了,但我不知道赋值是什么,我在添加播放器右移图像动画时得到了这个错误。我一直在移动东西,但这一点都没有帮助,我也尝试过修改一些代码,但这也不起作用 我的错误在哪里 if self.anim_index >= len( imager_list ): self.anim_index = 0 我的完整代码 import pygame pygame.init() window = pygame.display.set_mode((7

所以我得到了这个错误,它一直说我的东西在赋值之前被引用了,但我不知道赋值是什么,我在添加播放器右移图像动画时得到了这个错误。我一直在移动东西,但这一点都没有帮助,我也尝试过修改一些代码,但这也不起作用

我的错误在哪里

if self.anim_index >= len( imager_list ):
    self.anim_index = 0
我的完整代码

import pygame
pygame.init()

window = pygame.display.set_mode((700,500))
pygame.display.set_caption("Noobs First Game")


move = pygame.image.load("WASD.png")


# Playerman
class Player:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.speed = 5
        self.isJump = False
        self.JumpCount = 10
        self.idle =[pygame.image.load("player_idel_1.png"),
                            pygame.image.load("player_idel_2.png"),
                            pygame.image.load("player_idel_3.png"),
                            pygame.image.load("player_idel_4.png"),
                            pygame.image.load("player_idel_5.png"),
                            pygame.image.load("player_idel_6.png"),
                            pygame.image.load("player_idel_7.png"),
                            pygame.image.load("player_idel_8.png")
                            ]
        self.right = [pygame.image.load("Player_walk_right1.png"),
                      pygame.image.load("Player_walk_right2.png"),
                      pygame.image.load("Player_walk_right3.png")]

        self.fall = 0
        self.rect = pygame.Rect(x,y,height,width)
        self.idle = [pygame.transform.scale(image,(image.get_width()*2,image.get_height()*2)) for image in self.idle]
        self.fps = 10
        self.clock = pygame.time.Clock()
        self.anim_index = 0
        self.direction = "idle"
        self.direction = "right"
        self.next_frame_time = 0
        self.animindex = 0
    def get_rect(self):
        self.rect.topleft = (self.x,self.y)
        return self.rect
        pygame.draw.rect(self.color,self.rect)
    
    def draw(self):
        if self.direction == "idle":
            image_list = self.idle
        if self.direction == "right":
            imager_list = self.right
         

        # Is it time to show the next animation frame?
        time_now = pygame.time.get_ticks()
        if ( time_now > self.next_frame_time ):
            # set the time for the next animation-frame
            inter_frame_delay = 1000 // self.fps   
            self.next_frame_time = time_now + inter_frame_delay  # in the future
            # move the current image to the next (with wrap-around)
            self.anim_index += 1
            if self.anim_index >= len( image_list ):
                self.anim_index = 0
            if self.anim_index >= len( imager_list ):
                self.anim_index = 0
            

        pygame.draw.rect( window, self.color, self.get_rect() )
        player_image = image_list[self.anim_index]
        player_imager = imager_list[self.anim_index]
        window.blit(player_imager,player_rect)

        player_rect = player_image.get_rect(center = self.get_rect().center)
        player_rect.centerx += 3
        player_rect.centery -= 13
        window.blit(player_image, player_rect)
        

        


class Platform:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y =y
        self. width = width
        self.color = color
        self.height = height
        self.color = color
        self.speed = 4
        self.rect = pygame.Rect(x,y,width,height)
    def get_rect(self):
        self.rect.topleft = (self.x,self.y)
        return self.rect
    def draw(self):
        pygame.draw.rect(window,self.color,self.get_rect())


class Rule:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y =y
        self. width = width
        self.color = color
        self.height = height
        self.color = color
        self.speed = 4
        self.rect = pygame.Rect(x,y,width,height)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        pygame.draw.rect(window,self.color,self.rect)


# Colors for hitbox
white = (255,255,255)
green = (0,255,0)

# Drawing Player
playerman = Player(350,445,40,40,white)

#Drawing Platforms
platform1 = Platform(300,-9.1,40,500,green)
platform2 = Platform(330,451,2000,40,green)
platform3 = Platform(2300,-9.1,40,500,green)

# Drawing Rule
rule1 = Rule(340,-9.1,220,500,green)
rule2 = Rule(20000,-9,1,5,green)

# List
platforms = [platform1,platform2,platform3]

rules = [rule1,rule2]


# draws map
platformGroup = pygame.sprite.Group
Level = [
"                                         1   1",
"                                         1    ",
"                         1  1        111 1    ",
"                         1  1       111  1    ",
"                         11 1      1111 11    ",
"                         1  1     11111  1    ",
"                         1  1    1111111 1    ",
"           1   1   111   1 11   1111111  1    ",
"           1   1  11111     1  11111111          ",]

for iy,row in enumerate(Level):
    for ix, col in enumerate(row):
        if col == "1":
            new_platforms = Platform(ix*50,iy*50.2,50,50,(255,255,255))
            platforms.append(new_platforms)
    
            

# Windows color
def redrawwindow():
    window.fill((0,0,0))

    # Drawing the player and other stuff to the screen
    playerman.draw()

    for Platform in platforms:
        Platform.draw()
    for Rule in rules:
        Rule.draw()

x = 10
y = 10
x_change = 0
y_change = 0
old_x = x
old_y = y
fps = (30)
clock = pygame.time.Clock()

run = True
while run:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    


    if playerman.y < 250:
        playerman.y += 1
        for Platform in platforms:
            Platform.y += playerman.speed

    if playerman.y > 410:
        playerman.y -= playerman.fall
        for Platform in platforms:
            Platform.y -= playerman.fall

    
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_d:
            x_change = -7
        if event.key == pygame.K_a:
            x_change = 7

    if event.type == pygame.KEYUP:
        if event.key == pygame.K_d or event.key == pygame.K_a:
            x_change = 0

        x += x_change
        if x > 500 - playerman.width or x < 0:
            x = old_x
           
        # lets player move
    keys = pygame.key.get_pressed()
    px, py = playerman.x, playerman.y


    if keys[pygame.K_d]:
        for Platform in platforms:
            Platform.x -= playerman.speed
        for Rule in rules:
            Rule.x -= playerman.speed

    if keys[pygame.K_d]:
        playerman.direction = "right"
    else:
        playerman.direction = "idle"

        
 

    if keys[pygame.K_a]:
        for Platform in platforms:
            Platform.x += playerman.speed
        for Rule in rules:
            Rule.x += playerman.speed



    platform_rect_list = [p.rect for p in platforms]
    player_rect = playerman.get_rect()
    player_rect.topleft = (px, py)

    playerman.y = py
    if player_rect.collidelist(platform_rect_list) < 0:
        playerman.x = px

    move_right = keys[pygame.K_d]
    move_left = keys[pygame.K_a]
    if move_right: 
        for Platform in platforms:
            Platform.x -= playerman.speed
        for Rule in rules:
            Rule.x -= playerman.speed     # <---
    if move_left:
        for Platform in platforms:
            Platform.x += playerman.speed
        for Rule in rules:
            Rule.x += playerman.speed     # <---

    platform_rect_list = [p.get_rect() for p in platforms] # get_rect()
    player_rect = playerman.get_rect()
    player_rect.topleft = (px, py)

    playerman.y = py
    cI = player_rect.collidelist(platform_rect_list)
    if cI >= 0:
        # undo movement of platforms dependent on the direction and intersection distance
        dx = 0
        if move_right: 
            dx = platform_rect_list[cI].left - player_rect.right
        if move_left:
            dx = platform_rect_list[cI].right - player_rect.left
        for Platform in platforms:
            Platform.x -= dx
            Platform.get_rect() # update rectangle
        for Rule in rules:
            Rule.x -= dx             # <---

##############                
  
    # About isJump
    if not playerman.isJump:
        playerman.y += playerman.fall
        playerman.fall += 1
        playerman.isJump = False

        # this part lets you jump on platform only the top 
        collide = False
        for Platform in platforms:
            if playerman.get_rect().colliderect(Platform.rect):
                collide = True
                playerman.isJump = False
                playerman.y = Platform.rect.top - playerman.height
                if playerman.rect.right > Platform.rect.left and playerman.rect.left < Platform.rect.left - playerman.width:
                    playerman.x = Platform.rect.left - playerman.width
                if playerman.rect.left < Platform.rect.right and playerman.rect.right > Platform.rect.right + playerman.width:
                    playerman.x = Platform.rect.right
                       
            # colliding with floor      
            if playerman.rect.bottom >= 500:
                collide = True
                playerman.isJump = False
                playerman.Jumpcount = 10
                playerman.y = 500 - playerman.height

        # Jumping
        if collide:
            if keys[pygame.K_SPACE]:
                playerman.isJump = True
                py -= playerman.speed
            playerman.fall = 0

    # Jump Count

    else:
        if playerman.JumpCount >= 0:
            playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount))*0.3
            playerman.JumpCount -= 1
        else:
            playerman.isJump = False
            playerman.JumpCount = 10

    redrawwindow()
    if playerman.rect.colliderect(rule1.rect):
        window.blit(move,(-40,-100))
    
    pygame.display.update()
pygame.quit()

导入pygame
pygame.init()
window=pygame.display.set_模式((700500))
pygame.display.set_标题(“Noobs第一场游戏”)
move=pygame.image.load(“WASD.png”)
#玩家
职业球员:
定义初始值(自、x、y、宽度、高度、颜色):
self.x=x
self.y=y
self.width=宽度
自我高度=高度
self.color=颜色
自身速度=5
self.isJump=False
self.JumpCount=10
self.idle=[pygame.image.load(“player_idel_1.png”),
pygame.image.load(“player_idel_2.png”),
pygame.image.load(“player_idel_3.png”),
pygame.image.load(“player_idel_4.png”),
pygame.image.load(“player_idel_5.png”),
pygame.image.load(“player_idel_6.png”),
pygame.image.load(“player_idel_7.png”),
pygame.image.load(“player_idel_8.png”)
]
self.right=[pygame.image.load(“Player\u walk\u right1.png”),
pygame.image.load(“Player\u walk\u right2.png”),
pygame.image.load(“Player\u walk\u right3.png”)]
self.fall=0
self.rect=pygame.rect(x,y,高度,宽度)
self.idle=[pygame.transform.scale(image,(image.get_-width()*2,image.get_-height()*2))用于self.idle中的图像]
self.fps=10
self.clock=pygame.time.clock()
self.anim_索引=0
self.direction=“空闲”
self.direction=“right”
self.next\u frame\u time=0
self.animindex=0
def获取正确(自我):
self.rect.topleft=(self.x,self.y)
返回self.rect
pygame.draw.rect(self.color,self.rect)
def牵引(自):
如果self.direction==“空闲”:
image\u list=self.idle
如果self.direction==“right”:
imager_list=self.right
#是时候显示下一个动画帧了吗?
time\u now=pygame.time.get\u ticks()
如果(现在时间>下一帧时间):
#设置下一个动画帧的时间
帧间延迟=1000//self.fps
self.next_frame_time=现在的时间+将来的帧间延迟
#将当前图像移动到下一个图像(使用环绕)
self.anim_索引+=1
如果self.anim\u index>=len(图像列表):
self.anim_索引=0
如果self.anim\u index>=len(成像仪列表):
self.anim_索引=0
pygame.draw.rect(窗口,self.color,self.get_rect())
玩家图片=图片列表[自我动画索引]
player\u imager=imager\u list[self.anim\u index]
blit(player\u imager,player\u rect)
player\u rect=player\u image.get\u rect(中心=self.get\u rect().center)
player_rect.centerx+=3
玩家右中锋-=13
blit(player\u image,player\u rect)
课程平台:
定义初始值(自、x、y、宽度、高度、颜色):
self.x=x
self.y=y
自己宽度=宽度
self.color=颜色
自我高度=高度
self.color=颜色
自身速度=4
self.rect=pygame.rect(x,y,宽度,高度)
def获取正确(自我):
self.rect.topleft=(self.x,self.y)
返回self.rect
def牵引(自):
pygame.draw.rect(窗口,self.color,self.get_rect())
类别规则:
定义初始值(自、x、y、宽度、高度、颜色):
self.x=x
self.y=y
自己宽度=宽度
self.color=颜色
自我高度=高度
self.color=颜色
自身速度=4
self.rect=pygame.rect(x,y,宽度,高度)
def牵引(自):
self.rect.topleft=(self.x,self.y)
pygame.draw.rect(窗口,self.color,self.rect)
#hitbox的颜色
白色=(255255)
绿色=(0255,0)
#绘图播放器
玩家=玩家(350445,40,40,白色)
#绘图平台
平台1=平台(300,-9.1,40500,绿色)
平台2=平台(3304512000,40,绿色)
平台3=平台(2300,-9.1,40500,绿色)
#绘图规则
规则1=规则(340,-9.1220500,绿色)
规则2=规则(20000,-9,1,5,绿色)
#名单
平台=[平台1、平台2、平台3]
规则=[规则1,规则2]
#绘制地图
platformGroup=pygame.sprite.Group
级别=[
"                                         1   1",
"                                         1    ",
"                         1  1        111 1    ",
"                         1  1       111  1    ",
"                         11 1      1111 11    ",
"                         1  1     11111  1    ",
"                         1  1    1111111 1    ",
"           1   1   111   1 11   1111111  1    ",
"           1   1  11111     1  11111111          ",]
对于iy,枚举中的行(级别):
对于ix,枚举中的列(行):
如果列==“1”:
新_平台=平台(ix*50,iy*50.2,50,50,(255255))
平台。附加(新的_平台)
#窗户颜色
def redrawwindow():
窗口填充((0,0,0))
#将播放器和其他内容绘制到屏幕上
playerman.draw()
对于平台中的平台:
平台绘制()
对于规则中的规则:
规则.draw()
x=10
y=10
x_变化=0
y_变化=0
old_x=x
老y=y
fps=(30)
clock=pygame.time.clock()
运行=真
运行时:
时钟滴答声(fps)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
    if self.direction == "right":
        imager_list = self.right
    player_image  = image_list[self.anim_index]
    player_imager = imager_list[self.anim_index]    # <-- HERE