[Python] Terrain generation returning nonsensical array

Discussão em 'Python' iniciado por Stack, Março 21, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on a game for my Computer Science coursework, and I'm experiencing a weird issue

    my terrain generation should create terrain objects in set positions or if a random chance is met and it wouldn't fall within the player start area

    when actually running the code, and looking at the generated level, this appears to be working

    however, when comparing against the array (and proven by printing the array and checking) I noticed that every single space on y=42 was being treated as empty, and every other space was being treated as terrain, even if what the game was displaying directly contradicted that

    btw, I'm aware my code is very amateur, but my teacher would recognise if it suddenly changed up, so ideally I just want solutions to this problem, not general code improvement suggestions

    #tarray is a 50x50 array of 52x52 tiles, all starting as "Empty", and appears to not be working correctly
    #dgen is a procedure that places the level's exit door randomly at the bottom of the level, and appears to be working correctly
    #olist is a list of all objects, and appears to be working correctly
    #tlist is a list of all terrain objects, and appears to be working correctly
    #trange stores the values for x and y at which terrain should be able to spawn, 0 to 2548 going up in multiples of 52, and appears to be working correctly
    #boundaries stores the values for x and y at which terrain should always spawn, 0 to 312 and 2236 to 2548 going up in multiples of 52, and appears to be working correctly
    #xstart stores the values for x at which terrain should never spawn, 364 to 520 going up in multiples of 52, and appears to be working correctly
    #ystart stores the values of y for which terrain should never spawn, 364 and 416, and appears to be working correctly
    #terrain is a class that creates terrain objects of a set width, height and colour by taking in x and y values, and appears to be working correctly
    #the random chance is currently set to guaranteed for testing, normally it would be ~1/20

    def tgen():
    global olist
    global tarray
    global tlist
    for y in trange:
    for x in trange:
    ax = int(x / 52)
    ay = int(y / 52)
    if (1 == random.randint(0,2) or x in boundaries or y in boundaries) and not (x in xstart and y in ystart):
    tarray[ax][ay] = terrain(x,y)
    olist.append(tarray[ax][ay])
    tlist.append(tarray[ax][ay])
    else:
    tarray[ax][ay] = "Empty"
    dgen()

    Continue reading...

Compartilhe esta Página