Is there any other way to load a resource like an image, sound, or font into Pygame? [closed]

Place the image file in the same directory as the Python file. Change the current working directory to the directory of the file. The name and path of the file can be get by __file__. The current working directory can be get by os.getcwd() and can be changed by os.chdir(path): import os sourceFileDir = os.path.dirname(os.path.abspath(__file__)) … Read more

How can I add an image or icon to a button rectangle in Pygame?

All you have to do is to load an image: my_image = pygame.image.load(‘my_image.png’).convert_alpha() And blit it an top of the rectangle: def button(x, y, w, h, ic, ac, img, imgon, action=None): mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() rect = pygame.Rect(x, y, w, h) on_button = rect.collidepoint(mouse) if on_button: pygame.draw.rect(screen, ac, rect) screen.blit(imgon, imgon.get_rect(center = rect.center)) … Read more

How to convert the background color of image to match the color of Pygame window?

You don’t need to change the background color of the image to the background color of the window, but make the background of the image transparent. Set the transparent colorkey by pygame.Surface.set_colorkey: Set the current color key for the Surface. When blitting this Surface onto a destination, any pixels that have the same color as … Read more

How can I make the ball move instead of stretch in pygame?

You have to clear the display in every frame with pygame.Surface.fill: while True: # […] screen.fill(0) # <— main.draw_elements() main.move_ball() main.ball.x_pos += main.ball.speed pygame.display.flip() # […] Everything that is drawn is drawn on the target surface. The entire scene is redraw in each frame. Therefore the display needs to be cleared at the begin of … Read more