-1

The goal is to generate 25 objects using the same class.

I am currently using this code to create the object:

class Card:
    def __init__(self,pos):
        self.flipping = False
        self.images = loadanimationimages()
        self.frame = 0
        self.pos = pos
    def flip():
        self.flipping = True
    def update():
        if self.flipping:
            self.frame += 1
            self.frame %= len(self.images)
    def draw(screen):
        screen.blit(pygame.transform.smoothscale(self.images[self.frame],
        self.pos),55*scale).convert_alpha() #Continued.

def updatecards():  #Create the cards.
  cards = []
  for x in range(5):
     for y in range(5):
        cards.append(Card((x*92*scale+offsetx*scale,y*92*scale+offsety*scale)))

I know I have to call card.flip() but I don't know how to call the individual cards. Help?

derkyzer
  • 161
  • 1
  • 1
  • 9

1 Answers1

1

cards[10].flip()

Seeing as you've stored each individual card in a list ([]) and it's simply indexed by an integer, so to call the card number 10, you do cards[9].<function> etc.

Another way would be to flip the card before adding them into the cards list but that would probably ruin your game :)

while 1:
    cardNr = int(raw_input('Flip a card, any card of the total ' + str(len(cards)) + ': '))
    cards[cardNr-1].flip()  # -1 because humans don't count from 0 normally :)

Would flip a card the user chose to flip.

Since you're using a GUI, here's some sample code for ya:

while 1:
  ev = pygame.event.get()
  for event in ev:
    if event.type == pygame.MOUSEBUTTONUP:
      mouse = pygame.mouse.get_pos()
      clicked_cards = [c for c in cards if c.clicked(mouse)]
      for card in clicked_cards:
          if card:
              card.flip()

Now add a function to your card that does:

def clicked(self, mouse):
    if mouse.x >= self.x and mouse.x <= self.x+self.width:
        if mouse.y >= self.y and mouse.y <= self.y+self.height:
            return self
    return False

There's a better way to do it if i'm not mistaken via card.Rect.collidepoint but since i moved over to other GUI libraries quite early on using Pygame, you would need to read up here:

Community
  • 1
  • 1
Torxed
  • 22,866
  • 14
  • 82
  • 131
  • What if it were click based? – derkyzer Feb 03 '14 at 14:36
  • Then it's simple, you store each GUI representation as a number. Normally you call this a Uniqueue identifier or since you're using Pygame there are a `click()` function which you could do `self.flip()` from within. – Torxed Feb 03 '14 at 14:38
  • @Kweb123 Or you would have to do `pygame.mouse.get_pressed()` and then `pygame.mouse.get_pos()` manually, then loop through all your cards via `for card in cards: if mouse.x >= card.x and mouse.x <= card.x+card.width` and then do `.flip()` – Torxed Feb 03 '14 at 14:39
  • That's how it's set up now, but if there's a faster way, I'm all for it. (Or rather I have the positions typed out instead of tied to the object.) – derkyzer Feb 03 '14 at 14:41
  • @Kweb123 You're welcome and good luck with your assignment/game :) – Torxed Feb 03 '14 at 14:46