don't update rect color on mouse motion. How to fix?

Question:

import pygame
from pygame.locals import *

pygame.init()
screen=pygame.display.set_mode((800,600))

surf1=pygame.Surface((200,200))
surf1.fill((250,0,0))
rect1 = surf1.get_rect()
rect1.topleft = (200, 200)
screen.blit(surf1,rect1) 

pygame.display.flip()

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

pygame.display.flip() don’t work in or outside while cicle

        if ev.type==MOUSEMOTION:
            if rect1.collidepoint(ev.pos):
                surf1.fill((0, 250, 0))
                print('inside')
                pygame.display.flip()  

dont work, i tried to change pygame.display.flip() indentation but nothing

pygame.quit()
Asked By: redanol

||

Answers:

You have to redraw the scene in every frame. The typical PyGame application loop has to:

import pygame
from pygame.locals import *

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

surf1 = pygame.Surface((200, 200))
surf1.fill((255, 0, 0))
rect1 = surf1.get_rect()
rect1.topleft = (200, 200)

done = False
while not done:
    # limit the frames per second to limit CPU usage
    clock.tick(100)

    # handle the events and update the game states
    for ev in pygame.event.get():  
        if ev.type == QUIT:
            done = True
        if ev.type == MOUSEMOTION:
            if rect1.collidepoint(ev.pos):
                surf1.fill((0, 255, 0))
            else:
                surf1.fill((255, 0, 0))

    # clear the entire display
    screen.fill(0)

    # blit all the objects
    screen.blit(surf1,rect1) 

    # update the display
    pygame.display.flip()

pygame.quit()
Answered By: Rabbid76
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.