Error Global Variable not defined when importing class

前端 未结 2 1542
温柔的废话
温柔的废话 2021-01-28 20:45

I am trying to implement a movement system where the player move\'s towards the click position. But I have been running into some problems with the arguments of the movement met

2条回答
  •  清歌不尽
    2021-01-28 20:59

    So after a bit more work I found the solution to my problem.

    I only had two python files which both imported each other.

    I had to create a third file to avoid this "circular" importation.

    I made one file with my main code called main.py importing both the classes.py file and the variables.py file.

    main.py:

    import pygame, sys, variables
    from pygame.locals import *
    from classes import *
    
    #Creating the characters
    objects = []
    objects.append(GameObject(variables.player, 80, 0))
    for x in range(2):      #create 2 objects
        o = GameObject(variables.ennemi, x*40, 0)
        objects.append(o)
    
    #game loop    
    while True:
        for event in pygame.event.get(): #setting up quit
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == MOUSEBUTTONDOWN : #initiating movement on click
    
                for o in objects:
                    variables.screen.blit(variables.background, o.pos, o.pos) #erases players by bliting bg 
                for o in objects:
                    o.move(event)
        pygame.display.update()
        pygame.time.delay(20)
    

    classes.py:

    import pygame, sys, variables
    from pygame.locals import *
    
    class GameObject:
        def __init__(self, image, height, speed):
            self.speed = speed
            self.image = image
            self.pos = image.get_rect().move(0, height) #initial placement
    
        def move_NS(self):
            self.pos = self.pos.move(0, self.speed)
            if self.pos.right > 600:
                self.pos.left = 0
    
        def move_EW(self):
            self.pos = self.pos.move(self.speed , 0)
            if self.pos.right > 600:
                self.pos.left = 0
    
    
        def move(self,event):#,mouse_pos, screen, background
                if event.type == MOUSEBUTTONDOWN:
                    if pygame.mouse.get_pos()[1] > self.pos[1]:
                        variables.screen.blit(variables.background, self.pos, self.pos) #erases players by bliting bg 
                        self.speed = 1
                        self.move_NS() #moves player
                    if pygame.mouse.get_pos()[0] > self.pos[0]:
                        variables.screen.blit(variables.background, self.pos, self.pos) #erases players by bliting bg 
                        self.speed = 1
                        self.move_EW() #moves player
                    if pygame.mouse.get_pos()[1] < self.pos[1]:
                        variables.screen.blit(variables.background, self.pos, self.pos) #erases players by bliting bg 
                        self.speed = -1
                        self.move_NS() #moves player
                    if pygame.mouse.get_pos()[0] < self.pos[0]:
                        variables.screen.blit(variables.background, self.pos, self.pos) #erases players by bliting bg 
                        self.speed = -1
                        self.move_EW() #moves player
    
                    variables.screen.blit(self.image, self.pos) #draws player     
    

    variables.py:

    import pygame, sys
    from pygame.locals import *
    #Variables
    screen = pygame.display.set_mode((640, 480))
    
    #Importing Chars
    player = pygame.image.load('green_hunter_small.png').convert()
    player.set_colorkey((0,0,0)) #sets background colour to transparent
    
    ennemi =  pygame.image.load('red_hunter_small.png').convert()
    ennemi.set_colorkey((0,0,0))
    
    #importing background
    background = pygame.image.load('grass_map_640x640.png').convert()
    screen.blit(background, (0, 0))
    

    Hope it helps !

提交回复
热议问题