Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading