2

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()
Qiu YU
  • 517
  • 4
  • 20
  • 1
    can you share mousepressdown and mousemotion events code as well please –  Aug 18 '20 at 09:30
  • You only need to update the screen if the screen changes. If the screen is constant, don't update it. If you're only updating a portion of the screen you can set a clip region so the entire screen is not redrawn. – Mike67 Aug 18 '20 at 17:08

0 Answers0