0
votes

i'm trying to access variables and functions from "juego.py" file, which is in the same folder, but i'm getting an error. The current file name is "pantalla_fin.py"

Error: File "c:\Users\danie\OneDrive\Escritorio\PROGRAMACIÓN\ArchivosPyhton\Pong\view\pantalla_fin.py", line 5, in game_over_y = juego.VENTANA_VERTICAL/3 AttributeError: partially initialized module 'juego' has no attribute 'VENTANA_VERTICAL' (most likely due to a circular import)

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")

1
Please edit your question to create a minimal reproducible example, it's difficult to assist without the source of juego.py. - import random

1 Answers

-1
votes

I had the same problem in the past, and the reason I ran into it is because you, like me then, have probably created a circular dependency between your code files. Let me explain me better: if you create a game.py file that calls some functions or variables of your screen_fin.py file, and it also call functions from game.py, you have two files calling each other and that makes a lot of problems. When I changed the file imports in my project, all these problems disappeared. But if you want, here you can see more: https://stackabuse.com/python-circular-imports/

I hope that was the point XD