Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. pygame.init()
  2.  
  3. W = 407
  4. H = 700
  5. SIZE = W, H
  6. screen = pygame.display.set_mode(SIZE)
  7. clock = pygame.time.Clock()
  8.  
  9. # colours
  10. RED = (255, 0, 0)
  11. GREEN = (0, 255, 0)
  12. BLUE = (0, 0, 255)
  13. BACKGROUND = (80, 97, 175)
  14. LANELINE = (133, 153, 232)
  15.  
  16. x1 = 29
  17. lane2 = 129
  18. lane3 = 236
  19. x2 = 335
  20. y = 530
  21. width = 40
  22. height = 60
  23. vel = 5
  24.    
  25. toggle1 = 0
  26. toggle2 = 0
  27.  
  28. target_x1 = 29
  29. target_x2 = 335
  30. vel_x = 3
  31.  
  32.  
  33. def drawScene():
  34.     screen.fill(BACKGROUND)
  35.     pygame.draw.line(screen, LANELINE, (203, 0), (203, 700), 6)
  36.     pygame.draw.line(screen, LANELINE, (100, 0), (100, 700), 4)
  37.     pygame.draw.line(screen, LANELINE, (306, 0), (306, 700), 4)
  38.  
  39.  
  40. # main loop
  41. run = True
  42. while run:
  43.     clock.tick(30)
  44.  
  45.     for event in pygame.event.get():
  46.         if event.type == pygame.QUIT:
  47.             run = False
  48.        
  49.         if event.type == pygame.KEYDOWN:
  50.             if event.key == pygame.K_LEFT:
  51.                 toggle1 += 1
  52.                 if toggle1 % 2 == 1:
  53.                     target_x1 +=100
  54.                 else:
  55.                     target_x1 -= 100
  56.             elif event.key == pygame.K_RIGHT:
  57.                 toggle2 += 1
  58.                 if toggle2 % 2 == 1:
  59.                     target_x2 -= 100
  60.                 else:
  61.                     target_x2 += 100
  62.  
  63.                        
  64.     if x1 < target_x1:
  65.         x1 = min(x1 + vel_x, target_x1)
  66.     else:
  67.         x1 = max(x1 - vel_x, target_x1)
  68.    
  69.     if x2 < target_x2:
  70.         x2 = min(x2 + vel_x, target_x2)
  71.     else:
  72.         x2 = max(x2 - vel_x, target_x2)
  73.        
  74.    
  75.     drawScene()    
  76.    
  77.     pygame.draw.rect(screen, RED, (x1, y, width, height))
  78.     pygame.draw.rect(screen, RED, (x2, y, width, height))
  79.     pygame.display.update()
  80.  
  81. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement