I am writing a program in pygame that will change the view displayed on the screen when the user drags the mouse through the pygame window. In my code, the fps would drop drastically when the screen changes the view. It seems to me that it is caused by the image repeatedly being rendered. I am wondering if there is a better way to do this.
Below is my code:
import pygame
import os
screen = pygame.display.set_mode((screen_width, screen_height))
background_img = '''a 1200 by 1200 image'''
surface = pygame.Surface((1200,1200),pygame.SRCALPHA)
rect_x = 20
x = 0
y = 0
while True:
rect_x += 1
surface.blit(bg, (0,0))
pygame.draw.rect(surface, (255,0,0), (rect_x,50,50,50))
screen.blit(surface, (x,y))
'''Some codes with mousepressdown and mousemotion events to change x and y'''
pygame.display.update()