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??
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
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() |