我正在尝试更改列表中的单个元素。我如何在元素上使用功能?
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))
我认为您想要的就是
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))