Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- pygame.init()
- #General Variables
- running = True
- window = pygame.display.set_mode((400,600))
- clock = pygame.time.Clock()
- #New Variables
- randx = random.randrange(0, 300)
- randy = 0
- score = 0
- pygame.display.set_caption('Race car')
- class car(pygame.sprite.Sprite):
- def __init__(self,location):
- pygame.sprite.Sprite.__init__(self)
- self.image = pygame.image.load('racecar.png').convert_alpha()
- self.x_change = 0
- self.x = 370
- self.y = 430
- car1 = car([370,430])
- #The Loop
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- #key detection
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_LEFT:
- car1.x_change = -5
- elif event.key == pygame.K_RIGHT:
- car1.x_change = 5
- if event.type == pygame.KEYUP:
- if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
- car1.x_change = 0
- #When roadblock gets to the bottom put back to the top & add to score
- if randy > 570:
- randy = -100
- randx = random.randrange(0, 300)
- score = score + 1
- print(score)
- #move the roadblock down
- randy = randy + 5
- #change the car position
- car1.x += car1.x_change
- window.fill((0, 0, 0))
- #draw the roadblock
- rect1 = pygame.draw.rect(window, (255, 255, 0),(randx, randy, 100, 30))
- #draw the car
- window.blit(car1.image,[car1.x,car1.y])
- #Make an invisible rectangle around the car for crash detection.
- rect2 = pygame.Rect(car1.x,car1.y,70,150)
- #Draw the screen
- pygame.display.update()
- #Check to see if the rectangles have crashed
- if rect2.colliderect(rect1):
- print('crash')
- running = False
- clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement