首页 文章

Python,Pygame,赋值前引用的局部变量

提问于
浏览
0

Pygame相对较新 . 我正试图在按下向下箭头键的同时,从子弹(xeon)像子弹一样移动矩形 . 我一直收到这个错误 . “UnboundLocalError:赋值前引用的局部变量'项目符号'”

任何反馈都非常感谢 . 请原谅...........................

import pygame
import time
import random
import sys
import os
pygame.init()
display_width=800
display_height=600
black=(0,0,0)
white=(255,255,255)
red=(200,0,0)
green=(0,200,0)
blue=(0,0,200)
bright_green=(0,255,0)
bright_red=(255,0,0)
gameDisplay=pygame.display.set_mode((800,600))  
pygame.display.set_caption("Xeongame")
clock=pygame.time.Clock()
zheImg=pygame.image.load("xeon.png").convert()
bgImg=pygame.image.load("Spacebackground.jpg").convert()
xeon_width=300







def xeon(x,y):

    gameDisplay.blit(zheImg,(x,y))




def bullets(x,y,bx,by,bw,bh,color):
    while True:
        pygame.draw.rect(gameDisplay,color,[bx,by,bw,bh])

def quitgame():
    pygame.quit()
    quit()

def text_objects(text,font):
    textSurface=font.render(text,True,black)
    return textSurface,textSurface.get_rect()

def obstacles(obstaclex,obstacley,obstaclew,obstacleh,color):
    pygame.draw.rect(gameDisplay,color,[obstaclex,obstacley,obstaclew,obstacleh])




def message_display(text):
    largeText=pygame.font.Font("freesansbold.ttf",115)
    TextSurf,TextRect=text_objects(text,largeText)
    TextRect.center=((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf,TextRect) 
    pygame.display.update()
    time.sleep(1)
    game_loop()

def button(msg,x,y,w,h,ic,ac,action=None):
    mouse=pygame.mouse.get_pos()
    click=pygame.mouse.get_pressed()

    if x+w>mouse[0]>x and y+h>mouse[1]>y:
        pygame.draw.rect(gameDisplay,ac,(x,y,w,h))
        if click[0]==1 and action !=None:
            action()
            if action=="Play":
                game_loop()
            elif action=="Quit":
                pygame.quit()
                quit()

    else:
        pygame.draw.rect(gameDisplay,ic,(x,y,w,h))  
    smallText=pygame.font.Font("freesansbold.ttf",20)
    textSurf,textRect=text_objects(msg,smallText)
    textRect.center=(   (x+(w/2)),(y+(h/2)) )
    gameDisplay.blit(textSurf,textRect)
    largeText=pygame.font.Font("freesansbold.ttf",115)


def game_intro():
    intro=True

    while intro:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                quit()
        gameDisplay.fill(white)
        largeText=pygame.font.Font("freesansbold.ttf",115)
        TextSurf,TextRect=text_objects("Xeon",largeText)
        TextRect.center=((display_width/2),(display_height/2))
        gameDisplay.blit(TextSurf,TextRect)
        xeon(10,100)
        button("Play",150,450,100,50,green,bright_green,game_loop)
        button("Quit",550,450,100,50,red,bright_red,quitgame)




        pygame.display.update()
        clock.tick(15)

def game_loop():



    x=(display_width*.10)
    y=(display_height*0.50)
    x_change=0
    y_change=0
    x+=x_change
    y+=y_change


    ##ALTERNATIVE JUMPING
    #xeon_gravity=0.8
    #xeon_acc=0.5


    obstacle_speed=7
    obstacle_height=100
    obstacle_width=50
    obstacle_startx=random.randrange(300,display_width)
    obstacle_starty=-100

    ##bullets
    bullets_height=10
    bullets_width=50
    bullets_startx=xeon_width
    bullets_starty=xeon_width
    bullets_speed=0.7

    bullets_x_change=0





    ####KEYS



    gameExit=False
    while not gameExit:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                quit()
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_RIGHT:
                    x_change=-5
                if event.key==pygame.K_LEFT:
                    x_change=+5
                if event.key==pygame.K_UP:
                    y_change=-5




                if event.key==pygame.K_DOWN:
                    bullets=True
                    pygame.draw.rect(gameDisplay,[bx,by,bw,bh,color])
                    bullets_x_change=-5                 


            if event.type==pygame.KEYUP:
                if event.key==pygame.K_LEFT or event.key==pygame.K_RIGHT:
                    x_change=0  
        x+=x_change
        y+=y_change

        rel_x=x%bgImg.get_rect().width
        gameDisplay.blit(bgImg,(rel_x-bgImg.get_rect().width,0))

        gameDisplay.blit(bgImg,(rel_x,0))
        xeon(x_change,y)



        bullets(bullets_startx,bullets_starty,bullets_width,bullets_height,blue)
        bullets_starty+=bullets_speed   



        obstacles(obstacle_startx,obstacle_starty,obstacle_width,obstacle_height,red)
        obstacle_starty+=obstacle_speed


        if obstacle_starty>display_height:
            obstacle_starty=0-obstacle_height
            obstacle_startx=random.randrange(300,display_width)





        pygame.display.update()
        clock.tick(60)







game_intro()
game_loop()
pygame.quit()
quit()

1 回答

  • 1

    问题是你有一个名为 bullets 的局部变量(在 game_loop 函数中)和一个具有相同名称的全局函数 . 当您在 game_loop 中调用函数 bullets() 时,Python认为您指的是尚未分配任何内容的局部变量,因此会引发 UnboundLocalError .

    重命名函数或变量以避免 UnboundLocalError .

    Here's an answer了解更多信息 .


    你也没有将足够的参数传递给 bullets 函数 .

相关问题