0

I am working on an inventory system for a game I am working on, I've wrote these blocks of code for the system so far. I want the system to work without the use of global functions because I've read that you should avoid using global functions.

while not done:
    global inventory
    pygame.mouse.set_visible(False)
    #Event processing
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    elif event.type == pygame.KEYDOWN:
        if event.key == pygame.K_i:
             inv_sys.inventory = True
             inv_sys.inventory_u()

import pygame
import Constants

clock = pygame.time.Clock()
screen = pygame.display.set_mode(Constants.screen_size)
inv = pygame.image.load("inv.png").convert()
test = False

def uninventory():
    global inventory
    inventory = False

def inventory_u():

    while inventory:
        pygame.mouse.set_visible(True)
        screen.blit(inv,(0,0))
        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                pygame.quit
            elif event.type == pygame.KEYDOWN:    
                if event.key == pygame.K_i:
                     uninventory()

         pygame.display.update()
         clock.tick(60)

thank you ahead of time for any help!

Sconce
  • 11
  • 3

3 Answers3

2

because I've read that you should avoid using [global statements]

I think you've misunderstood. The global keyword is not bad in and of itself. Rather, if your implementation requires use of the global keyword then your implementation is bad (in most cases, though there may be exceptions). Instead of writing code with globally-scoped variables, you should try to pass your variables around to the functions.

def main():
    eels = 42
    start_hovercraft(eels)

def start_hovercraft(number_of_eels):
    if number_of_eels > 3:
        raise Exception("My hovercraft is full of eels!")

@PatNowak's suggestion of using a class is also a good way around the problem, but the gist of the issue is that you should avoid global whenever possible because it leads to bad code:

Community
  • 1
  • 1
Pierce Darragh
  • 2,072
  • 2
  • 16
  • 29
1

In general, if you frequently reference a variable whose value is assigned once and not changed, define it at module scope near the top of your file according to the PEP 8 style guide so it is clearly a constant to anyone reading your code. Your import statements should also be at the top of the file.

If it's something you refer to within the program and use to change the state of the program, as you do with global inventory, it (and the methods that refer to it) should probably be part of a class definition which has the variable as a property, as PatNowak describes. You can refer to these methods either inside of or outside of the class (using self or a class instance, respectively) but the property itself should only be directly altered inside the class, or else your code quickly becomes impossible to debug or rewrite.

The global keyword has its uses, but I've never had a reason to use it instead of one of these other two methods.

Ryan I.
  • 21
  • 4
0

Consider why do you want to share one local variable between many functions? Maybe better way for you is to simply create a class, where inventory or something more descriptive will be as a object variable.

class MyGame:
    def __init__():
        # something
        self.inventory = True
    # some code
    def unEquip():
        self.inventory = False
PatNowak
  • 5,721
  • 1
  • 25
  • 31