How can I move the ball instead of leaving a trail all over the screen 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

Move Character with Vector

You can use pygame’s Vector2 class instead of calculating the position of your sprite yourself. I also suggest to let the sprite itself handle its movement instead of doing so in the main loop and using a clock for constant framerates and easy control of the speed of your sprites. You also probably want to … Read more

How to get the correct dimensions for a pygame rectangle created from an image

pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object. This function does not consider the drawing area in the image. You can find the bounding rectangle of the painted area in the surface, with pygame.Surface.get_bounding_rect: bounding_rect = image.get_bounding_rect() bounding_rect.move_ip(target_rect.topleft) See the example. The black rectangle is the Surface rectangle and the red rectangle … Read more

Pygame is running slow

The game is running slow because you load the START_Image in every frame. pygame.image.load is a very expensive operation, because it has to read the images from the data store. Load START_Image once at startup surface = pygame.display.set_mode((width,height)) clock = pygame.time.Clock()#for fps #caption pygame.display.set_caption(‘Survival Island’) START_Image = pygame.image.load(‘START_Image.png’).convert() Do not call pygame.display.update() more than once … Read more

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

tech