python - module not setting main files variable content -
python - module not setting main files variable content -
using pygame, have made main menu few buttons on it. can observe when button pressed , it.
however code controls happens when button pressed in file (using getattr) , seems causing issues.
i using variable menu_open
command when things relating menu should done. when game starts , after one-off dev warning shows (works fine), set true. works intended until click on new game
button. should create blank screen. nil happens.
i have discovered menu_open
still true
. appears happening code controls new game
button in file , reasons cannot understand seems working different version of menu_open
main file is. (it's not setting main files menu_open
false
although testing print statement prints false
)
code controls happens when button pressed:
def new_game(): print('starting new game') import main main.menu_open=false print(2,main.menu_open)
start of program:
import pygame,commands #line 1 done = false menu_open= false #deceleration of menu_open @ start of programme game_playing = false
code updates menu (should create white screen when menu_open
false):
def display_frame(self,screen): global menu_open print(1,menu_open) screen.fill(white) if menu_open: screen.blit(menu_image,[0,0]) button in button_list: button.draw() pygame.display.flip()
code causes button control:
def run_logic(self): #worth noting called right before display_frame() global mouse_pos,mouse_press mouse_pos = pygame.mouse.get_pos() mouse_press = pygame.mouse.get_pressed() button in button_list: button.check() #runs following: def check(self): if self.hovered: if mouse_press[0] == true: try: command_to_call = getattr(commands,self.command) command_to_call() except: print('[dev]: invalid command')
result of print statements:
1 true # button not pressed 1 true # true here main files 'menu_open' 1 true 1 true 1 true 1 true starting new game #button pressed 2 false #false other files 'menu open' 1 true # true here main files 'menu_open' starting new game 2 false 1 true starting new game 2 false 1 true starting new game 2 false 1 true #button released, menu still normal 1 true 1 true 1 true
i'm not experienced multi-file programming help appreciated. may worth noting ide (pyscripter) bugs out alot pygame. button command far has worked fine. have made quit button using it.
if need more code programme sense free inquire :) if code fine , bug python/pyscripter/pygame please so.
the right way prepare move variable separate module. i'll explain you're doing wrong regardless.
presumably "start of [...] program" in file called main.py
. , when want access module import path/filename, in case main
. however, first script invoked interpreter not named after path/filename, instead always named __main__
regardless of else. right way import it, not should ever it, utilize import __main__
, access via name.
python module pygame
Comments
Post a Comment