为什么 Pygame 中的精灵现在不绘制,而是提前加载的?

因此,我的代码中的“bullet(或laser.png)”精灵在运行时不会绘制。我在代码中破坏了一些东西,试图解决另一个问题,即精灵留下了痕迹。我尝试了多种方法,但最终破坏了我的代码(抱歉,我是 pygame 新手,无论如何,这可能是一个愚蠢的问题)。我之前还定义了所有变量


def redrawGameWindow(win, HeroX,HeroY):

    win.blit(bg, (0,0))

    win.blit(HeroSprite, (HeroX, HeroY))

    win.blit(EnemySprite, (EnemyX, EnemyY))

    pygame.draw.rect(bg, (211,211,211), (430 - 5, 25 - 5, EnemyHealth + 10, 30 + 10))

    pygame.draw.rect(bg, (255,0,0), (430, 25, EnemyHealth, 30))

    pygame.draw.rect(bg, (211,211,211), (430 - 5, 665 - 5, EnemyHealth + 10, 30 + 10))

    pygame.draw.rect(bg, (0,255,0), (430, 665, PlayerHealth, 30))

    pygame.display.update()


def fire_bullet(x, y):

    global BulletState

    BulletState = "fire"

    win.blit(Laser, (BulletX,BulletY))



while GameRun:

    clock.tick(60)

    pressed = pygame.key.get_pressed()

    EnemyX += EnemyVel

    if EnemyX <= 0 :

        EnemyVel = 5

    elif EnemyX >= winX - 60:

        EnemyVel = -5

    if pressed[pygame.K_LEFT]:

        HeroX -= vel if HeroX > 0 else 0

    elif pressed[pygame.K_RIGHT]:

        HeroX += vel if HeroX < winX - 57 else 0


    if pressed[pygame.K_SPACE]:

        if BulletState == "ready" :

            BulletX =  HeroX

            fire_bullet(BulletX, BulletY)


    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            GameRun = False

    if BulletY <= 0:

        BulletY = HeroY

        BulletState = "ready"

    if BulletState == "fire":

        fire_bullet(BulletX, BulletY)

        BulletY -= BulletVel


    redrawGameWindow(win, HeroX, HeroY)


pygame.quit()


慕码人8056858
浏览 105回答 1
1回答

素胚勾勒不出你

您正在调用fire_bullet绘制子弹的函数,但之后在更新显示之前您正在调用redrawGameWindow覆盖所有内容的函数。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python