Advertisement
jabela

10 Super Car

Dec 20th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 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. crashed = False
  9. clock = pygame.time.Clock()
  10. randx = random.randrange(0, 300)
  11. randy = 0
  12. marky = 600
  13. score = 0
  14. hiscore = 0
  15. speed = 5
  16.  
  17. pygame.display.set_caption('Race car')
  18.  
  19. class car(pygame.sprite.Sprite):
  20.     def __init__(self,location):
  21.         pygame.sprite.Sprite.__init__(self)
  22.         self.image = pygame.image.load('racecar.png').convert_alpha()
  23.         self.x_change = 0
  24.         self.x = 200
  25.         self.y = 430
  26.  
  27. #Draws the road markings
  28. def road_markings(marky):
  29.     for i in reversed(range(0, 1800,120)):
  30.         pygame.draw.rect(window, (255, 255, 255),(190, marky-i, 20, 100))
  31.     return
  32.  
  33. #Waits for a key press
  34. def wait():
  35.     while (pygame.event.wait().type != pygame.KEYDOWN): pass
  36.     return
  37.  
  38. #Prints fonts on the screen
  39. def fontprint(passtext,x,y):
  40.     font = pygame.font.Font(None,32)
  41.     text1=font.render(str(passtext), 1, (0, 255, 255))
  42.     window.blit(text1, (x, y))
  43.     return
  44.  
  45. car1 = car([200,430])
  46. #Display Graphics before game starts
  47. fontprint('GET READY!',50,300)
  48. pygame.display.update()
  49. pygame.time.wait(2000)
  50.  
  51.  
  52. #The Loop
  53. while running:
  54.     for event in pygame.event.get():
  55.         if event.type == pygame.QUIT:
  56.             running = False
  57.     if event.type == pygame.KEYDOWN:
  58.             if event.key == pygame.K_LEFT:
  59.                 car1.x_change = -5
  60.             elif event.key == pygame.K_RIGHT:
  61.                 car1.x_change = 5
  62.     if event.type == pygame.KEYUP:
  63.         if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
  64.             car1.x_change = 0
  65.     if randy > 570:
  66.         randy = -100
  67.         randx = random.randrange(0, 300)
  68.         score = score + 1
  69.         speed = speed + 0.1
  70.     randy = randy + speed
  71.     car1.x += car1.x_change
  72.     window.fill((0, 0, 0))
  73.     road_markings(marky)
  74.     marky = marky + speed
  75.     if marky > 1200:
  76.         marky = 600
  77.     rect1 = pygame.draw.rect(window, (255, 255, 0),(randx, randy, 100, 30))
  78.     window.blit(car1.image,[car1.x,car1.y])
  79.     rect2 = pygame.Rect(car1.x,car1.y,70,150)
  80.     fontprint(score,10,10)
  81.     pygame.display.update()
  82.     #No slipping off to the side.
  83.     if rect2.colliderect(rect1) or car1.x<0 or car1.x>330:
  84.         window.fill((0, 0, 0))
  85.         fontprint("Game Over. You scored: "+str(score),10,200)
  86.         if score > hiscore:
  87.             hiscore = score
  88.             fontprint('You have a new high Score !!',10,250)
  89.         else:
  90.             fontprint("High Score: "+str(hiscore),10,250)
  91.  
  92.         pygame.display.update()
  93.         randx = random.randrange(0, 300)
  94.         randy = 0
  95.         marky = 600
  96.         score = 0
  97.         speed = 5
  98.         car1.x = 200
  99.         wait()
  100.     clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement