import sys

import pygame

        
def check_events(ship):
    """Respond to keypresses and mouse events."""
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                ship.moving_right = True
            elif event.key == pygame.K_LEFT:
                ship.moving_left = True
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                ship.moving_right = False
            elif event.key == pygame.K_LEFT:
                ship.moving_left = False

def update_screen(ai_settings, screen, ship):
    """Update images on the screen and flip to the new screen."""
    # Redraw the screen during each pass through the loop.
    screen.fill(ai_settings.bg_color)
    ship.blitme()
    # Make the most recently drawn screen visible.
    pygame.display.flip()

@@ -20,24 +20,16 @@
 pygame%0A%0A
-        
 %0Adef che
@@ -35,15 +35,30 @@
 eck_
-events(
+keydown_events(event, 
 ship
@@ -92,25 +92,8 @@
 sses
- and mouse events
 .%22%22%22
@@ -101,28 +101,31 @@
     
+i
 f
-or
  event
- in
+.key ==
  pygame.
 even
@@ -124,29 +124,56 @@
 ame.
-event.get():%0A   
+K_RIGHT:%0A        ship.moving_right = True%0A
     
- 
+el
 if e
@@ -169,36 +169,35 @@
 %0A    elif event.
-type
+key
  == pygame.QUIT:
@@ -195,11 +195,13 @@
 ame.
-QUI
+K_LEF
 T:%0A 
@@ -211,74 +211,113 @@
     
-  
-  sys.exit()%0A        elif event.type == pygame.KEYDOWN:%0A        
+ship.moving_left = True%0A        %0Adef check_keyup_events(event, ship):%0A    %22%22%22Respond to key releases.%22%22%22%0A
     
@@ -344,32 +344,24 @@
 me.K_RIGHT:%0A
-        
         ship
@@ -376,29 +376,22 @@
 right = 
-True%0A        
+False%0A
     elif
@@ -423,32 +423,24 @@
 FT:%0A        
-     
-   
 ship.moving_
@@ -438,35 +438,36 @@
 p.moving_left = 
-Tru
+Fals
 e%0A        elif e
@@ -464,45 +464,120 @@
     
-elif event.type == pygame.KEYUP:%0A    
+%0Adef check_events(ship):%0A    %22%22%22Respond to keypresses and mouse events.%22%22%22%0A    for event in pygame.event.get():%0A
     
@@ -581,35 +581,36 @@
        if event.
-key
+type
  == pygame.K_RIG
@@ -596,38 +596,35 @@
 .type == pygame.
-K_RIGH
+QUI
 T:%0A             
@@ -626,42 +626,108 @@
     
-    ship.moving_right = False%0A    
+sys.exit()%0A        elif event.type == pygame.KEYDOWN:%0A            check_keydown_events(event, ship)%0A
     
@@ -733,35 +733,36 @@
      elif event.
-key
+type
  == pygame.K_LEF
@@ -749,37 +749,36 @@
 type == pygame.K
-_LEFT
+EYUP
 :%0A              
@@ -779,36 +779,39 @@
     
-    ship.moving_left = False
+check_keyup_events(event, ship)
 %0A%0Ade

refactor check_events