Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- pygame.init()
- display_height, display_width = 600, 800
- car_width, car_height = 60, 100
- block_width, block_height = 100, 100
- wheel_width, wheel_height = 15, 30
- block_speed = 12
- car_x, car_y = display_width / 2, display_height / 2 + car_height * 1.5
- black = (0, 0, 0)
- white = (255, 255, 255)
- x_change = 0
- block_x = random.randrange(block_width / 2, display_width - block_width / 2)
- block_y = -600 - block_height / 2
- score = 0
- display = pygame.display.set_mode((display_width, display_height))
- pygame.display.set_caption("Racing Car")
- clock = pygame.time.Clock()
- crashed = False
- font = pygame.font.SysFont(None, 25)
- def draw_Car(car_x, car_y, car_width, car_height, wheel_width, wheel_height, color):
- pygame.draw.rect(display, color, [car_x - car_width / 2, car_y - car_height / 2, car_width, car_height], 1)
- pygame.draw.ellipse(display, color,
- [(car_x - car_width / 2) + car_width, (car_y - car_height / 2) + car_height / 2 * 1.5,
- wheel_width, wheel_height], 1)
- pygame.draw.ellipse(display, color,
- [(car_x - car_width / 2) - wheel_width, (car_y - car_height / 2) + car_height / 2 * 1.5,
- wheel_width, wheel_height], 1)
- pygame.draw.ellipse(display, color, [(car_x - car_width / 2) + car_width, (car_y - car_height / 2),
- wheel_width, wheel_height], 1)
- pygame.draw.ellipse(display, color,
- [(car_x - car_width / 2) - wheel_width, (car_y - car_height / 2),
- wheel_width, wheel_height], 1)
- def drawBlock(block_x, block_y, block_width, block_height, color):
- pygame.draw.rect(display, color, [block_x - block_width / 2, block_y - block_height / 2, block_width, block_height],
- 1)
- def gen_Block():
- global block_x, block_y
- block_x = random.randrange(block_width / 2, display_width - block_width / 2)
- block_y = -600 - block_height / 2
- def show_score(score):
- text = font.render(score, True, white)
- display.blit(text, [display_width - 100, 20])
- def check():
- if (block_x + block_width / 2) > car_x + car_width / 2 + wheel_width and (
- block_x - block_width / 2) < car_x + car_width / 2 + wheel_width:
- if block_y + block_height / 2 > car_y - car_height / 2 and block_y - block_height / 2 < car_y + car_height / 2:
- return False
- if (block_x + block_width / 2) > car_x - car_width / 2 - wheel_width and (
- block_x - block_width / 2) < car_x - car_width / 2 - wheel_width:
- if block_y + block_height / 2 > car_y - car_height / 2 and block_y - block_height / 2 < car_y + car_height / 2:
- return False
- return True
- while not crashed:
- for event in pygame.event.get():
- if (event.type == pygame.QUIT):
- crashed = True
- if (event.type == pygame.KEYDOWN):
- if event.key == pygame.K_LEFT:
- x_change = -10
- elif event.key == pygame.K_RIGHT:
- x_change = 10
- elif event.type == pygame.KEYUP:
- if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
- x_change = 0
- if car_x + car_width / 2 + wheel_width + x_change <= display_width and car_x - car_width / 2 - wheel_width + x_change >= 0:
- car_x += x_change
- block_y += block_speed
- print(car_x)
- display.fill(black)
- draw_Car(car_x, car_y, car_width, car_height, wheel_width, wheel_height, white)
- drawBlock(block_x, block_y, block_width, block_height, white)
- if not check():
- crashed = True
- pygame.quit()
- quit()
- if block_y - block_height / 2 >= display_height:
- gen_Block()
- score += 1
- show_score("Score: " + str(score))
- pygame.display.update()
- clock.tick(60)
- pygame.quit()
- quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement