How to access a global object inside of a class?

73 views Asked by At

I need to access a global object inside of a class to change it's image. Here's the code:

import pygame
from livewires import games
from livewires import color
import time
import random

games.init(screen_width = 640, screen_height = 480, fps = 50)

class Cursor(games.Sprite):
    """The pokemon based cursor!"""

    def __init__(self, image, x ,y):
        super().__init__(image=image, x=x, y=y)



    def update(self):


        self.x = games.mouse.x
        self.y = games.mouse.y   
        self.check_collide()

    def check_collide(self):
        for sprite in self.overlapping_sprites:
            sprite.handle_collide()



class Play_Button(games.Sprite):
    """The 'Play' button on the menu."""

    def __init__(self , image, x ,y):
        super().__init__(image=image, x=x, y=y)

    def handle_collide(self):
        global play_obj

        play_image2 = games.load_image("playbtn2.png", transparent = True)
        play_obj.value = play_image2
        print("COME ON!")


class P1C_Button(games.Sprite):

    def __init__(self, image, x, y):
        super().__init__(image=image, x=x, y=y)

class Logo(games.Sprite):

    def handle_collide(self):
        print("Collision ignored.")



play_image = games.load_image("playbtn.png", transparent = True)
play_obj = Play_Button(image = play_image,
                x = games.screen.width/2,
                y = games.screen.height/2)
games.screen.add(play_obj)


logo_image = games.load_image("FamilyMon.png", transparent = True)
logo_obj = Logo(image = logo_image,
                x = games.screen.width/2,
                y = 75)
games.screen.add(logo_obj)

white_image = games.load_image("white.png", transparent = False)
games.screen.background = white_image

cursor_image = games.load_image("cursor.png", transparent = True)
cursor_obj = Cursor(image = cursor_image,
                    x = games.mouse.x,
                    y = games.mouse.y)
games.screen.add(cursor_obj)
games.mouse.is_visible = False
games.screen.event_grab = True

games.screen.mainloop()

The important part of that is inside of 'Play_Button''s handle_collide method. It's trying to access the object created, 'play_obj' but the code seems to do nothing. When the mouse sets of Play_Button's handle_collide nothing. I've tried my best here so if this seems like a stupid question than sorry, because i'm new.

2

There are 2 answers

0
Kevin J. Chase On BEST ANSWER

You shouldn't be using a global variable in the first place.

You only create one Play_Button object, so the global play_obj always is self. You could just have that object set its own field:

self.value = play_image2
0
boaz_shuster On

I am trying to understand why doing this in the following way doesn't satisfy you

def handle_collide(self):
    play_image2 = games.load_image("playbtn2.png", transparent = True)
    self.value = play_image2
    print("COME ON!")

...

From what I see, there is only one Play_Button instance, which you want to change its image when collide occurs.

Then, why not using self.value?