Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I upload a png image in pygame as a sprite, without the black background?

]How do I get my sprite to appear on the screen. i have been days at this and I couldn't get it to appear on the screen and it kept either taking over the screen as black.

import pygame pygame.init() win = pygame.display.set_mode((600,600))

pygame.display.set_caption("Napoleon")

bg = pygame.image.load("apple.png").convert() win.blit(bg, [0,0])

flower= pygame.image.load("flower2.png").convert() win.blit(flower, [603,29])

pygame.display.update()

like image 447
xx_xia_xx Avatar asked Dec 29 '25 09:12

xx_xia_xx


1 Answers

Instead of convert() try convert_alpha(). Assuming your image has an alpha channel (PNG should be good) and it has been applied to your background. If your image has no alpha channel and you need a background gone, you should look into the set_colorkey() function.

Also, for future reference, you can format your code in your question by highlighting it all and pressing CTRL+K. Makes it much easier to read (and you'll get faster answers!)

like image 148
Hoog Avatar answered Dec 30 '25 22:12

Hoog



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!