Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.89 KB | None | 0 0
  1. import pygame
  2. import random
  3.  
  4. pygame.init()
  5.  
  6. display_height, display_width = 600, 800
  7. car_width, car_height = 60, 100
  8. block_width, block_height = 100, 100
  9. wheel_width, wheel_height = 15, 30
  10. block_speed = 12
  11. car_x, car_y = display_width / 2, display_height / 2 + car_height * 1.5
  12. black = (0, 0, 0)
  13. white = (255, 255, 255)
  14. x_change = 0
  15. block_x = random.randrange(block_width / 2, display_width - block_width / 2)
  16. block_y = -600 - block_height / 2
  17. score = 0
  18.  
  19. display = pygame.display.set_mode((display_width, display_height))
  20. pygame.display.set_caption("Racing Car")
  21. clock = pygame.time.Clock()
  22.  
  23. crashed = False
  24.  
  25. font = pygame.font.SysFont(None, 25)
  26.  
  27.  
  28. def draw_Car(car_x, car_y, car_width, car_height, wheel_width, wheel_height, color):
  29.     pygame.draw.rect(display, color, [car_x - car_width / 2, car_y - car_height / 2, car_width, car_height], 1)
  30.     pygame.draw.ellipse(display, color,
  31.                         [(car_x - car_width / 2) + car_width, (car_y - car_height / 2) + car_height / 2 * 1.5,
  32.                          wheel_width, wheel_height], 1)
  33.     pygame.draw.ellipse(display, color,
  34.                         [(car_x - car_width / 2) - wheel_width, (car_y - car_height / 2) + car_height / 2 * 1.5,
  35.                          wheel_width, wheel_height], 1)
  36.     pygame.draw.ellipse(display, color, [(car_x - car_width / 2) + car_width, (car_y - car_height / 2),
  37.                                          wheel_width, wheel_height], 1)
  38.     pygame.draw.ellipse(display, color,
  39.                         [(car_x - car_width / 2) - wheel_width, (car_y - car_height / 2),
  40.                          wheel_width, wheel_height], 1)
  41.  
  42.  
  43. def drawBlock(block_x, block_y, block_width, block_height, color):
  44.     pygame.draw.rect(display, color, [block_x - block_width / 2, block_y - block_height / 2, block_width, block_height],
  45.                      1)
  46.  
  47.  
  48. def gen_Block():
  49.     global block_x, block_y
  50.     block_x = random.randrange(block_width / 2, display_width - block_width / 2)
  51.     block_y = -600 - block_height / 2
  52.  
  53.  
  54. def show_score(score):
  55.     text = font.render(score, True, white)
  56.     display.blit(text, [display_width - 100, 20])
  57.  
  58.  
  59. def check():
  60.     if (block_x + block_width / 2) > car_x + car_width / 2 + wheel_width and (
  61.                 block_x - block_width / 2) < car_x + car_width / 2 + wheel_width:
  62.         if block_y + block_height / 2 > car_y - car_height / 2 and block_y - block_height / 2 < car_y + car_height / 2:
  63.             return False
  64.     if (block_x + block_width / 2) > car_x - car_width / 2 - wheel_width and (
  65.                 block_x - block_width / 2) < car_x - car_width / 2 - wheel_width:
  66.         if block_y + block_height / 2 > car_y - car_height / 2 and block_y - block_height / 2 < car_y + car_height / 2:
  67.             return False
  68.     return True
  69.  
  70.  
  71. while not crashed:
  72.     for event in pygame.event.get():
  73.         if (event.type == pygame.QUIT):
  74.             crashed = True
  75.         if (event.type == pygame.KEYDOWN):
  76.             if event.key == pygame.K_LEFT:
  77.                 x_change = -10
  78.             elif event.key == pygame.K_RIGHT:
  79.                 x_change = 10
  80.         elif event.type == pygame.KEYUP:
  81.             if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
  82.                 x_change = 0
  83.     if car_x + car_width / 2 + wheel_width + x_change <= display_width and car_x - car_width / 2 - wheel_width + x_change >= 0:
  84.         car_x += x_change
  85.         block_y += block_speed
  86.     print(car_x)
  87.     display.fill(black)
  88.     draw_Car(car_x, car_y, car_width, car_height, wheel_width, wheel_height, white)
  89.     drawBlock(block_x, block_y, block_width, block_height, white)
  90.     if not check():
  91.         crashed = True
  92.         pygame.quit()
  93.         quit()
  94.     if block_y - block_height / 2 >= display_height:
  95.         gen_Block()
  96.         score += 1
  97.     show_score("Score: " + str(score))
  98.  
  99.     pygame.display.update()
  100.     clock.tick(60)
  101.  
  102. pygame.quit()
  103. quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement