How can I do that without needing this global variable?

Asked

Viewed 40 times

0

The problem I have is the following I have a method that should be called 4 times, once every 750 ms. The problem is that the way it is called I can’t pass arguments to it, why it is called via pygame.time.set_timer. So I created a global variable (because it would be even worse if I created an attribute called n) to use in this method but it doesn’t seem like the most beautiful way to do it. How could I fit that code?

global n
n = 0

def shuffle(self):
        self.card_shuffle.play()
        random.shuffle(self.cards)
        for n, card in enumerate(self.cards):
            card.flip("back")
            card.rect.center = [self.screen_rect.centerx - n * 0.5,
                                self.screen_rect.centery - n * 0.5]
        self.human.cards.clear()
        self.ia.cards.clear()
        pygame.time.set_timer(USEREVENT, 750)

def give_cards(self):
        global n
        if n < 6:
            self.cards[n].flip("front")
            self.cards[n].rect.midbottom = [self.screen_rect.centerx + 70 * (
                n / 2 - 1), self.screen_rect.bottom]
            self.human.cards.append(self.cards[n])
            self.cards[n + 1].rect.midtop = [self.screen_rect.centerx + 70 * (
                n / 2 - 1), self.screen_rect.top]
            self.ia.cards.append(self.cards[n + 1])
            n += 2
        else:
            self.cards[n].rotoflip("front", -90, {
                "center": self.cards[n + 1].rect.midright})
            self.card_vira = self.cards[n]
            pygame.time.set_timer(USEREVENT, 0)
            n = 0

def handler_events(self):
        for event in pygame.event.get():
            if event.type == QUIT:
                self.running = False
            if event.type == USEREVENT:
                self.give_cards()

1 answer

0

It seems to me that shuffle(), give_cards() and handler_events() are methods of a class! n could be an attribute of your class, look at the class CardGame, based on the code of your question:

class CardGame

    def __init__( self ):
        self.n = 0
        self.vagas_ocupadas = 0

    def shuffle(self):
            self.card_shuffle.play()
            random.shuffle(self.cards)
            for i, card in enumerate(self.cards):
                card.flip("back")
                card.rect.center = [self.screen_rect.centerx - i * 0.5,
                                    self.screen_rect.centery - i * 0.5]
            self.human.cards.clear()
            self.ia.cards.clear()
            pygame.time.set_timer(USEREVENT, 750)

    def give_cards(self):
            if self.n < 6:
                self.cards[self.n].flip("front")
                self.cards[self.n].rect.midbottom = [self.screen_rect.centerx + 70 * (
                    self.n / 2 - 1), self.screen_rect.bottom]
                self.human.cards.append(self.cards[self.n])
                self.cards[self.n + 1].rect.midtop = [self.screen_rect.centerx + 70 * (
                    self.n / 2 - 1), self.screen_rect.top]
                self.ia.cards.append(self.cards[self.n + 1])
                self.n += 2
            else:
                self.cards[self.n].rotoflip("front", -90, {
                    "center": self.cards[self.n + 1].rect.midright})
                self.card_vira = self.cards[self.n]
                pygame.time.set_timer(USEREVENT, 0)
                self.n = 0

    def handler_events(self):
            for event in pygame.event.get():
                if event.type == QUIT:
                    self.running = False
                if event.type == USEREVENT:
                    self.give_cards()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.