pygame 中的随机平台生成器

Random platform generator in pygame

我正在 pygame 中创建一个随机平台,其中平台的位置和大小由 python 随机函数确定。我想弄清楚如何创建一个 returns 列表的算法(在本例中称为级别) 它有像这样的元素列表

    level = [
    #[  x, y, width, height, color ]
    [48, 43, 281, 29, BLACK ]
    [357, 104, 234, 33, BLACK ] ,
    [334, 341, 328, 22, BLACK ] ,
    [481, 159, 13, 117, BLACK ] ] 

这是我到目前为止创建的代码。

#!\user\bn\env python
import random,time
#Colors
BLACK = (  0,   0,   0)
WHITE =  (255, 255, 255)
BLUE =    (  0,   0, 255)
GREEN = (  0, 255,   0)
RED =      (255,   0,   0)
#default starting platform

#[  x, y, width, height, color ]
#[126, 404, 536, 33, BLACK ] ,
xco_ord = 126
yco_ord = 404
width = 536
height = 33

a = []
holder = []
level = []
while True:
    #random numbers
    gaps = random.randrange(300, 480)
    width = random.randrange(100, 700)
    height = random.randrange(28, 140)

    Uyco_ord = yco_ord  + 113
    up_height = random.randrange(yco_ord, Uyco_ord)

    Dyco_ord = yco_ord - 300
    down_height = random.randrange(Dyco_ord, yco_ord)

    up_down_choice = random.choice('DU')

    count = 0
    other_counter = 7
    while count <= other_counter:
        if up_down_choice == 'U':
            up_down_height = down_height
        if up_down_choice == 'D':
            up_down_height = up_height

        #  [30, 355, 503, 28, BLACK ]
        #===================================================================
        a.append(gaps)
        a.append(up_height)
        a.append(width)
        a.append(height)
        a.append(BLACK)
        holder.append(a)
        count+=1
        other_counter +=1
        #===================================================================
        rgaps = gaps + 180
        gaps =random.randrange(gaps,rgaps )

        width = random.randrange(100, 700)
        height = random.randrange(28, 140)

        yco_ord = up_height
        Uyco_ord = yco_ord  + 113
        up_height = random.randrange(yco_ord, Uyco_ord)
        yco_ord = down_height
        Dyco_ord = yco_ord - 300 
        down_height = random.randrange(Dyco_ord, yco_ord)
        up_down_choice = random.choice('DU')
        #===================================================================
        time.sleep(2)
        if len(level)> 25:
            time.sleep(2)
            del(block[0])
            time.sleep(2)
            del(block[1])
            time.sleep(2)
            del(block[2])
            time.sleep(2)
            del(block[3])
            time.sleep(2)
            del(block[4])

        for blocks in holder:
            level.append(blocks)
            del(holder[0])
            print up_down_choice
            print level

但每次我 运行 它都会将一些列表连接在一起。有人可以帮助我或为我指出一个好的方向吗?

您没有清除 'a' 列表。之后:

holder.append(a)

地点:

a = []

这应该可以解决问题。

像这样:

#!\user\bn\env python
import random,time
#Colors
BLACK = (  0,   0,   0)
WHITE =  (255, 255, 255)
BLUE =    (  0,   0, 255)
GREEN = (  0, 255,   0)
RED =      (255,   0,   0)
#default starting platform

#[  x, y, width, height, color ]
#[126, 404, 536, 33, BLACK ] ,
xco_ord = 126
yco_ord = 404
width = 536
height = 33

a = []
holder = []
level = []

while True:
    #random numbers
    gaps = random.randrange(300, 480)
    width = random.randrange(100, 700)
    height = random.randrange(28, 140)

    Uyco_ord = yco_ord  + 113
    up_height = random.randrange(yco_ord, Uyco_ord)

    Dyco_ord = yco_ord - 300
    down_height = random.randrange(Dyco_ord, yco_ord)

    up_down_choice = random.choice('DU')

    count = 0
    other_counter = 7
    while count <= other_counter:
        if up_down_choice == 'U':
            up_down_height = down_height
        if up_down_choice == 'D':
            up_down_height = up_height

        #  [30, 355, 503, 28, BLACK ]
        #===================================================================
        a.append(gaps)
        a.append(up_height)
        a.append(width)
        a.append(height)
        a.append(BLACK)
        holder.append(a)
        a = []
        count+=1
        other_counter +=1
        #===================================================================
        rgaps = gaps + 180
        gaps =random.randrange(gaps,rgaps )

        width = random.randrange(100, 700)
        height = random.randrange(28, 140)

        yco_ord = up_height
        Uyco_ord = yco_ord  + 113
        up_height = random.randrange(yco_ord, Uyco_ord)
        yco_ord = down_height
        Dyco_ord = yco_ord - 300 
        down_height = random.randrange(Dyco_ord, yco_ord)
        up_down_choice = random.choice('DU')
        #===================================================================
        time.sleep(2)
        if len(level)> 25:
            time.sleep(2)
            del(block[0])
            time.sleep(2)
            del(block[1])
            time.sleep(2)
            del(block[2])
            time.sleep(2)
            del(block[3])
            time.sleep(2)
            del(block[4])

        for blocks in holder:
            level.append(blocks)
            del(holder[0])
            print '\n'
            print '\nup_down_choice: %s' % up_down_choice
            print '\n'
            print '\nlevel:'
            print level