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

pygame image background does not match main background

If the background has a uniform color, then you can set the transparent colorkey by pygame.Surface.set_colorkey. For instance if the background is the white (255, 255, 255): image = pygame.image.load(‘./car1.bmp’).convert() image.set_colorkey((255, 255, 255)) Anyway that won’t solve the issue if the background has multiple colors. I recommend to use an image format which supports per … Read more

How do I scale a PyGame image (Surface) with respect to its center?

You missed to update the size of self.pixeltitlerect after the Surface has been scaled: self.pixeltitle = pg.transform.scale(self.pixeltitle,(xsize,ysize)) # size of surface has been changed get the new rectangle self.pixeltitlerect = self.pixeltitle.get_rect() self.pixeltitlerect.center = (250,120) self.screen.blit(self.pixeltitle,self.pixeltitlerect) Or even shorter (see pygame.Surface.get_rect()): self.pixeltitle = pg.transform.scale(self.pixeltitle,(xsize,ysize)) self.pixeltitlerect = self.pixeltitle.get_rect(center = (250,120)) self.screen.blit(self.pixeltitle,self.pixeltitlerect) Do not scale the original Surface. … Read more

SVG rendering in a PyGame application. Prior to Pygame 2.0, Pygame did not support SVG. Then how did you load it?

This is a complete example which combines hints by other people here. It should render a file called test.svg from the current directory. It was tested on Ubuntu 10.10, python-cairo 1.8.8, python-pygame 1.9.1, python-rsvg 2.30.0. #!/usr/bin/python import array import math import cairo import pygame import rsvg WIDTH = 512 HEIGHT = 512 data = array.array(‘c’, … 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