Advertisement
jabela

Road Block Game

Dec 20th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.84 KB | None | 0 0
  1. import pygame
  2. import random
  3. pygame.init()
  4.  
  5. #General Variables
  6. running = True
  7. window = pygame.display.set_mode((400,600))
  8. clock = pygame.time.Clock()
  9.  
  10. #New Variables
  11. randx = random.randrange(0, 300)
  12. randy = 0
  13. score = 0
  14.  
  15. pygame.display.set_caption('Race car')
  16.  
  17. class car(pygame.sprite.Sprite):
  18.     def __init__(self,location):
  19.         pygame.sprite.Sprite.__init__(self)
  20.         self.image = pygame.image.load('racecar.png').convert_alpha()
  21.         self.x_change = 0
  22.         self.x = 370
  23.         self.y = 430
  24.  
  25. car1 = car([370,430])
  26.  
  27. #The Loop
  28. while running:
  29.     for event in pygame.event.get():
  30.         if event.type == pygame.QUIT:
  31.             running = False
  32.     #key detection
  33.     if event.type == pygame.KEYDOWN:
  34.             if event.key == pygame.K_LEFT:
  35.                 car1.x_change = -5
  36.             elif event.key == pygame.K_RIGHT:
  37.                 car1.x_change = 5
  38.     if event.type == pygame.KEYUP:
  39.         if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
  40.             car1.x_change = 0
  41.     #When roadblock gets to the bottom put back to the top & add to score
  42.     if randy > 570:
  43.         randy = -100
  44.         randx = random.randrange(0, 300)
  45.         score = score + 1
  46.         print(score)
  47.     #move the roadblock down
  48.     randy = randy + 5
  49.     #change the car position
  50.     car1.x += car1.x_change
  51.     window.fill((0, 0, 0))
  52.     #draw the roadblock
  53.     rect1 = pygame.draw.rect(window, (255, 255, 0),(randx, randy, 100, 30))
  54.     #draw the car
  55.     window.blit(car1.image,[car1.x,car1.y])
  56.     #Make an invisible rectangle around the car for crash detection.
  57.     rect2 = pygame.Rect(car1.x,car1.y,70,150)
  58.     #Draw the screen
  59.     pygame.display.update()
  60.     #Check to see if the rectangles have crashed
  61.     if rect2.colliderect(rect1):
  62.         print('crash')
  63.         running = False
  64.     clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement