Jul-04-2021, 10:49 AM
I am new to pygame, so I am looking at the docs here and trying each bit out in Idle.
My little function rect11() works OK, but it doesn't refresh when I press the right arrow key.
I presume K_r means the right arrow key, but nothing happens when I press the right arrow key.
Do you know why??
My little function rect11() works OK, but it doesn't refresh when I press the right arrow key.
I presume K_r means the right arrow key, but nothing happens when I press the right arrow key.
Do you know why??
def rect11(): pygame.init() width = 600 height = 600 SIZE = (500, 200) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) YELLOW = (255, 255, 0) MAGENTA = (255, 0, 255) CYAN = (0, 255, 255) BLACK = (0, 0, 0) GRAY = (150, 150, 150) WHITE = (255, 255, 255) def random_point(): x = randint(0, width) y = randint(0, height) return (x, y) def random_rects(n): rects = [] for i in range(n): r = Rect(random_point(), (20, 20)) rects.append(r) return rects screen = pygame.display.set_mode((width, height)) running = True rect = Rect(100, 50, 200, 80) n = 50 rects = random_rects(n) while running: for event in pygame.event.get(): if event.type == QUIT: running = False if event.type == KEYDOWN: if event.key == K_r: rects = random_rects(n) screen.fill(GRAY) pygame.draw.rect(screen, GREEN, rect, 1) for r in rects: if rect.colliderect(r): pygame.draw.rect(screen, RED, r, 2) else: pygame.draw.rect(screen, BLUE, r, 1) pygame.display.flip() pygame.quit()