Reputation: 25
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()
Upvotes: 1
Views: 29
Reputation: 211277
You have to redraw the scene in every frame. The typical PyGame application loop has to:
pygame.time.Clock.tick
pygame.event.pump()
or pygame.event.get()
.blit
all the objects)pygame.display.update()
or pygame.display.flip()
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()
Upvotes: 1