1

我想用随机生成的地形制作简单的游戏,但是当我移动地形而不是随机生成地形时,它会生成最大高度为 8 的地形。我是 pygame 的初学者,这个错误让我很恼火。

代码:

    import pygame
from random import randint
pygame.init()

# Set up the drawing window
screen = pygame.display.set_mode([1000, 500])

kolory = ["deepskyblue2","chocolate4"]

w = 6

def wysokosc_spr():
    global w
    if w == 8:
        w -= 1
        return w
    elif w <= 3:
        w += 1
        return w
    else:
        w = randint(w-1,w+1)
        return w

swiat = [[kolory[1] for j in range(wysokosc_spr())] for i in range(20)]

def wyswietl_swiat(tablica_2D,x,y = 500):
    for i in tablica_2D:
        for j in i:
            pygame.draw.rect(screen,kolory[1],pygame.Rect(x,y,50,50))
            y -= 50
        y = 500
        x += 50

# Run until the user asks to quit
running = True
while running:

    screen.fill((255, 255, 255))
    wyswietl_swiat(swiat,0,500)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_d:
                swiat += [kolory[1] for i in range(wysokosc_spr())]
                swiat.pop(0)

    pygame.display.flip()


# Done! Time to quit.
pygame.quit()

谢谢!

4

1 回答 1

2

实际上,您添加w了列。您必须附加一个带有w项目的新列:

swiat += [kolory[1] for i in range(wysokosc_spr())]

swiat.append([kolory[1] for i in range(wysokosc_spr())])
于 2021-12-20T16:10:43.403 回答