Missing
Missing

Reputation: 25

Why is the sprite in Pygame not drawing now, it loaded earlier?

So the "bullet (or laser.png)" sprite in my code when run doesn't draw. I broke something in my code trying to fix another problem the that the sprite was leaving a trail. I tried multiple things but ended up breaking my code(sorry im new to pygame and this might be a dumb question thx anyways).I also defined all the variable earlier

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()

Upvotes: 1

Views: 33

Answers (1)

xemeds
xemeds

Reputation: 316

Your are calling the fire_bullet function which draws the bullet, but after that before updating the display you are calling the redrawGameWindow function which overrides everything.

Upvotes: 1

Related Questions