<s>[INST] import pygame
import random

pygame.init()

screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()

rect1 = pygame.Rect(100, 100, 50, 50)
rect2 = pygame.Rect(300, 300, 50, 50)

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill((0, 0, 0))
    pygame.draw.rect(screen, (255, 0, 0), rect1)
    pygame.draw.rect(screen, (0, 0, 255), rect2)

    rect1.move_ip(1, 1)
    if rect1.x > 800 or rect1.y > 600:
        rect1.x = 0
        rect1.y = 0

    pygame.display.flip()
    clock.tick(60)

pygame.quit()

In this piece of code, I'm using Pygame to create a game window with two rectangles, one red and one blue. The red rectangle is moving diagonally across the screen at a constant velocity. If the rectangle hits either side of the screen, it resets back to the origin (0,0).

I'm experiencing an issue with performance. The game window freezes randomly and the diagonal red rectangle stops moving. However, the blue rectangle remains stationary on the screen and the window is still responsive (e.g. I can move the window around, close it, etc.) The problem doesn't seem to be consistent and happens at random, although it happens more frequently when I have other resource-intensive programs open in the background.

What could be the cause of this problem and how can I solve it?[/INST]