How can I do this without needing this global variable?

0

The problem I have is the following I have a method that should and called 4 times, once every 750 ms. The problem is that the way it is called I can not pass arguments to it, because it is called through pygame.time.set_timer. So I created a global variable (because it would be even worse if I created an attribute called n) to be able to use this method but it does not seem to me the most beautiful way to do it. How could I fit this 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()
    
asked by anonymous 09.06.2018 / 17:30

1 answer

0

It seems like shuffle() , give_cards() and handler_events() are methods of a class! n could be an attribute of your class, see only 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()
    
10.06.2018 / 13:58