Facebook
From Morose Ostrich, 2 Months ago, written in Python.
Embed
Download Paste or View Raw
Hits: 27
  1. import pygame
  2. import random
  3. from os import path
  4.  
  5. pygame.init()
  6. clock = pygame.time.Clock()
  7.  
  8. width = 600
  9. height = 800
  10. FPS = 60
  11.  
  12. screen = pygame.display.set_mode((width, height))
  13. pygame.display.set_caption('Car Race')
  14. img_dir = path.join(path.dirname(__file__), 'Images')
  15. player_sprite = pygame.image.load(path.join(img_dir, 'Car.png')).convert_alpha()
  16. background = pygame.image.load(path.join(img_dir, 'road1.png')).convert()
  17. background_rect = background.get_rect()
  18.  
  19. sprites = pygame.sprite.Group()
  20. enemies = pygame.sprite.Group()
  21.  
  22. # enemy
  23. # enemy_sprite = pygame.image.load(path.join(img_dir, 'Audi.png')).convert_alpha()
  24.  
  25.  
  26. # defining the regions
  27. centerR1 = (185 / 2)
  28. centerR2 = centerR1 + 185
  29. centerR3 = centerR2 + 185
  30.  
  31. y = 0
  32.  
  33.  
  34. class Player(pygame.sprite.Sprite):
  35.     def __init__(self):
  36.         pygame.sprite.Sprite.__init__(self)
  37.         self.image = pygame.transform.scale(player_sprite, (71, 168))
  38.         self.rect = self.image.get_rect()
  39.         self.rect.x = 265
  40.         self.rect.y = 540
  41.  
  42.         self.speedx = 0
  43.  
  44.     def update(self):
  45.         self.speedx = 0
  46.         keystate = pygame.key.get_pressed()
  47.         if keystate[pygame.K_a]:
  48.             self.speedx = -5
  49.         if keystate[pygame.K_d]:
  50.             self.speedx = 5
  51.  
  52.         self.rect.x += self.speedx
  53.  
  54.         if self.rect.x <= 22:
  55.             self.rect.x = 22
  56.         elif self.rect.x >= 600 - (71 + 22):
  57.             self.rect.x = 600 - (71 + 22)
  58.  
  59.  
  60. class Enemies(pygame.sprite.Sprite):
  61.     def __init__(self, car_graphic):
  62.         pygame.sprite.Sprite.__init__(self)
  63.         # self.image = pygame.transform.scale(enemy_sprite, (197, 197))
  64.         self.original_image = car_graphic
  65.         self.image = self.original_image
  66.         self.rect = self.image.get_rect()
  67.         self.rect.x = random.randrange(22, width - 22)
  68.         self.rect.y = -500
  69.         self.speedy = 0
  70.  
  71.     def update(self):
  72.         self.speedy = 14
  73.         self.rect.y += self.speedy
  74.  
  75.         if self.rect.y > height:
  76.             self.rect.y = 0 - height
  77.             self.rect.x = random.randrange(0, width)
  78.  
  79.  
  80. def loadEnemy():
  81.     car_names = ['Ambulance.png', 'Audi.png', 'Black_viper.png', 'Mini_truck.png', 'Mini_van.png', 'Police.png',
  82.                  'taxi.png', 'truck.png']
  83.     car_files = []
  84.     for image in car_names:
  85.         car_files.append(pygame.image.load(path.join(img_dir, image)).convert_alpha())
  86.  
  87.     return car_files
  88.  
  89.  
  90. def createEnemyInstance():
  91.     enemy = Enemies(random.choice(enemy_graphic))
  92.     sprites.add(enemy)
  93.     enemies.add(enemy)
  94.  
  95.  
  96. enemy_graphic = loadEnemy()
  97. for i in range(5):
  98.     createEnemyInstance()
  99.  
  100. #  player
  101. player_car = Player()
  102. sprites.add(player_car)
  103.  
  104. # enemy
  105. enemy = Enemies(enemy_graphic)
  106. enemies.add(enemy)
  107.  
  108. running = True
  109. while running:
  110.     clock.tick(FPS)
  111.     for event in pygame.event.get():
  112.         if event.type == pygame.QUIT:
  113.             running = False
  114.  
  115.     sprites.update()
  116.     enemies.update()
  117.  
  118.     # Continuous scrolling background
  119.     rel_y = y % background.get_rect().height
  120.     screen.blit(background, (0, rel_y - background.get_rect().height))
  121.     if rel_y < height:
  122.         screen.blit(background, (0, rel_y))
  123.     y += 14
  124.  
  125.     sprites.draw(screen)
  126.     enemies.draw(screen)
  127.     pygame.display.update()
  128.  
  129. pygame.quit()
  130.