Pygame:添加到列表的顺序混乱了

时间:2019-03-27 00:04:58

标签: python-3.7

我已经制作了一个循环,以该特定顺序将图片,x坐标和y坐标添加到列表中。但是,我在程序中遇到了进一步的错误,并且发现问题是这些元素添加到列表的顺序。谁能告诉我为什么会这样吗?


代码:

zero = pygame.image.load("0.png").convert_alpha()

one = pygame.image.load("1.png").convert_alpha()

two = pygame.image.load("2.png").convert_alpha()

three = pygame.image.load("3.png").convert_alpha()

four = pygame.image.load("4.png").convert_alpha()

five = pygame.image.load("5.png").convert_alpha()

six = pygame.image.load("6.png").convert_alpha()

seven = pygame.image.load("7.png").convert_alpha()

eight = pygame.image.load("8.png").convert_alpha()

nine = pygame.image.load("9.png").convert_alpha()

numList = [zero,one,two,three,four,five,six,seven,eight,nine]

blitList = []

i=0

while i<10:

    i=i+1

    numX = random.randint(20,1320)

    numY = random.randint(20,700)

    number = random.choice(numList)

    blitList.append(number)

    blitList.append(numX)

    blitList.append(numY)

打印列表时:

[<Surface(70x69x32 SW)>, 506, 110, <Surface(69x69x32 SW)>, 1177, 559, <Surface(69x69x32 SW)>, 524, 467, <Surface(69x69x32 SW)>, 263, 232, 630, <Surface(69x69x32 SW)>, 551, <Surface(69x69x32 SW)>, 58, 123, <Surface(69x69x32 SW)>, 1157, 520, <Surface(69x69x32 SW)>, 772, 640, <Surface(69x69x32 SW)>, 178, 522]

顺序应该是“ [表面],x值,​​y值...”,依此类推,但是当我打印时,每次都弄乱了顺序。 (在本例中为第五)

1 个答案:

答案 0 :(得分:0)

我对此很陌生,但也许您在循环中或在某事中两次选择了同一张图片,那又是什么搞砸了。如果您在循环外随机播放numlist并从被跳过的numlist追加将有所帮助。距离拾取所有对象还有1个短的循环。

-Terje-

代码:

zero = pygame.image.load("0.png").convert_alpha()

one = pygame.image.load("1.png").convert_alpha()

two = pygame.image.load("2.png").convert_alpha()

three = pygame.image.load("3.png").convert_alpha()

four = pygame.image.load("4.png").convert_alpha()

five = pygame.image.load("5.png").convert_alpha()

six = pygame.image.load("6.png").convert_alpha()

seven = pygame.image.load("7.png").convert_alpha()

eight = pygame.image.load("8.png").convert_alpha()

nine = pygame.image.load("9.png").convert_alpha()

numList = [zero,one,two,three,four,five,six,seven,eight,nine]

Random.shuffle(numList)

blitList = []

i=0

while i<10:

    numX = random.randint(20,1320)

    numY = random.randint(20,700)

    number = numList[i]

    blitList.append(number)

    blitList.append(numX)

    blitList.append(numY)

    i=i+1