1

I am making a game with pygame and I have a file called "settings_template.py" and another file called "pygame_functions.py" and when I import in my variables it gives me an error "NameError: name 'FPS' is not defined" however, I defined "FPS" in "settings_template.py". I don't know what the problem is and I've tried everything.

This is in my "settings_template.py"

FPS = 10 # how fast the game runs (frames per second)

and this is in my "pygame_functions.py"

from settings_template import *

def showSprite(sprite):
    global FPS
    spriteGroup.add(sprite)
    gameClock.tick(FPS)  # changed updateDisplay to tick for better framerate

I also tried it without global FPS but it made no difference. If anyone could help me out I will be eternally grateful :)

sloth
  • 99,095
  • 21
  • 171
  • 219
joseph gil
  • 13
  • 3
  • 1
    Possible duplicate of [What's the best way to initialise and use contants across Python classes?](https://stackoverflow.com/questions/6345840/whats-the-best-way-to-initialise-and-use-contants-across-python-classes) – Mihai Chelaru Jun 07 '18 at 03:09
  • 1
    How are you importing your pygame_function.py in settings_template.py? Is `FPS` define in the global scope already in pygame_function.py? – Miket25 Jun 07 '18 at 03:09
  • 1
    The problem cannot be reproduced from the code you provided. @Miket25 may have guessed at the issue (if FPS is defined in a function). You may need to copy over more of your code. Especially if there is any more code in your settings_template.py – Zev Jun 07 '18 at 03:24

1 Answers1

0

You can try like:

settings_template.py

FPS = 10

pygame_functions.py

import settings_template

def showSprite(sprite):
    spriteGroup.add(sprite)
    gameClock.tick(settings_template.FPS)

Explanation:

Import the file settings_template.py then you can access the variables which are declared by settings_template.<var_name>