Why is one of my images not coming up in my program using pygame library?

Question:
I am using the pygame library for a simple game I am making. However my variable player image is not coming up on the screen despite other variables doing so. What can I do to fix its
Repl link: https://replit.com/@beabech/game-2024-yr-9#main.py

playerimage = pygame.transform.scale(
  pygame.image.load(os.path.join(current_path, "trident4.jpeg")), (pWidth, pHeight)
)

player = pygame.Rect(WIDTH / 2, HEIGHT - pHeight - 10, pWidth, pHeight)

bHeight, bWidth = 55, 40
bImage = pygame.transform.scale(pygame.image.load("bubble.webp"), (bWidth, bHeight))
baddies = []
baddie = pygame.Rect(0, 0, bWidth, bHeight)
baddies.append(baddie)
baddieSpeed = 3
dir = "left"

playerSpeed = 3

bullets = []
bulletSpeed = 5

GREEN = (0, 255, 0)

run = True

while True:
  keys_pressed = pygame.key.get_pressed()

  clock.tick(60)
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      run = False
      pygame.quit()

    if event.type == pygame.KEYDOWN:
      if event.key == pygame.K_LCTRL:
        bullet = pygame.Rect(player.x + player.width // 2 - 2, player.y, 5, 10)
        bullets.append(bullet)
  if keys_pressed[pygame.K_LEFT] and player.x - playerSpeed > 0:
    player.x -= playerSpeed
  if keys_pressed[pygame.K_RIGHT] and player.x + playerSpeed + pWidth < WIDTH:
    player.x += playerSpeed
  if keys_pressed[pygame.K_UP] and player.y - playerSpeed > 0:
    player.y -= playerSpeed
  if keys_pressed[pygame.K_DOWN] and player.y + playerSpeed + pHeight < HEIGHT - 15:
    player.y += playerSpeed

  WIN.blit(background, (0, 0))
  WIN.blit(playerimage, (player.x, player.y))

  if baddie.x < 0:
    dir = "left"
    baddie.y += baddie.height
  elif baddie.x > WIDTH - baddie.width:
    dir = "right"
    baddie.y += baddie.height
  if dir == "left":
    baddie.x += baddieSpeed
  if dir == "right":
    baddie.x -= baddieSpeed
  if len(baddies) == 0:
    baddie = pygame.Rect(0, 0, bWidth, bHeight)
    baddies.append(baddie)
  for baddie in baddies:
    WIN.blit(bImage, baddie)

  for bullet in bullets:
    bullet.y -= bulletSpeed
    pygame.draw.rect(WIN, GREEN, bullet)

    if bullet.y < 0 - bullet.height:
      bullets.remove(bullet)
    if bullet.colliderect(baddie):
      bullets.remove(bullet)
      baddies.remove(baddie)

  pygame.display.update()

1 Like


Is this your player image?

When it starts in an average sized output window it will be off the bottom of the screen , so you have to first click in the output window, and then move up to see it.

gif

Or you could resize the window

Or do you mean how the image that appears doesn’t match with trident4.jpeg for some reason?

1 Like

Hello, your player trident image does indeed show according to vvithershin’s post.
So I am assuming you are talking about the trident having a white background. This can be fixed with the following code:

playerimage.set_colorkey((255,255,255))

This makes all white pixels transparent.

1 Like