【问题标题】:Why won't the rectangle appear or move为什么矩形不会出现或移动
【发布时间】:2014-07-13 23:23:17
【问题描述】:

我试图让一个矩形向并列的精灵射击,并在它击中时击败它。就我而言,当我单击空间并以 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

【问题讨论】:

  • 不确定它是否会修复它,但您需要从 shootRect() -&gt; shootRect 中删除括号,您的错误是由于尝试调用 shootRect() 这是一个 pygame.Rect 对象并且不可调用。
  • 现在又出现了一个错误Traceback (most recent call last): File "***.py", line 106, in &lt;module&gt; move.ip[5, 0] NameError: name 'move' is not defined
  • move 没有按照错误提示定义,move 应该是什么?也可能最好不要在任何地方使用shootRect
  • 如果您从baddieHasHitShoot() 中删除变量,您将遇到.colliderect(shootRect) 的问题
  • 因为 .colliderect() 期望 pygame.Rect() 但现在 shootRect 是函数名称:)

标签: python python-3.x pygame


【解决方案1】:

shootRect内部baddieHasHitShoot是局部变量,与外部shootRect = pygame.draw.rect(screen, WHITE, MESH)无关

shootRect 里面的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()

【讨论】:

    【解决方案2】:

    您对shootRect 的调用缺少括号。应该是shootRect() 那是假设它是您尝试调用的函数。

    如果它只是一个变量,你需要对它做一些事情。

    【讨论】:

    • shootRect 存在不同的问题 - 有两个 shootRect - 全局和本地 :)
    • 不知道你说的there are two shootRect - global and local是什么意思
    猜你喜欢
    • 2022-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 2019-11-20
    • 1970-01-01
    相关资源
    最近更新 更多