随机修改列表中的多个元素

时间:2014-01-26 12:05:33

标签: python list random pygame element

我正在尝试更改列表中的单个元素,在这种情况下是pygame.rect但我不明白 我如何在元素上使用函数?

leftOrRightOrUpOrDown = random.randint(0,1,2,3)
    if leftOrRightOrUpOrDown == 0:
        rabbit in rabbits[] move(x-1, y)
    elif leftOrRightOrUpOrDown == 1:
        rabbit in rabbits[] move(x+1, y)
    elif leftOrRightOrUpOrDown == 2:
        rabbit in rabbits[] move(x, y-1)
    elif leftOrRightOrUpOrDown == 3:
        rabbit in rabbits[] move(x, y+1)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 游戏的其余部分循环:

startGame = True
while startGame == True:

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        if event.type == KEYDOWN:
            if event.key == K_LEFT:
                moveRight = False
                moveLeft = True
            if event.key == K_RIGHT:
                moveRight = True
                moveLeft = False
            if event.key == K_UP:
                moveDown = False
                moveUp = True
            if event.key == K_DOWN:
                moveDown = True
                moveUp = False
        if event.type == KEYUP:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
            if event.key == K_LEFT:
                moveLeft = False
            if event.key == K_RIGHT:
                moveRight = False
            if event.key == K_UP:
                moveUp = False
            if event.key == K_DOWN:
                moveDown = False

    rabbitCounter += 1
    if rabbitCounter >= NEW_RABBIT:
        rabbitCounter = 0
        rabbits.append(pygame.Rect(random.randint(0, WINDOW_WIDTH
    - RABBIT_SIZE), random.randint (0, WINDOW_HEIGHT - RABBIT_SIZE),
    RABBIT_SIZE, RABBIT_SIZE))



    #windowSurface.fill(BLACK)
    windowSurface.blit(background_image,[0,0])


    if moveDown and player.bottom < WINDOW_HEIGHT:
        player.top += MOVE_SPEED
    if moveUp and player.top > 0:
        player.top -= MOVE_SPEED
    if moveLeft and player.left > 0:
        player.left -= MOVE_SPEED
    if moveRight and player.right < WINDOW_WIDTH:
        player.right += MOVE_SPEED

    windowSurface.blit(playerImage, player)
    for rabbit in rabbits:
        windowSurface.blit(rabbitImage, rabbit)

    rabbitMovement = random.choice([(-1,0), (1, 0), (0, -1), (0, 1)])
    for rabbit in rabbits:
        rabbit.move(*rabbitMovement)


    for rabbit in rabbits[:]:

        if player.colliderect(rabbit):
            windowSurface.blit(rabbitImageTwo, rabbit)
            windowSurface.blit(playerImageTwo, player)

        def explosion():
            for rabbit in rabbits:
                if player.colliderect(rabbit) and (moveLeft == False and
        moveRight == False and moveUp == False and
        moveDown == False):
                     rabbits.remove(rabbit)

        if player.colliderect(rabbit) and (moveLeft == False and
    moveRight == False and moveUp == False and moveDown == False):
            t = Timer(3, explosion)
            t.start()

    if len(rabbits) == 0:
        rabbitCounter = 0
        windowSurface.blit (text, (210, 104))
        startGame = False


    pygame.display.update()
    mainClock.tick(40)



while startGame == False:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

stepmovement

    stepMovementNegative = random.randrange(0, -6, 2)

    stepMovementPositive = random.randrange(0, 6, 2)

    rabbitMovement = [((stepMovementNegative),0), ((stepMovementPositive), 0)
                      , (0, (stepMovementNegative)), (0, (stepMovementPositive))]
    for rabbit in rabbits:
        rabbit.move_ip(*random.choice(rabbitMovement))

1 个答案:

答案 0 :(得分:2)

我认为你想要的是

movement = random.choice([(-1, 0), (1, 0), (0, -1), (0, 1)]) # choose tuple (x, y)
for rabbit in rabbits: # work through all rabbits
    rabbit.move(*movement) # apply movement tuple to rabbit

这可以避免不必要的if:检查和移动方向的“幻数”。请注意,这为每个rabbit提供了相同的移动;要使它们全部不同,请移动choice for循环:

movements = [(-1, 0), (1, 0), (0, -1), (0, 1)]
for rabbit in rabbits:
    rabbit.move(*random.choice(movements))