AttributeError:部分初始化的模块“juego”没有属性“VENTANA_VERTICAL”(很可能是由于循环导入)

2024-10-01 17:38:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从“juego.py”文件中访问变量和函数,该文件位于同一文件夹中,但我得到了一个错误。当前文件名为“pantalla_fin.py”

错误: 文件“c:\Users\danie\OneDrive\Escritorio\PROGRAMACIÓN\ArchivosPyhton\Pong\view\pantalla_fin.py”,第5行,在game_over_y=juego.VENTANA_垂直/3中 AttributeError:部分初始化的模块“juego”没有属性“VENTANA_VERTICAL”(很可能是由于循环导入)

import juego
import pygame

# Coordinates depending on the message
game_over_y = juego.VENTANA_VERTICAL/3
pulsar_y = juego.VENTANA_VERTICAL/3+75

# Function that draws the text 
def escribir_mensaje(tamaño, frase, y):
    font = pygame.font.Font("C:/Users/danie/OneDrive/Escritorio/PROGRAMACIÓN/ArchivosPyhton/Pong/imagenes_y_fuentes/fuente_numeros.ttf", tamaño)
    ancho = font.height(frase)
    x = juego.VENTANA_HORIZONTAL/2 - ancho/2
    
    mensaje = font.render(frase, True, (255, 255, 255))
    juego.ventana.blit(mensaje, (x, y))

# Main function
def game_over(ganador):
    
    running = True
    while running:
        juego.ventana.fill(juego.NEGRO)
        if ganador == "score1":
            escribir_mensaje(64, "You have won", game_over_y)
        elif ganador == "score2":
            escribir_mensaje(64, "Game Over", game_over_y)
        escribir_mensaje(16, "Click to restart", pulsar_y)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button==1:
                    juego.main()

        pygame.display.flip()
        pygame.time.Clock().tick(juego.FPS)

    pygame.quit()

game_over("score1")


Tags: 文件pyeventgameifpygameoverfont
1条回答
网友
1楼 · 发布于 2024-10-01 17:38:40

我在过去也遇到过同样的问题,我之所以遇到这个问题,是因为您和我一样,可能在代码文件之间创建了循环依赖关系。让我更好地解释一下:如果您创建一个game.py文件来调用screen_fin.py文件中的一些函数或变量,并且它也调用game.py中的函数,那么有两个文件相互调用,这会产生很多问题。当我在项目中更改文件导入时,所有这些问题都消失了。但如果您愿意,您可以在这里看到更多:https://stackabuse.com/python-circular-imports/

我希望这就是XD的要点

相关问题 更多 >

    热门问题