#๐Ÿ”’ Confused on why adding variable messes up my code

34 messages ยท Page 1 of 1 (latest)

winged pumice
#

Original code (redundancies replaced with ...):

def generate_map():
    base = seed_biome_spread()

    num_rows = len(base)
    num_cols = len(base[0])

    hex_offset = 0
    tile_num_x = 0

    tile_num_y = 0
    y_offset = 0
    x_offset = 0

    temp_surface = pygame.Surface((num_cols * 24 + 12, num_rows * 18 + 8))
    temp_surface.fill((0, 0, 0))

    tile_clr = (0, 0, 0)

    for r in range(num_rows):
        for c in range(num_cols):
            tile_type = base[r][c]

            if tile_type == "W":
            ...
            elif tile_type == "HD":
                tile_clr = (203, 214, 186)

            tile = [(0 + x_offset, 6 + y_offset),
                    (0 + x_offset, 18 + y_offset),
                    (12 + x_offset, 24 + y_offset),
                    (24 + x_offset, 18 + y_offset),
                    (24 + x_offset, 6 + y_offset),
                    (12 + x_offset, 0 + y_offset)]

            pygame.draw.polygon(temp_surface, tile_clr, tile, 0)
            pygame.draw.polygon(temp_surface, (0, 0, 0), tile, 1)

            x_offset = x_offset + 24
            tile_num_x = tile_num_x + 1

        y_offset = y_offset + 18
        tile_num_x = 0
        tile_num_y = tile_num_y + 1

        x_offset = 0
        if hex_offset == 0:
            hex_offset = 1
            x_offset = x_offset + 12
        elif hex_offset == 1:
            hex_offset = 0

    temp_surface.set_colorkey((0, 0, 0))
    return temp_surface, base

Adding mountains = mountain_seed() after base = seed_biome_spread() messes up the generation even though the mountains variable is not being used and they share no variables.

muted sparrowBOT
#

@winged pumice

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

winged pumice
#
def mountain_seeds():
    random.seed(seed_value)

    m_base = base_array
    num_rows = len(m_base)
    num_cols = len(m_base[0])

    num_seeds_mount = round(((num_rows * num_cols) / 10) + 1)

    for _ in range(num_seeds_mount):
        row = random.randint(0, num_rows - 1)
        col = random.randint(0, num_cols - 1)
        m_base[row][col] = 'M'

    def has_adjacent_m(rows, cols, array):
        rows = len(array)
        cols = len(array[0])

        for dr in [-1, 0, 1]:
            for dc in [-1, 0, 1]:
                if dr == 0 and dc == 0:  # Skip the current tile
                    continue
                new_r = r + dr
                new_c = c + dc
                if 0 <= new_r < rows and 0 <= new_c < cols and array[new_r][new_c] == "M":
                    return True
        return False

    for r in range(num_rows):
        for c in range(num_cols):
            h_chance = random.uniform(0.01, 0.99)
            if h_chance <= .10:
                m_base[r][c] = "H"

    for _ in range(random.randint(3, 5)):
        for r in range(num_rows):
            for c in range(num_cols):
                m_chance = random.uniform(0.01, 0.99)
                if has_adjacent_m(r, c, m_base):
                    if m_chance >= .4:
                        m_base[r][c] = "M"
                    else:
                        m_base[r][c] = "H"
    for r in range(num_rows):
        for c in range(num_cols):
            h_chance = random.uniform(0.01, 0.99)
            if has_adjacent_m(r, c, m_base):
                if m_base[r][c] == "M":
                    continue
                elif h_chance >= .9:
                    m_base[r][c] = "H"

    return m_base```
#

The mountain_seeds() function in question

#

Code when bugged:

#

Seems to generate cold and hot areas normally but messes up normal tiles

balmy adder
#

did you mean to say

#

mountains = mountain_seed()

instead of

mountains = mountain_seed

#

?

winged pumice
#

Yeah

#

Sorry

#

Edited for clarity

#

Example of normal generation:

#

My assumption is something in the mountain seed function messes up the array for the tiles

#

So when I call the function, it messes stuff up

#

Whole code without bug ^^^

#

brb, need to take care of something

balmy adder
#

hey sorry i forgot to check in

#

were you able to figure it out?

winged pumice
#

Not yet, no- been a little busy with unexpected business

balmy adder
#

no worries let me try this locally

#

my guess is mountain isnt doing what you think its doing

#

what do i pip for this?

#

oh i needed pygame lol

#

dont have your images >.>

winged pumice
#

pip install pygame-ce

#

And these are the images, just put them in a folder called images in the project folder

winged pumice
muted sparrowBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.