PYGAME:UnboundLocalError:分配前引用的局部变量

问题描述

如果我把我的代码放到类中,它就不会像我期望的那样工作,以及在我把它放到类中之前它是如何工作的。目标是能够在我的主循环中调用类并能够更新它们。

该程序也不会在屏幕上呈现我试图显示的任何内容。查看第二个代码段。

这有效...

import pygame
from pygame.locals import *

clock = pygame.time.Clock()
fps = clock.tick(60)

winx = 900
winy = 480

pygame.init()
win = pygame.display.set_mode((winx,winy))
pygame.display.set_caption('Game Test')

class spawning():
    def spawn():
        ecolor = (0,255,0)
        pcolor = (255,0)
        enemy = pygame.draw.rect(win,(ecolor),(hx,hy,hwidth,hheight))
        player = pygame.draw.rect(win,(pcolor),(charx,chary,width,height))
        if player.colliderect(enemy):
            pygame.quit()

    def enemyspawn():
        spawn_chance = rd.randint(0,10)
        if spawn_chance == 10:
            sx = rd.randint(0,winx)
            sy = rd.randint(0,winy)
            new_enemy = enemy((sx,sy))
            print('spawned')

#Hostile Control Variables
hvel = 2
hx = 20
hy = 20
hheight = 20
hwidth = 20
#player control variables
charx = winx / 2
chary = winy / 2
width = 20
height = 20
vel = 1

run = True

while run:
    pygame.time.delay(10)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT] and charx>0:
        charx -= vel * fps / 2
    if keys[pygame.K_RIGHT] and charx<winx-width:
        charx += vel * fps / 2
    if keys[pygame.K_UP] and chary>0:
        chary -= vel * fps / 2
    if keys[pygame.K_DOWN] and chary<winx-height:
        chary += vel * fps / 2
        
    #ensure bottom boundary isn't crossed
    if chary > winy - 25:
        chary = winy - 25
        print(chary)

    hostile_vector = pygame.Vector2(hx,hy)
    player_vector = pygame.Vector2(charx,chary)
##    towards = (player_vector - hostile_vector).normalize() * hvel

    if hostile_vector != player_vector:
        if hx == charx:
            pass
        elif hx <= charx:
            hx = hx + hvel
        elif hx >= charx:
            hx = hx - hvel
        if hy == chary:
           pass
        elif hy <= chary:
            hy = hy + hvel
        elif hy >= chary:
            hy = hy - hvel

    win.fill((0,0))
    spawning.spawn()
    pygame.display.update() 
pygame.quit()

但这不起作用。根本

import random as rd
import pygame
from pygame.locals import *

clock = pygame.time.Clock()
fps = clock.tick(60)

winx = 900
winy = 480

pygame.init()
win = pygame.display.set_mode((winx,winy))
pygame.display.set_caption('Game Test')

def foobar():
    global hvel
    global hx
    global hy
    global hheight
    global hwidth
    global charx
    global chary
    global width
    global width
    global height
    global vel
    #Hostile Control Variables
    hvel = 2
    hx = 20
    hy = 20
    hheight = 20
    hwidth = 20
    #player control variables
    charx = winx / 2
    chary = winy / 2
    width = 20
    height = 20
    vel = 1


foobar()

class management():
    def playersp():
        pcolor = (255,0)
        player = pygame.draw.rect(win,height))
    def enemyUpdate():
        ecolor = (0,hheight))
        hostile_vector = pygame.Vector2(hx,hy)
        player_vector = pygame.Vector2(charx,chary)
        if hostile_vector != player_vector:
            if hx == charx:
                pass
            elif hx <= charx:
                hx = hx + hvel
            elif hx >= charx:
                hx = hx - hvel
            if hy == chary:
               pass
            elif hy <= chary:
                hy = hy + hvel
            elif hy >= chary:
                hy = hy - hvel

run = True

while run:
    pygame.time.delay(10)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT] and charx>0:
        charx -= vel * fps / 2
    if keys[pygame.K_RIGHT] and charx<winx-width:
        charx += vel * fps / 2
    if keys[pygame.K_UP] and chary>0:
        chary -= vel * fps / 2
    if keys[pygame.K_DOWN] and chary<winx-height:
        chary += vel * fps / 2
        
    #ensure bottom boundary isn't crossed
    if chary > winy - 25:
        chary = winy - 25
        print(chary)

    win.fill((0,0))
    management.playersp()
    management.enemyUpdate()
    pygame.display.update() 
pygame.quit()

非常感谢你们。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)