1
votes

I am making a memory game for an assignment. I can get all the tiles to be covered and make them reveal themselves upon being clicked on but I cannot cover them after they have been clicked on or match them. I dont know how to match the tiles also.

class Tile:
    surface = None
    border_size = 3
    border_color = pygame.Color('black')
    # An object in this class represents a Dot that moves
    @classmethod
    def set_surface(cls,game_surface):
        cls.surface = game_surface
    # instance method

    def __init__(self,x , y, image, cover):
        self.image = image
        self.cover = cover
        self.covered = True
        self.time_cover = None
        # self.timer = pygame.time.get_ticks()
        width = self.image.get_width()
        height = self.image.get_height()

        self.rect = pygame.Rect(x, y, width, height)

    def draw(self):
        pygame.draw.rect(Tile.surface,Tile.border_color,self.rect,Tile.border_size)
        Tile.surface.blit(self.image,self.rect)
        if self.covered:
            Tile.surface.blit(self.cover, self.rect)

    def select(self, position):
        valid_click = False

        if self.rect.collidepoint(position):
            if self.covered:
                valid_click = True
                self.expose_tile()
                self.time_cover = pygame.time.get_ticks() + 2000
                self.update()
            else:
                valid_click = False

        return valid_click

    def update(self):
        if not self.covered and self.time_cover >= 2000:
            self.covered = True
        return self.covered

    def expose_tile(self):
        # if a tile is clicked this method will show the pic ture underneath that tile
        self.covered = False

    def __eq__ (self, other_tile):
        pass
1
Is the issue solved? - Rabbid76

1 Answers

1
votes

When you call update() in the main application loop, then revealed tiles will be covered after 2 seconds.

But you can add a cover_tile method, too:

class Tile:

    # [...]

    def cover_tile(self):
        self.covered = True

If matching tiles share the same image (self.image), then matching tiles can be identified by comparing the images. e.g.: (In the following tileA and tileB are instances of Tile)

if tielA.image != tileB.image:
    tileA.cover_tile()
    tileB.cover_tile()
else
    print("matching")