Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pygame.init()
- W = 407
- H = 700
- SIZE = W, H
- screen = pygame.display.set_mode(SIZE)
- clock = pygame.time.Clock()
- # colours
- RED = (255, 0, 0)
- GREEN = (0, 255, 0)
- BLUE = (0, 0, 255)
- BACKGROUND = (80, 97, 175)
- LANELINE = (133, 153, 232)
- x1 = 29
- lane2 = 129
- lane3 = 236
- x2 = 335
- y = 530
- width = 40
- height = 60
- vel = 5
- toggle1 = 0
- toggle2 = 0
- target_x1 = 29
- target_x2 = 335
- vel_x = 3
- def drawScene():
- screen.fill(BACKGROUND)
- pygame.draw.line(screen, LANELINE, (203, 0), (203, 700), 6)
- pygame.draw.line(screen, LANELINE, (100, 0), (100, 700), 4)
- pygame.draw.line(screen, LANELINE, (306, 0), (306, 700), 4)
- # main loop
- run = True
- while run:
- clock.tick(30)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- run = False
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_LEFT:
- toggle1 += 1
- if toggle1 % 2 == 1:
- target_x1 +=100
- else:
- target_x1 -= 100
- elif event.key == pygame.K_RIGHT:
- toggle2 += 1
- if toggle2 % 2 == 1:
- target_x2 -= 100
- else:
- target_x2 += 100
- if x1 < target_x1:
- x1 = min(x1 + vel_x, target_x1)
- else:
- x1 = max(x1 - vel_x, target_x1)
- if x2 < target_x2:
- x2 = min(x2 + vel_x, target_x2)
- else:
- x2 = max(x2 - vel_x, target_x2)
- drawScene()
- pygame.draw.rect(screen, RED, (x1, y, width, height))
- pygame.draw.rect(screen, RED, (x2, y, width, height))
- pygame.display.update()
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement