Reputation: 1966
I am trying to make a red rectangle move to the right and by using pygame.move.rect or .blit, I Am able to accomplish the same thing. I am able to display the red rectangle and move it to the right by pressing the right arrow. However, is there any difference between these 2 functions that I should know? Why are there 2 functions that basically do the same thing.
Code with pygame.move.rect
import pygame
import sys
pygame.init()
#obtain the surface and rect for screen
screen_surface = pygame.display.set_mode((1200,800))
pygame.display.set_caption("Hi")
#Obtain Surface and rect for the rectangle
red_rectangle = pygame.Surface((600,400))
red_rectangle_rect = red_rectangle.get_rect()
#make the rectangle surface red
red_rectangle.fill((255,0,0))
move_right = False
while True:
#event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
move_right = True
print(event.type)
print(event.key)
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
move_right = False
#rectangle move to right when right arrow is pressed
if move_right:
red_rectangle_rect.x += 10
print(red_rectangle_rect.x)
screen_surface.fill((255,255,255))
# the difference between this function and the .blit
pygame.draw.rect(screen_surface,(255,0,0),red_rectangle_rect)
pygame.display.flip()
Code with .blit
import pygame
import sys
pygame.init()
#obtain the surface and rect for screen
screen_surface = pygame.display.set_mode((1200,800))
pygame.display.set_caption("Hi")
#Obtain Surface and rect for the rectangle
red_rectangle = pygame.Surface((600,400))
red_rectangle_rect = red_rectangle.get_rect()
#make the rectangle surface red
red_rectangle.fill((255,0,0))
move_right = False
while True:
#event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
move_right = True
print(event.type)
print(event.key)
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
move_right = False
#rectangle move to right when right arrow is pressed
if move_right: then
red_rectangle_rect.x += 10
print(red_rectangle_rect.x)
screen_surface.fill((255,255,255))
#Difference between this and the draw function
screen_surface.blit(red_rectangle,red_rectangle_rect)
pygame.display.flip()
Upvotes: 3
Views: 5113
Reputation: 211176
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:
What is a good way to draw images using pygame?
Upvotes: 5
Reputation: 1966
So just to clarify,
Use blit for image and rect for rectangle with 1 colour.
Upvotes: 0
Reputation: 700
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.
Upvotes: 2