TkinterEP / ttkthemes

A group of themes for the ttk extenstions for Tkinter
GNU General Public License v3.0
364 stars 47 forks source link

name 'Game' is not defined in a file but not in another one #82

Closed sonwayy closed 3 years ago

sonwayy commented 3 years ago

Hi here is my issue: i imported a class from a file to 2 other files but it's working only in 1 of them and the issue code is: name 'Game' is not defined and when i import that class with 'from game import Game' it says: 'cannot import name 'Game' from partially initialized module 'game'

here is my 'game.py' file:

import pygame as pg
from settings import *
from pygame import gfxdraw
from UI_backup import *
from Logic import *

class Game():
    def __init__(self):
        self.UI = UI()
        self.Logic = logic()
        self.is_playing = False

    def start(self):
        self.is_playing = True

    def stop(self):
        self.is_playing = False
        self.continuer = False
        pg.mixer.music.stop()

here is my first file wich the import works fine, named 'Mastermind.py':

from random import randint
import copy
import pygame as pg
import sys
from game import *
from Logic import *
from UI_backup import *

couleurs = ["R","Y","G","B","O","W","C","P"]
code = [ couleurs[randint(0,len(couleurs)-1)] for _ in range(4)]
game = Game()
UI = UI()
logic = logic()

UI.essai()
UI.arrow()
UI.circles()

while UI.continuer:
    UI.ecoute()
    if game.is_playing:
        game.start()
    else:
        #UI.lobby()
        pass
    pg.display.flip()

pg.display.quit()

and the second file wich the import is not working named 'UI_backup.py':

import pygame as pg
from pygame import gfxdraw
import Logic
from game import Game
from settings import*
import math

class UI():
    def __init__(self):
        pg.init()
        self.song = pg.mixer.music.load("playback.mp3")
        pg.mixer.music.play()
        pg.mixer.music.set_volume(0.15)
        self.continuer = True
        self.music_play = True
        self.colors = colors
        self.noms = color_names
        self.pointeur = False
        self.fenetre = pg.display.set_mode((width, height), pg.RESIZABLE)
        self.font = pg.font.SysFont('Arial', 25)
        self.fond = pg.image.load("Mastermind-fond.png").convert()
        self.banner = pg.image.load("play.png")
        self.banner = pg.transform.scale(self.banner, (695,720))
        self.no_music_icon = pg.image.load("no_music.png")
        self.no_music_icon = pg.transform.scale(self.no_music_icon, (100,100))
        self.music_icon = pg.image.load("music.png")
        self.music_icon = pg.transform.scale(self.music_icon, (100,100))
        self.music_icon_rect = self.music_icon.get_rect()
        self.music_icon_rect.x = 563
        self.music_icon_rect.y = 0
        self.fenetre.blit(self.fond, (0,0))
        self.fenetre.blit(self.music_icon, (self.music_icon_rect))
        self.pile = [0,0,0,0]
        self.clickable, self.clickable_essai, self.clickable_arrow = [], [], []

    def lobby(self):
        self.fenetre.blit(self.banner, (0,0))

    def addRect(self):
        pg.draw.rect(self.fenetre, (0,0,0), (590,0,50,120), 10)
        pg.display.flip()

    def arrow(self):
        for i in range(10):
            self.clickable_arrow.append(pg.Rect( (300,520-45*i), (100,30) ))
            pg.draw.rect(self.fenetre,(0,0,0),(300,520-45*i,100,30))
        pg.display.flip()

    def essai(self):
        for i in range(4):
            self.clickable_essai.append(pg.Rect((101+45.5*i,515),(40,40)))

    def circles(self):
        for i in range(8):    
            self.clickable.append(pg.Rect((80+50*i, 580), (40, 40)))
            pg.gfxdraw.aacircle(self.fenetre, 100+50*i, 600, 20,self.colors[i])
            pg.gfxdraw.filled_circle(self.fenetre, 100+50*i, 600, 20,self.colors[i]) 

    def ecoute(self):
        for event in pg.event.get():
            if event.type == pg.QUIT or (event.type == pg.KEYDOWN and event.key == (pg.K_LALT and pg.K_F4)):
                self.continuer = False
                pg.mixer.music.stop()
                return
            if event.type == pg.MOUSEBUTTONDOWN:
                for click in self.clickable:
                    if click.collidepoint(pg.mouse.get_pos()):
                        print("vous avez cliqué sur", self.noms[self.clickable.index(click)] )
                        self.pointeur = self.colors[self.clickable.index(click)]
                        print(self.pointeur)
                        return

            if event.type == pg.MOUSEBUTTONUP and self.pointeur:
                for area in self.clickable_essai:
                    if area.collidepoint(pg.mouse.get_pos()):
                        pg.gfxdraw.aacircle(self.fenetre, area[0]+20, area[1]+20, 20,self.pointeur)
                        pg.gfxdraw.filled_circle(self.fenetre, area[0]+20, area[1]+20, 20,self.pointeur)
                        self.pile[self.clickable_essai.index(area)] = self.pointeur
                        print(self.pile)

            if event.type == pg.MOUSEBUTTONUP:
                for arrow in self.clickable_arrow:
                    if arrow.collidepoint(pg.mouse.get_pos()):
                        self.Logic.verif(self.Logic.code,self.pile)

            if event.type == pg.MOUSEBUTTONDOWN:
                if self.music_play == True and self.music_icon_rect.collidepoint(event.pos):
                    pg.mixer.music.pause()
                    self.music_play = False
                    self.fenetre.blit(self.no_music_icon, (self.music_icon_rect))
                    return

            if event.type == pg.MOUSEBUTTONDOWN:
                if self.music_play == False and self.music_icon_rect.collidepoint(event.pos):
                    pg.mixer.music.unpause()
                    self.music_play = True
                    self.fenetre.blit(self.music_icon, (self.music_icon_rect))

            if event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE:
                UI().lobby()
                game().stop()
RedFantom commented 3 years ago

I'm not sure what this has to do with ttkthemes, but you have a circular import in your modules.

rdbende commented 3 years ago

Wrong repo