0

I have two different files like this:

file name Game.py

from objects import *
...
player = Player()
self.screen.blit(player.draw[0], player.draw[1])
...

and

file name Player.py

class Player
  def __init__(self)
    player = pygame.Rect(0, 0, Tilewidth, Tileheight)
    player_image = pygame.image.load('media/player.jpg')
    player_stretch_image = pygame.transform.scale(player_image, (Tilewidth, Tileheight))
  ...
  def draw
    array = [player_stretch_image, player]
    return array

And the folders looks like this

objects/
    __init__.py
    player.py
Game.py

This doesn't work and I don't know why. I want to use some of the variables in the other file without using "import everything".

Is there any alternative way to make variable global using methods or should I make a third file containing all the variables and then importing them into the files?

I'm still pretty new to Python so I don't really know the way to solve this problem.

Laurel
  • 5,965
  • 14
  • 31
  • 57

2 Answers2

0

In Python, 'import' can easily reach any files or folders with the same filepath. Therefore, why not put the Game and Player files in the same folder? If you do, you could import select variables using :

from Game import <variable>, <another variable>, <etc...>

in the Player file, and reference them by those names in the file (with no module.var required). Otherwise I recommend having a look at this reply if you want to keep the python files in different filepaths:

How to import a module given the full path?

Edit: I can see what you're doing by importing the contents of the objects file into Game, however since you also want to be able to access Game variables from inside the Player file I would say that you could either:

  • keep the variables is a file inside the objects folder, where it would be both accessible to the Game object and Player object

  • or simply put the Game object in the same folder as the Player object, where the variables could be kept in Game and accessed with the above code.

Hope this helps!

Community
  • 1
  • 1
Almonso
  • 51
  • 4
  • This makes sense :) I thought that there were no difference between import * and import .... Thanks for clearing it up! – TawnyPeach04 Jul 06 '16 at 10:19
0

Instead of from objects import * try in Game.py

from objects.player import Player

It's good practice avoid using wildcard imports (*) to avoid namespace polution as explained here. Instead choose what you need from the other module and import explicitly.

Jad S
  • 2,705
  • 6
  • 29
  • 49
  • Thanks for your tip! I will never use (*) again, since its destroying my variables names :D – TawnyPeach04 Jul 06 '16 at 10:21
  • Did that solve your issue? I might have misunderstood your question. What are you trying to make global? – Jad S Jul 07 '16 at 13:19
  • yea it was exactly what i wanted :) I wanted to import the variable with its original name and this did the trick, sry if i was question was a little vague. – TawnyPeach04 Jul 08 '16 at 20:51
  • No problem TawnyPeach, glad to help. If this answer satisfied you, I'd appreciate if you accepted it :) – Jad S Jul 09 '16 at 17:00