2

Check the official documents as follows: Surface objects are objects that represent a rectangular 2D image. The Sprite class is intended to be used as a base class for the different types of objects in the game.

They all have image and rect attributes.The difference is that sprites can be grouped?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
wang John
  • 21
  • 1
  • 4
  • 2
    Does this answer your question? [Why is my PyGame Sprite, in a Group, not drawn - AttributeError: 'Group' object has no attribute 'blitme'](https://stackoverflow.com/questions/64076676/why-is-my-pygame-sprite-in-a-group-not-drawn-attributeerror-group-object) –  Mar 17 '21 at 02:01

2 Answers2

4

They all have image and rect attributes [...]

No this is wrong. A pygame.Surface has not rect attribute. pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, that always starts at (0, 0) since a Surface object has no position. The rectangle returned is just a rectangle the size of the surface area starting at (0, 0).


In short words: A pygame.Surface has no location, it's just a bitmap. A pygame.sprite.Sprite is a object that consists of a Surface object and pygame.Rect object. An instance of a Sprite describes the position of an image in the game window.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
0

From what I've learned Surface is a place (or kind of cointainer with two axises) where you position your Sprites which can be group with SpriteGroup.

surface = pygame.display.set_mode((800, 600))

Sprite has it's coordinates relatively to the Surface it is drawn on.

So basically you want to group all the sprites by specific objects you show on the screen, for example ActionGroup, MinimapGroup, PaperdollGroup etc. In the end of each tick you have to choose which UI objects (mean groups) you want to draw on current Surface.

ActionGroup.add(backgroud_sprite)
ActionGroup.add(move_icon_sprite)

ActionGroup.update()
ActionGroup.draw(surface)

A Surface class has some methods to work with overlapping, pixel masks, transparations and other stuff, so it's more "how to draw" than "what to draw".

The question for me is should I use several Surfaces or I can do most things with just one?

Rezolventa
  • 11
  • 2
  • It's best not to post questions as a part of the answer. You should post your follow-on question as a comment or as a new question. – DaveL17 Mar 10 '23 at 18:43