为什么不会出现或移动矩形

时间:2014-07-13 23:23:17

标签: python python-3.x pygame

我正试图让一个矩形射向相应的精灵并在它击中时击败它。就我而言,当我点击空格并以5像素move.ip[5,0]的速度发射时,应创建矩形,但我看不到矩形。我的代码有什么问题,为什么我看不到矩形。

我的代码:

MESH = ((playerRect.topright[1] + 1), (playerRect.topright[1] + 1), (playerRect.centery - 10), (playerRect.centery + 10))
shootRect = pygame.draw.rect(screen, WHITE, MESH)

def baddieHasHitShoot():
    if baddieRect.colliderect(shootRect):
        return True
    return False

        if event.type == KEYDOWN:
            if event.key == ord(' '):
                shootRect()
                move.ip[5, 0]

    if baddieHasHitShoot():
        if score > topScore:
            score = topscore
            break

错误:

Traceback (most recent call last):
   line 105, in <module>
    shootRect()
TypeError: 'pygame.Rect' object is not callable

2 个答案:

答案 0 :(得分:0)

shootRect内的baddieHasHitShoot是本地变量,与外部shootRect = pygame.draw.rect(screen, WHITE, MESH)

无关 shootRect内的{p> baddieHasHitShoot不起作用,因此您可以执行shootRect()

更好地使用不同名称的函数,不要惊讶。

baddieHasHitShoot内你可能需要使用

pygame.draw.rect(screen, WHITE, shootRect)

编辑1:

至于我,我不会使用这样的东西

shootRect = pygame.draw.rect(screen, WHITE, MESH)

而是

shoot_rect = MESH
shoot_color = WHITE

然后使用此

pygame.draw.rect(screen, shoot_color, shoot_rect)

在我需要的地方。


编辑2:

我需要你的东西:

MESH = ((playerRect.topright[1] + 1), (playerRect.topright[1] + 1), (playerRect.centery - 10), (playerRect.centery + 10))

shootRect = pygame.Rect( MESH[0], MESH[1], MESH[2], MESH[3] )

shooted = False

#-----------

def baddieHasHitShoot(b_rect, s_rect):
    if b_rect.colliderect(s_rect):
        return True
    return False

#-----------

if event.type == KEYDOWN:
    if event.key == ord(' '):
        # pygame.draw.rect(screen, WHITE, shootRect) # see below
        shooted = True

#-----------

if baddieHasHitShoot( baddieRect, shootRect):
    if score > topScore:
        score = topscore
        break

编辑3:

pygame.draw.rect(screen, WHITE, shootRect)必须在bliting背景之后和display.update()之前

    screen.blit(background, backgroundRect)
    screen.blit(playerImage, playerRect)
    screen.blit(baddieImage, baddieRect)

    if shooted:
        pygame.draw.rect(screen, WHITE, shootRect)
        shootRect.move_ip( (5, 0) )

    drawText('Score: %s' % (score), font, screen, 200, 0)
    drawText('Top Score: %s' % (topScore), font, screen, 195, 40)

    pygame.display.update()

答案 1 :(得分:-1)

您对shootRect的来电缺少括号。它应该是shootRect() 这是假设它是你试图打电话的功能。

如果它只是一个变量,你需要用它做点什么。