示例#1
0
class Puzzle(QuitScene):
    def __init__(self):
        QuitScene.__init__(self)
        grid = Grid(self._rect, (16, 15), (3, 3))
        Text(self,
             'Puzzle',
             grid.position(8, 1),
             self.font.large,
             'mediumorchid1',
             anchor=Anchor.CENTER)

        # image artist
        Text(self,
             'artwork by Kenny',
             grid.position(4, 14),
             self.font.basic,
             'mediumorchid1',
             anchor=Anchor.CENTER)
        Text(self,
             'www.kenney.nl',
             grid.position(12, 14),
             self.font.basic,
             'mediumorchid1',
             anchor=Anchor.CENTER)

        box = Grid(self._rect.inflate(-10, -10), (19, 17), (2, 2))
        Button(self, 'Back', box.align(0, 0, 3, 1), 'mediumorchid1',
               self.push_back)
        Button(self, 'New Game', box.align(0, 1, 3, 1), 'mediumorchid1',
               self.push_newgame)

        self.grab_images()
        width = 288
        height = 288
        gx = (self._rect.w - width) / 2
        gy = (self._rect.h - height) / 2
        self.board_grid = Grid((gx, gy, width, height), (4, 4), (1, 1))
        self.board = []
        self.board_rect = []
        for j in range(4):
            for i in range(4):
                rect = self.board_grid.rect(i, j)
                clip_rect = rect.copy()
                clip_rect.x -= i * 2 + self.board_grid._rect.x + 1
                clip_rect.y -= j * 2 + self.board_grid._rect.y + 1
                self.board.append(clip_rect)
                self.board_rect.append(rect)

        self.board[-1] = None
        self.push_newgame(None, None)

    def grab_images(self):
        filepath = os.path.join('images', 'PNG')
        self.images_file = []
        items = os.listdir(filepath)
        for item in items:
            if item.endswith('.png'):
                filename = os.path.join(filepath, item)
                self.images_file.append(filename)

    def load_image(self):
        filename = choice(self.images_file)
        self.image = pygame.image.load(filename)
        self.image = self.image.convert_alpha()
        self.shadow_image = pygame.transform.scale(self.image, (60, 60))
        self.image = pygame.transform.scale(self.image,
                                            self.board_grid.get_size())

    def push_newgame(self, button, pydata):
        self.load_image()
        shuffle(self.board)

    def push_back(self, button, pydata):
        self.set_scene('SimpleGames')

    def blit(self, surface):
        surface.fill((40, 0, 40))
        surface.blit(self.shadow_image, self.board_grid.rect(5, 1))
        for clip_rect, rect in zip(self.board, self.board_rect):
            if clip_rect:
                surface.blit(self.image.subsurface(clip_rect), rect)

    def event(self, event):
        QuitScene.event(self, event)

        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                x, y = self.board_grid.location(*event.pos)
                if x is None or y is None:
                    return

                if 0 <= x < 4 and 0 <= y < 4:
                    pos = y * 4 + x
                    if self.board[pos] is None:
                        return

                    def setboard(npos):
                        if self.board[npos] is None:
                            self.board[npos] = self.board[pos]
                            self.board[pos] = None

                    if pos + 4 < 16:
                        setboard(pos + 4)
                    if pos - 4 >= 0:
                        setboard(pos - 4)
                    if pos % 4 + 1 < 4:
                        setboard(pos + 1)
                    if pos % 4 - 1 >= 0:
                        setboard(pos - 1)
示例#2
0
class Memory(QuitScene):
    def __init__(self):
        Card.load_images()
        QuitScene.__init__(self)
        grid = Grid(self._rect, (16, 15), (3, 3))
        Text(self,
             'Memory',
             grid.position(8, 1),
             self.font.large,
             'orange',
             anchor=Anchor.CENTER)

        self.move_text = Text(self,
                              'Moves: 0',
                              grid.position(8, 3),
                              self.font.basic,
                              'orange',
                              anchor=Anchor.CENTER)

        # image artist
        Text(self,
             'artwork by Kenny',
             grid.position(4, 14),
             self.font.basic,
             'orange',
             anchor=Anchor.CENTER)
        Text(self,
             'www.kenney.nl',
             grid.position(12, 14),
             self.font.basic,
             'orange',
             anchor=Anchor.CENTER)

        box = Grid(self._rect.inflate(-10, -10), (19, 17), (2, 2))
        Button(self,
               'Back',
               box.align(0, 0, 3, 1),
               'orange',
               callback=self.push_back)
        Button(self,
               'New Game',
               box.align(0, 1, 3, 1),
               'orange',
               callback=self.push_newgame)

        gx = (self._rect.w - 700) / 2
        gy = (self._rect.h - 360) / 2 + 30
        self.grid = Grid((gx, gy, 700, 360), (10, 6), (5, 5))
        self.push_newgame(None, None)

    def push_newgame(self, button, data):
        self.cards = []
        self.move_text.set_text('Moves: 0')
        objects = list(range(30)) + list(range(30))
        for across in range(10):
            self.cards.append([])
            for down in range(6):
                pick = choice(objects)
                objects.remove(pick)
                self.cards[across].append(
                    Card(self.grid.rect(across, down), pick))

        self.last_pick = []
        self.move_count = 0

    def push_back(self, button, data):
        self.set_scene('SimpleGames')

    def blit(self, surface):
        surface.fill((30, 30, 30))
        for item in self.cards:
            for card in item:
                card.blit(surface)

    def event(self, event):
        QuitScene.event(self, event)

        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                x, y = self.grid.location(*event.pos)
                if x == None or y == None:
                    return

                if 0 <= x < 10 and 0 <= y < 6:
                    if self.cards[x][y].show == True:
                        return

                    self.cards[x][y].show = True
                    if len(self.last_pick) == 0:
                        self.last_pick = [(x, y)]
                    elif len(self.last_pick) == 1:
                        cx, cy = self.last_pick[0]
                        if self.cards[x][y].item == self.cards[cx][cy].item:
                            self.last_pick = []
                        else:
                            self.last_pick.append((x, y))
                            self.move_count += 1
                            self.move_text.set_text('Moves: {0}'.format(
                                self.move_count))
                    else:
                        for cx, cy in self.last_pick:
                            self.cards[cx][cy].show = False
                        self.last_pick = [(x, y)]
示例#3
0
class FloodIt(QuitScene):
    def __init__(self):
        QuitScene.__init__(self)
        grid = Grid(self._rect, (16, 15), (3, 3))
        Text(self,
             'FloodIt',
             grid.position(8, 1),
             self.font.large,
             'royalblue',
             anchor=Anchor.CENTER)

        self.turn_text = Text(self,
                              'Turn: 0',
                              grid.position(8, 14),
                              self.font.basic,
                              'wheat4',
                              anchor=Anchor.CENTER)

        box = Grid(self._rect.inflate(-10, -10), (19, 17), (2, 2))
        Button(self, 'Back', box.align(0, 0, 3, 1), 'royalblue',
               self.push_back)
        Button(self, 'New Game', box.align(0, 1, 3, 1), 'royalblue',
               self.push_newgame)

        self.colors = tuple(
            map(pygame.Color, [
                'dodgerblue', 'gold', 'firebrick1', 'darkslateblue',
                'forestgreen', 'darkorange', 'mediumorchid'
            ]))

        self.board_grid = Grid((190, 100, 420, 420), (14, 14), (1, 1))
        self.push_newgame(None, None)

    def push_newgame(self, button, data):
        self.board = []
        for j in range(14):
            self.board.append([])
            for i in range(14):
                self.board[j].append(
                    Dot(self.colors, self.board_grid.rect(i, j), (j, i)))

        self.board[0][0].choosen = True
        self.board[0][0].disturbance(self.board, self.board[0][0].color)
        for item in self.board:
            for dot in item:
                dot.ignore_me = False

        self.count = 0
        self.turn_text.set_text("Turn: 0")

    def push_back(self, button, data):
        self.set_scene('SimpleGames')

    def blit(self, surface):
        surface.fill((10, 10, 20))

        for row in self.board:
            for dot in row:
                surface.fill(dot.color, dot.rect)

    def event(self, event):
        QuitScene.event(self, event)

        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                x, y = self.board_grid.location(*event.pos)
                if x is None or y is None:
                    return

                if 0 <= x < 14 and 0 <= y < 14:
                    color = self.board[y][x].color
                    match = self.board[0][0].disturbance(self.board, color)
                    if match > 0:
                        self.count += 1
                        self.turn_text.set_text('Turn: ' + str(self.count))
                        for item in self.board:
                            for dot in item:
                                dot.ignore_me = False
                                if dot.choosen:
                                    dot.color = color
                    else:
                        for item in self.board:
                            for dot in item:
                                dot.ignore_me = False