-1

When I load my image in pygame (see code below), the image appears pitch black, instead of drawing properly!

Using Python 3.12.8

import pygame

pygame.init()

def imageload(imagepath):
    return pygame.image.load(imagepath)

screen = pygame.display.set_mode((300, 200), 0, 32)
pygame.display.set_caption('Python Clicker')
background_colour = (255,255,255)
cookieimage = Surface.convert(imageload('images/python.bmp'))
running = True

while running:
  screen.fill(background_colour)
  screen.blit(pygame.Surface((64,64), 0, cookieimage), (150, 100), None, 0)
  pygame.display.flip()

  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      running = False
      pygame.quit()

Black box in place of image.

5
  • screen.blit(pygame.transform.scale(cookieimage, (64, 64)), (150, 100)) Commented Jan 11 at 7:54
  • This was not a duplicate of that question, what are you all doing. Commented Jan 11 at 7:55
  • @Rabbid76 the image is already 64x64 Commented Jan 26 at 23:28
  • What color format is your BMP? And how do we know that the image is not black? Commented Jan 29 at 7:11
  • You didn't define the Surface. here is the correct approach: cookieimage = imageload('images/python.bmp').convert() Commented Apr 14 at 21:07

1 Answer 1

-1

You are painting an empty 64x64 surface, that is why it is black. In your line screen.blit(pygame.Surface((64,64), 0, cookieimage), (150, 100), None, 0) you are creating a new Surface, which is (i) not necessary and (ii) you provide the cookieimage as the depth argument, which is surely not what you want to do.

Replace the line with screen.blit(cookieimage, (150, 100), None, 0), you can also remove the parameters that are default anyway and it contracts down to screen.blit(cookieimage, (150, 100)).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.