What Is The Difference Between Pygame.draw.rect And Screen_surface.blit()?
Solution 1:
Why are there 2 functions that basically do the same thing.
No, they do not the same thing. While pygame.draw.rect
draw uniform colored rectangles, pygame.Surface.blit
is a method of pygame.Surface
and is used to draw bitmap images.
See pygame.draw.rect
:
rect(surface, color, rect) -> Rect
Draws a rectangle on the given surface.
blit(source, dest, area=None, special_flags=0) -> Rect
Draws a source Surface onto this Surface
In your case, it looks like they're doing the same thing because your Surface object is evenly filled with one color.
The behavior changes completely when you load a bitmap image with pygame.image.load
. You cannot draw an image using pygame.draw.rect
, but you can use blit
.
When to use pygame.draw.rect
, see:
When to use blit
, see:
Solution 2:
blit(image, (left, top)) - Draws image to the screen at the given position.The function accepts either a Surface or a string as its image parameter. If image is a str then the named image will be loaded from the images/ directory.
draw.rect(rect, (r, g, b)) - Draws the outline of a rectangle. Takes a rectangle.
Solution 3:
So just to clarify,
Use blit for image and rect for rectangle with 1 colour.
Post a Comment for "What Is The Difference Between Pygame.draw.rect And Screen_surface.blit()?"