Jan Camara
Jan Camara

Reputation: 21

Python Project Structure - multiple files and modules

I was used to PHP where a global variable will be available on the whole project, and including/requiring a PHP file is pretty much just like 'injecting' it's code lines on that spot: the whole thing would be 'seen' as a single file, and all vars would be instinctively acessed.

Python works in a different way and I'd like to know good practices in order to make a large project more manageable by splitting it into files...

So lets say I have a file called configs.py that's something like:

FRAME_DURATION = int(1000 / 25)
SCREEN_SIZE = 500

LIGHT_GREY  = (190,190,190)
RED = (255,0,0)
GREEN = (0,255,0)
YELLOW = (255,255,0)
#etc...

Then, on the Main.py file I could do something like:

import pygame
from configs import *

pygame.init()   
SCREEN = pygame.display.set_mode((SCREEN_SIZE, SCREEN_SIZE),1)
SCREEN.fill(LIGHT_GREY)

run = True
while run:
    pygame.time.delay(FRAME_DURATION )

    #HERE I'M SUPPOSED TO CHECK THE KEYBOARD AND MOUSE EVENTS

    pygame.display.update()

pygame.quit()

And this work just fine since we're handling only variables. But if i want to make the event listener into another separate file I run into some 'troubles'. Let's say I want a file with all my functions. I'll name it actions.py and it contains the following function:

def checkEvents():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                run = False
            if event.key == pygame.K_r:
                pass

If I want to be able to call the checkEvents() function inside the main.py file I get a NameError sinceit doesn't recognizes pygame module inside the actions.py file.

I can't seem to make it work: if i import it on both files (which i know is sup-optimal), the game launches but keys and exit key don't do nothing. I guess the checkEvents() function is listening to another instance of pygame?

Anyway, I'd like to know if there's a way to make modules 'globally available'... Importing it once on main file and using it on the whole project, including inside other imported custom modules?

The final result should look something like this:

import pygame
from configs import *
import actions

pygame.init()   
SCREEN = pygame.display.set_mode((SCREEN_SIZE, SCREEN_SIZE),1)
SCREEN.fill(LIGHT_GREY)

run = True
while run:
    pygame.time.delay(FRAME_DURATION )

    actions.checkEvents()

    pygame.display.update()

pygame.quit()

Upvotes: 2

Views: 2312

Answers (1)

furas
furas

Reputation: 142631

You have to only import pygame in actions.py

Other problem is run which you have to return from checkEvents()

import pygame 

def checkEvents():
    run = True # local variable with default value before all tests

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False # set local variable

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                run = False # set local variable
            if event.key == pygame.K_r:
                pass

    return run # return local value to main code

And then you get run in main.py

import pygame
from configs import *
import actions

pygame.init()   
SCREEN = pygame.display.set_mode((SCREEN_SIZE, SCREEN_SIZE),1)
SCREEN.fill(LIGHT_GREY)

run = True
while run:
    pygame.time.delay(FRAME_DURATION )

    run = actions.checkEvents() # get result from function and assign to `run`

    pygame.display.update()

pygame.quit()

Upvotes: 1

Related Questions