Python 的列表对象不允许索引值更改



我正在使用Python和Pygame创建我的第一个游戏。它由掉落在地上的物体和由玩家控制的物体组成。但是,当坠落的物体落地时,它给了我一个例外:

Traceback (most recent call last):
  File "/home/me/Chest/game.py", line 79, in <module>
    resetShirtPosition()
  File "/home/me/Chest/game.py", line 35, in resetShirtPosition
    shirtPosition[0] = random.randint(0, DISPLAY_WIDTH - shirt_size[0])
TypeError: 'tuple' object does not support item assignment

似乎它不允许向量按索引更改。我觉得这很奇怪,因为我不是用元组而是用list shirtPosition分配.除此之外,我正在以相同的方式更改chestPosition,并且它会移动而不会崩溃。

这是我的代码:

import pygame, random, os
pygame.init()
DISPLAY_WIDTH, DISPLAY_HEIGHT = 1600, 900
clock   = pygame.time.Clock()
gamedisplay = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
chest_imag  = pygame.image.load(os.path.join("catcher.png"))
shirt_red   = pygame.image.load(os.path.join("shirt_red.png"))
background  = pygame.image.load(os.path.join("background.png"))
shirt_blue  = pygame.image.load(os.path.join("shirt_blue.png"))
shirt_green = pygame.image.load(os.path.join("shirt_green.png"))
shirt = shirt_blue
chest_size = chest_imag.get_size()
chestPosition = [DISPLAY_WIDTH / 2, DISPLAY_HEIGHT - chest_size[1]]
shirt_blue_size = shirt_blue.get_size()
shirt_size      = shirt_blue_size
shirtPosition = [random.randint(0, DISPLAY_WIDTH - shirt_size[0]), 0]
dX, dY = 0, 20
score = 0
fallWidth = fallHeight = 100
COLLISION_LEVEL_LINE = DISPLAY_HEIGHT - chest_size[1]
onGame = True
collide = False
# **************** Exception happens here *******************
def resetShirtPosition():
    shirtPosition[0] = random.randint(0, DISPLAY_WIDTH - shirt_size[0])
    shirtPosition[1] = 0
def hitCollisionLevelLine():
    return (shirtPosition[1] + shirt_size[1]) >= COLLISION_LEVEL_LINE
def hitGround():
    return (shirtPosition[1] + shirt_size[1]) >= DISPLAY_HEIGHT
def hitHorizontalEdge():
    willTouchLeftEdge = (chest_size[0] + dX) < 0
    willTouchRightEdge = (chest_size[0] + dX) > (DISPLAY_WIDTH  - chest_size[0])
    return willTouchLeftEdge or  willTouchRightEdge
def collides():
    touchFromLeft = shirtPosition[0] > (chestPosition[0] - shirt_size[0])
    touchFromRight = shirtPosition[0] < (chestPosition[0] + chest_size[0])
    return touchFromLeft and touchFromRight
while onGame:
    clock.tick(60)
    # action
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                dX = -20
            elif event.key == pygame.K_d:
                dX = 20
        elif event.type == pygame.KEYUP:
            dX = 0
        elif event.type == pygame.QUIT:
            onGame = false
    # logic
    if hitCollisionLevelLine():
        hasCollided = collides()
        if hasCollided:
            collide = False
            score = score + 1
            print score
            resetShirtPosition()
    if hitGround():
        resetShirtPosition()
    if hitHorizontalEdge():
        dX = 0
    # update positions and draw
    chestPosition = (chestPosition[0] + dX, chestPosition[1])
    shirtPosition = (shirtPosition[0], shirtPosition[1] + dY)
    gamedisplay.blit(background , (0, 0))
    gamedisplay.blit(shirt_blue, shirtPosition)
    gamedisplay.blit(chest_imag, chestPosition)
    pygame.display.flip()

我已经发布了我的所有代码,因为我也希望你能给我一些改进它的技巧,从代码编写到提高性能的技巧。例如,我正在考虑将我的图像放入字典中,并通过其键(名称(获取它们。谢谢。

在下一行:

shirtPosition = (shirtPosition[0], shirtPosition[1] + dY)

您已将衬衫位置重新分配为元组,这导致了您的问题。请将其转换为列表对象,您的问题将得到解决。

最新更新