Skip to main content
added 9 characters in body
Source Link
Rabbid76
  • 212.1k
  • 30
  • 163
  • 205

pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, but it returns a rectangle that always starts at (0, 0) since a Surface object has no position.
The SurfaceSurface is placed at a position when it ison the display with the blit to the displayfunction.

pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, but it returns a rectangle that always starts at (0, 0) since a Surface object has no position.
The Surface is placed at a position when it is blit to the display.

pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, but it returns a rectangle that always starts at (0, 0) since a Surface object has no position.
The Surface is placed at a position on the display with the blit function.

added 157 characters in body
Source Link
Rabbid76
  • 212.1k
  • 30
  • 163
  • 205

Of course, because pygame.Surface.get_rect.get_rect() returns a rectangle whichwith the size of the Surface object, but it returns a rectangle that always starts at (0, 0), by default since a Surface object has no position.
The Surface is placed at a position when it is blit to the display.

You've to set the location of the rectangle, either by an assignment to a virtual attribute (see pygame.Rect)keyword argument, e.g:

self.rect = self.image.get_rect() self.rect.topleft = (self.x, self.y)) 

or by keyword an argumentassignment to a virtual attribute (see pygame.Rect), e.g:

self.rect = self.image.get_rect() self.rect.topleft = (self.x, self.y)) 

Of course, because pygame.Surface.get_rect returns a rectangle which always starts at (0, 0), by default.

You've to set the location of the rectangle, either by an assignment to a virtual attribute (see pygame.Rect), e.g:

self.rect = self.image.get_rect() self.rect.topleft = (self.x, self.y) 

or by keyword an argument, e.g:

self.rect = self.image.get_rect(topleft = (self.x, self.y)) 

pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, but it returns a rectangle that always starts at (0, 0) since a Surface object has no position.
The Surface is placed at a position when it is blit to the display.

You've to set the location of the rectangle, either by a keyword argument, e.g:

self.rect = self.image.get_rect(topleft = (self.x, self.y)) 

or an assignment to a virtual attribute (see pygame.Rect), e.g:

self.rect = self.image.get_rect() self.rect.topleft = (self.x, self.y) 
added 1666 characters in body
Source Link
Rabbid76
  • 212.1k
  • 30
  • 163
  • 205

Further note, that you can get rid of the methods Ball.update() respectively Obstacle.update() (you can delete them), if you use a pygame.sprite.Group and call .draw(), which uses the .image and .rect properties of the contained sprites, to draw them. e.g.:

obstacle = Obstacle() ball = Ball() all_sprites = pygame.sprite.Group([obstacle, ball]) while not crashed: # [...] gameDisplay.fill((255,255,255)) all_sprites.draw(gameDisplay) pygame.display.flip() clock.tick(1000) 

Further note, that you can get rid of the methods Ball.update() respectively Obstacle.update() (you can delete them), if you use a pygame.sprite.Group and call .draw(), which uses the .image and .rect properties of the contained sprites, to draw them. e.g.:

obstacle = Obstacle() ball = Ball() all_sprites = pygame.sprite.Group([obstacle, ball]) while not crashed: # [...] gameDisplay.fill((255,255,255)) all_sprites.draw(gameDisplay) pygame.display.flip() clock.tick(1000) 
added 1666 characters in body
Source Link
Rabbid76
  • 212.1k
  • 30
  • 163
  • 205
Loading
Source Link
Rabbid76
  • 212.1k
  • 30
  • 163
  • 205
Loading