Pygame:附加到列表的顺序搞乱了

问题描述 投票:-2回答:1

我做了一个循环,以特定的顺序将图片,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]

顺序应该是'[surface],x-value,y-value ...'等等,但是当我打印它时,每次都有一个混乱的订单改变了。 (在这种情况下,它是第五个)

python-3.7
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.