Ejemplo n.º 1
0
def edit(size, edge, screen, banned_blocks):
    '''
    Allows the player to edit the boards, marking spaces which can be banned and result in a loss
    if touched by the snake
    '''

    while True:
        screen.fill(colors.get('black'))
        draw_arrow(screen)
        make_grid(size, edge, screen, banned_blocks)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN and pygame.mouse.get_pressed(
                    num_buttons=3)[0]:
                x, y = pygame.mouse.get_pos()
                if x <= 50 and y <= 50:
                    return banned_blocks
                elif 50 <= x <= size[0] - 50 and 50 <= y <= size[1] - 50:
                    cell_x, cell_y = edge * ((x - 50) // edge) + 50, edge * (
                        (y - 50) // edge) + 50
                    if [cell_x, cell_y] == [50, 50]:
                        pass
                    elif [cell_x, cell_y] not in banned_blocks:
                        pygame.draw.line(screen, colors.get('white'),
                                         (cell_x, cell_y),
                                         (cell_x + edge, cell_y + edge))
                        banned_blocks.append([cell_x, cell_y])
                    else:
                        pygame.draw.line(screen, colors.get('black'),
                                         (cell_x, cell_y),
                                         (cell_x + edge, cell_y + edge))
                        banned_blocks.remove([cell_x, cell_y])
        pygame.display.update()
def draw_arrow(screen):
    '''
    draws back arrow
    '''
    pygame.draw.line(screen, colors.get('white'), (10, 25), (45, 25))
    pygame.draw.line(screen, colors.get('white'), (10, 25),
                     (10 + 20 * (3**(1 / 2) / 2), (25 - 20 / 2)))
    pygame.draw.line(screen, colors.get('white'), (10, 25),
                     (10 + 20 * (3**(1 / 2) / 2), (25 + 20 / 2)))
def draw_banned_blocks(banned_blocks, screen, edge):
    '''
    draws lines onto the grid through the banned blocks to mark them
    '''
    for blocks in banned_blocks:
        pygame.draw.rect(screen, colors.get('dark_purple'),
                         (blocks[0], blocks[1], edge, edge))
        pygame.draw.rect(screen, colors.get('purple'),
                         (blocks[0] + 1, blocks[1] + 1, edge - 2, edge - 2))
def make_outline(size, screen):
    '''
    draws an outline of the board onto the screen for playing
    '''
    width, height = size
    pygame.draw.line(screen, colors.get('white'), (50, 50), (50, height - 50))
    pygame.draw.line(screen, colors.get('white'), (width - 50, 50),
                     (width - 50, height - 50))
    pygame.draw.line(screen, colors.get('white'), (50, 50), (width - 50, 50))
    pygame.draw.line(screen, colors.get('white'), (50, height - 50),
                     (width - 50, height - 50))
Ejemplo n.º 5
0
    def show_background(self, width=10, height=20, gap=10):

        self.screen.fill(colors.get('gray'))
        lines = self.screen.get_height() // height
        color = colors.get('white')
        y = gap // 2
        for i in range(lines):
            pygame.draw.rect(
                self.screen, color,
                (self.screen.get_width() // 2 - width, y, width, height))
            y += height + gap
def make_grid(size, edge, screen, banned_blocks):
    '''
    draws the grid of the board onto the screen for precision in editing
    '''
    width, height = size
    draw_banned_blocks(banned_blocks, screen, edge)
    for i in range(0, ((width - 100) // edge) + 1):
        pygame.draw.line(screen, colors.get('white'), ((i * edge) + 50, 50),
                         ((i * edge) + 50, height - 50))
    for i in range(0, ((height - 100) // edge) + 1):
        pygame.draw.line(screen, colors.get('white'), (50, (i * edge) + 50),
                         (width - 50, i * edge + 50))
Ejemplo n.º 7
0
 def __init__(self, WIDTH, HEIGHT, r=10, color='red'):
     self.x = WIDTH // 2  # random.randint(0 + r * 3, WIDTH - r)
     self.y = HEIGHT // 2  # random.randint(0 + r, HEIGHT - r)
     self.vel_x = -2  # random.randint(-1, 1)
     self.vel_y = 2  # random.randint(1)
     self.r = r
     self.color = colors.get(color)
Ejemplo n.º 8
0
    def create_room(self, room):
        # go through the tiles in the rectangle and make them passable
        for x in range(room.x1 + 1, room.x2):
            for y in range(room.y1 + 1, room.y2):
                self.tiles[x][y].blocked = False
                self.tiles[x][y].block_sight = False

                i = randint(0, 3)
                if i == 1:
                    self.items.insert(0,
                                      Entity(x, y, '.', "Stone", libtcod.grey))
                elif i == 2:
                    self.items.insert(
                        0, Entity(x, y, '"', "Grass", colors.get('grass')))

                i = randint(0, 60)
                if i == 1:
                    bot = Entity(x, y, 'W', "WANDERER", libtcod.green,
                                 ai.wanderer_text)
                    self.entities.insert(0, bot)
                elif i == 2:
                    bot = Entity(x, y, 'G', "GATHERER", libtcod.green,
                                 ai.gatherer_text)
                    self.entities.insert(0, bot)
                elif i == 3:
                    bot = Entity(x, y, 'M', "MINER", libtcod.green,
                                 ai.miner_text)
                    self.entities.insert(0, bot)
Ejemplo n.º 9
0
 def __init__(self, WIDTH, HEIGHT, x, color='black'):
     self.width = 20
     self.height = 70
     self.x = x
     self.y = HEIGHT // 2
     self.vel = 3
     self.KEYUP = False
     self.direction = 0
     self.color = colors.get(color)
     self.score = 0
Ejemplo n.º 10
0
    def create_room(self, room):
        # go through the tiles in the rectangle and make them passable
        for x in range(room.x1 + 1, room.x2):
            for y in range(room.y1 + 1, room.y2):
                self.tiles[x][y].blocked = False
                self.tiles[x][y].block_sight = False

                i = randint(0, 50)
                if i == 1:
                    self.items.insert(0, Entity(x, y, '.', "Stone", tcod.grey))
                elif i == 2:
                    self.items.insert(0, Entity(x, y, '"', "Grass", colors.get('grass')))
                elif i == 3:
                    self.entities.insert(0, Entity(x, y, 'b', "Bat", tcod.Color(100, 60, 50)))
Ejemplo n.º 11
0
def loss_screen(length, screen, size, base_font, player, banned_blocks):
    '''
    The display which is shown when a loss occurs. Displays the length of the snake and gives the player the
    option to either play again or edit the board.
    '''
    while True:
        x, y = pygame.mouse.get_pos()
        loss = base_font.render('Game Over', True, colors.get('white'))
        score = base_font.render(f'Final Score: {length} ', True,
                                 colors.get('white'))
        replay = pygame.font.SysFont('arial', 32, False, True if 240 <= x <= 390 and 320 <= y <= 350 else False) \
            .render('Play Again', True, colors.get('white'))
        editor = pygame.font.SysFont('arial', 32, False, True if 490 <= x <= 640 and 320 <= y <= 350 else False) \
            .render('Settings', True, colors.get('white'))
        make_outline(size, screen)
        draw_banned_blocks(banned_blocks, screen, player.edge)
        pygame.draw.rect(screen, colors.get('white'), [
            player.track[0][0] + 1, player.track[0][1] + 1, player.edge - 2,
            player.edge - 2
        ])
        screen.fill(colors.get('white'), (226, 210, 449, 193))
        screen.fill(colors.get('black'), (227, 211, 447, 191))
        screen.blit(loss, (360, 225))
        screen.blit(score, (330, 265))
        screen.blit(replay, (250, 320))
        screen.blit(editor, (500, 320))
        pygame.display.update()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if pygame.mouse.get_pressed(
                        num_buttons=3
                )[0] and 240 <= x <= 390 and 320 <= y <= 350:
                    return True
                elif pygame.mouse.get_pressed(
                        num_buttons=3
                )[0] and 490 <= x <= 640 and 320 <= y <= 350:
                    return False
def hamiltonian_circuit(size, screen, apples, player):
    '''
    A basic AI which creates a path which hits all places on the board and runs the snake game
    until the snake reaches the maximum length.
    '''
    width, height = size
    length = 1
    path = [[50, 50]]
    track = []
    for rows in range(0, ((height - 50) // (2 * player.edge)) - 1):
        path += (build_path(width - 50, height - 50, rows, player))
    final_y = height - 50 - player.edge
    while final_y > 50:
        path.append([50, final_y])
        final_y -= player.edge
    index = 0
    clock = pygame.time.Clock()
    apple = Food(size, apples)
    for apples in range(0, apple.apples):
        apple.apple_pos.append(apple.get_coordinates(player.track, []))
    while True:
        x, y = pygame.mouse.get_pos()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if pygame.mouse.get_pressed(
                        num_buttons=3)[0] and 0 <= x <= 50 and 0 <= y <= 50:
                    return
        pygame.time.delay(25)
        clock.tick(50)
        screen.fill(colors.get('black'))
        track.insert(0,
                     [path[index % len(path)][0], path[index % len(path)][1]])
        if len(track) > length:
            track.pop()
        index += 1
        if [path[index % len(path)][0],
                path[index % len(path)][1]] in apple.apple_pos:
            if length + 4 >= 1600:
                length = 1
                track = []
            else:
                length += 4
            apple.apple_pos.pop(
                apple.apple_pos.index(
                    [path[index % len(path)][0], path[index % len(path)][1]]))
            apple.apple_pos.append(apple.get_coordinates(track, []))

        for apples in apple.apple_pos:
            pygame.draw.rect(screen, colors.get('dark_red'),
                             [apples[0], apples[1], apple.edge, apple.edge])
            pygame.draw.rect(
                screen, colors.get('red'),
                [apples[0] + 1, apples[1] + 1, apple.edge - 2, apple.edge - 2])

        for cubes in track:
            pygame.draw.rect(screen, colors.get('dark_green'),
                             [cubes[0], cubes[1], player.edge, player.edge])
            pygame.draw.rect(
                screen, colors.get('green'),
                [cubes[0] + 1, cubes[1] + 1, player.edge - 2, player.edge - 2])
        make_outline(size, screen)
        draw_arrow(screen)

        pygame.display.update()
Ejemplo n.º 13
0
def maingame(size,
             screen,
             base_font,
             player,
             banned_blocks,
             gamemode,
             length=1,
             direction=None,
             track=[]):
    '''
    The main snake game
    '''
    clock = pygame.time.Clock()
    apple = Food(size, (2 * gamemode[1]) + 1)
    dualist = Dualist(size, 1 if gamemode[2] else 0)
    poison_apple = P_Food(size, gamemode[0])
    for apples in range(0, apple.apples):
        apple.apple_pos.append(
            apple.get_coordinates(player.track, banned_blocks))
    for poison_apples in range(0, poison_apple.apples):
        poison_apple.apple_pos.append(
            poison_apple.get_coordinates(player.track, banned_blocks,
                                         apple.apple_pos))

    while True:
        pygame.time.delay(75)
        clock.tick(15)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        keys = pygame.key.get_pressed()
        direction = player.direction_check(direction, keys)
        player.movement(direction)
        player.tracker(length)
        screen.fill(colors.get('black'))

        if [player.x_position, player.y_position] in apple.apple_pos:
            length += 4
            dualist.length += 4
            if length + dualist.length + len(banned_blocks) >= 1600:
                return length
            apple.apple_pos.pop(
                apple.apple_pos.index([player.x_position, player.y_position]))
            apple.apple_pos.append(
                apple.get_coordinates(player.track, banned_blocks))
            if poison_apple.change_check(gamemode[0]):
                poison_apple.apple_pos.pop(0)
                poison_apple.apple_pos.append(
                    poison_apple.get_coordinates(player.track, banned_blocks,
                                                 apple.apple_pos))

        draw_banned_blocks(banned_blocks, screen, player.edge)

        for cubes in player.track:
            pygame.draw.rect(screen, colors.get('dark_green'),
                             [cubes[0], cubes[1], player.edge, player.edge])
            pygame.draw.rect(
                screen, colors.get('green'),
                [cubes[0] + 1, cubes[1] + 1, player.edge - 2, player.edge - 2])
        if gamemode[2]:
            dualist.movement(direction)
            dualist.tracker(dualist.length)
            for cubes in dualist.track:
                pygame.draw.rect(
                    screen, colors.get('dark_orange'),
                    [cubes[0], cubes[1], player.edge, player.edge])
                pygame.draw.rect(screen, colors.get('orange'), [
                    cubes[0] + 1, cubes[1] + 1, player.edge - 2,
                    player.edge - 2
                ])
        for apples in apple.apple_pos:
            pygame.draw.rect(screen, colors.get('dark_red'),
                             [apples[0], apples[1], apple.edge, apple.edge])
            pygame.draw.rect(
                screen, colors.get('red'),
                [apples[0] + 1, apples[1] + 1, apple.edge - 2, apple.edge - 2])

        for poison_apples in poison_apple.apple_pos:
            pygame.draw.rect(screen, colors.get('dark_blue'), [
                poison_apples[0], poison_apples[1], poison_apple.edge,
                poison_apple.edge
            ])
            pygame.draw.rect(screen, colors.get('blue'), [
                poison_apples[0] + 1, poison_apples[1] + 1,
                poison_apple.edge - 2, poison_apple.edge - 2
            ])
        make_outline(size, screen)

        if player.collision_check(banned_blocks, dualist.track,
                                  poison_apple.apple_pos):
            return length

        score = base_font.render(f'Length: {length}', True,
                                 colors.get('white'))
        screen.blit(score, (50, 10))

        pygame.display.update()
Ejemplo n.º 14
0
def settings(size, screen, base_font, player, gamemode, banned_blocks):
    '''
    The page where the player will be able to customize their game with certain game modes which can be turned or off
    at will in between rounds of play.
    '''
    while True:
        x, y = pygame.mouse.get_pos()
        title = base_font.render("Configure Your Game ", True,
                                 colors.get('white'))
        game_mode_list = base_font.render("Settings:", True,
                                          colors.get('white'))
        start = pygame.font.SysFont('arial', 30, False, True if 580 <= x <= 835 and 470 <= y <= 500 else False) \
            .render("Play Normal Game", True, colors.get('white'))
        hamilton = pygame.font.SysFont('arial', 30, False, True if 475 <= x <= 835 and 520 <= y <= 550 else False) \
            .render("Play Hamiltonian Pathway", True, colors.get('white'))
        edit_marker = pygame.font.SysFont('arial', 30, False, True if 65 <= x <= 135 and 200 <= y <= 230 else False) \
            .render("Edit", True, colors.get('white'))
        poison = pygame.font.SysFont('arial', 30, False, True if 65 <= x <= 255 and 250 <= y <= 280 else False) \
            .render("Poison Apple", True, colors.get('white'))
        multi = pygame.font.SysFont('arial', 30, False, True if 65 <= x <= 210 and 300 <= y <= 330 else False) \
            .render("Multi Ball", True, colors.get('white'))
        dualist = pygame.font.SysFont('arial', 30, False, True if 65 <= x <= 175 and 350 <= y <= 380 else False) \
            .render("Dualist", True, colors.get('white'))

        screen.fill(colors.get('black'))
        make_outline(size, screen)
        screen.blit(title, (250, 70))
        screen.blit(game_mode_list, (75, 150))
        screen.blit(edit_marker, (75, 200))
        screen.blit(poison, (75, 250))
        screen.blit(multi, (75, 300))
        screen.blit(dualist, (75, 350))
        screen.blit(hamilton, (480, 520))
        screen.blit(start, (580, 470))

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if pygame.mouse.get_pressed(
                        num_buttons=3
                )[0] and 65 <= x <= 135 and 200 <= y <= 230:
                    banned_blocks = edit(size, player.edge, screen,
                                         banned_blocks)
                elif pygame.mouse.get_pressed(
                        num_buttons=3
                )[0] and 65 <= x <= 255 and 250 <= y <= 280:
                    gamemode[0] = (gamemode[0] + 1) % 6
                elif pygame.mouse.get_pressed(
                        num_buttons=3
                )[0] and 65 <= x <= 210 and 300 <= y <= 330:
                    gamemode[1] = (gamemode[1] + 1) % 8
                elif pygame.mouse.get_pressed(
                        num_buttons=3
                )[0] and 65 <= x <= 175 and 350 <= y <= 380:
                    gamemode[2] = 1 if gamemode[2] == 0 else 0
                elif pygame.mouse.get_pressed(
                        num_buttons=3
                )[0] and 580 <= x <= 835 and 470 <= y <= 500:
                    return banned_blocks, gamemode, True
                elif pygame.mouse.get_pressed(
                        num_buttons=3
                )[0] and 475 <= x <= 835 and 520 <= y <= 550:
                    return banned_blocks, gamemode, False

        x_poison = 265
        for poison_apples in range(0, gamemode[0]):
            pygame.draw.rect(screen, colors.get('dark_blue'),
                             [x_poison, 260, 20, 20])
            pygame.draw.rect(screen, colors.get('blue'),
                             [x_poison + 1, 261, 18, 18])
            x_poison += 25

        x_apple = 220
        for apples in range(0, (2 * gamemode[1]) + 1):
            pygame.draw.rect(screen, colors.get('dark_red'),
                             [x_apple, 310, 20, 20])
            pygame.draw.rect(screen, colors.get('red'),
                             [x_apple + 1, 311, 18, 18])
            x_apple += 25

        if gamemode[2]:
            x_cube = 185
            for cubes in range(0, 5):
                pygame.draw.rect(screen, colors.get('dark_orange'),
                                 [x_cube, 360, 20, 20])
                pygame.draw.rect(screen, colors.get('orange'),
                                 [x_cube + 1, 361, 18, 18])
                x_cube += 20

        pygame.display.update()