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()