overactive

Untitled

Sep 5th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.36 KB | None | 0 0
  1. import pygame
  2. import time
  3. import sys
  4. import random
  5.  
  6. WIDTH, HEIGHT = 600, 700
  7. WHITE = (255, 255, 255)
  8. BLUE = (0,100,200)
  9. PINK = (255, 0, 120)
  10. BAR = 60
  11.  
  12. ### SETTINGS  ###
  13. MIN_ENEMY_SPEED = 4
  14. MAX_ENEMY_SPEED = 6
  15. PLAYER_SPEED = 3
  16. MIN_BALL_WIDTH = 20
  17. MAX_BALL_WIDTH = 30
  18. ENEMY_COLOR = WHITE
  19. PLAYER_COLOR = PINK
  20. DIFFICULTY_LEVEL = 15
  21. MAX_LIFE = 10
  22. PLAYER_SIZE = 30
  23. MAX_PLAYER_UP_POS = 200
  24. ### SETTINGS  ###
  25.  
  26. enemies = []
  27. pygame.init()
  28. canvas = pygame.display.set_mode((WIDTH,HEIGHT))
  29. background  = pygame.Surface((WIDTH,HEIGHT))
  30. font = pygame.font.SysFont("Calibri", 30)
  31. pygame.display.set_caption('Dodger')
  32. clock = pygame.time.Clock()
  33.  
  34. def drawBorder(y):
  35.     pygame.draw.line(canvas, WHITE, (0, y), (WIDTH, y), 2)
  36.  
  37. def quitGame():
  38.     pygame.quit()
  39.     sys.exit(0)
  40.  
  41. def drawScore():
  42.     global life
  43.     global score
  44.  
  45.     life_text = font.render("{} / {} ".format(life, MAX_LIFE), 1, WHITE)
  46.     rect_life = life_text.get_rect()
  47.     rect_life.x = BAR/2
  48.     rect_life.centery = HEIGHT - BAR/2
  49.     canvas.blit(life_text, (rect_life.x, rect_life.y))
  50.  
  51.     score_text = font.render("{}".format(score), 1, WHITE)
  52.     rect_score = score_text.get_rect()
  53.     rect_score.right = WIDTH - BAR/2
  54.     rect_score.centery = HEIGHT - BAR/2
  55.     canvas.blit(score_text, (rect_score.x, rect_score.y))
  56.  
  57. class Enemy():
  58.     def __init__(self):
  59.         self.randomize()
  60.  
  61.     def randomize(self):
  62.         self.x = random.randint(0, WIDTH - 50)
  63.         self.y = random.randint(-700, -10)
  64.         self.w = random.randint(MIN_BALL_WIDTH, MAX_BALL_WIDTH)
  65.         self.speed = random.randint(MIN_ENEMY_SPEED, MAX_ENEMY_SPEED)
  66.        
  67.     def update(self, player):
  68.         global life
  69.         self.rec = pygame.Rect(self.x, self.y, self.w, self.w)
  70.         self.y += self.speed
  71.        
  72.         if self.rec.bottom > HEIGHT - BAR:
  73.             self.randomize()
  74.             self.rec = pygame.Rect(self.x, self.y, self.w, self.w)
  75.  
  76.         if self.rec.colliderect(player.rec):
  77.             life -=1
  78.  
  79.     def show(self):
  80.         pygame.draw.rect(canvas, ENEMY_COLOR, self.rec, 0)
  81.  
  82. class Player():
  83.     def __init__(self):
  84.         self.rec = pygame.Rect(0, HEIGHT - (BAR + PLAYER_SIZE), PLAYER_SIZE, PLAYER_SIZE)
  85.         self.rec.centerx = WIDTH/2
  86.         self.speed = PLAYER_SPEED
  87.         self.horizontalDir = 0
  88.         self.verticalDir = 0
  89.    
  90.     def update(self):
  91.         self.rec.x += self.speed * self.horizontalDir
  92.         self.rec.y += self.speed * self.verticalDir
  93.        
  94.         if self.rec.left <= 0:
  95.             self.rec.left = 0
  96.         elif self.rec.right >= WIDTH:
  97.             self.rec.right = WIDTH
  98.        
  99.         if self.rec.top <= HEIGHT - MAX_PLAYER_UP_POS:
  100.             self.rec.top = HEIGHT - MAX_PLAYER_UP_POS
  101.         elif self.rec.bottom >= HEIGHT - BAR:
  102.             self.rec.bottom = HEIGHT - BAR
  103.  
  104.     def show(self):
  105.         pygame.draw.rect(canvas, PLAYER_COLOR, self.rec, 0)
  106.  
  107. for i in range(DIFFICULTY_LEVEL):
  108.      enemies.append(Enemy())
  109.  
  110. player = Player()
  111. life = MAX_LIFE
  112. score = 0
  113. start = time.time()
  114.  
  115. def theGame():
  116.     global life
  117.     global score
  118.     global start
  119.  
  120.     player.horizontalDir = 0
  121.     player.verticalDir = 0
  122.     keys = pygame.key.get_pressed()
  123.     if keys[pygame.K_LEFT]:
  124.         player.horizontalDir = -1
  125.     elif keys[pygame.K_RIGHT]:
  126.         player.horizontalDir = 1
  127.     if keys[pygame.K_UP]:
  128.         player.verticalDir = -1
  129.     elif keys[pygame.K_DOWN]:
  130.         player.verticalDir = 1
  131.  
  132.     for event in pygame.event.get():
  133.         if event.type == pygame.QUIT:
  134.             quitGame()
  135.    
  136.     canvas.blit(background, (0,0))
  137.     drawBorder(HEIGHT - BAR)
  138.    
  139.     current = time.time()
  140.     score = int(current - start)
  141.     drawScore()
  142.  
  143.     for enemy in enemies:
  144.         enemy.update(player)
  145.         enemy.show()
  146.    
  147.     player.update()
  148.     player.show()
  149.  
  150.     while life <= 0:
  151.         canvas.fill((0,0,0,))
  152.  
  153.         line1 = 'The End'
  154.         line2 = 'Your score: ' + str(score)
  155.         line3 = 'press P to play again'
  156.         line4 = 'press Q to quit'
  157.  
  158.         textSet1 = font.render(line1, True, (PINK))
  159.         textSet2 = font.render(line2, True, (PINK))
  160.         textSet3 = font.render(line3, True, (PINK))
  161.         textSet4 = font.render(line4, True, (PINK))
  162.  
  163.         canvas.blit(textSet1,(20,20))
  164.         canvas.blit(textSet2,(20,80))
  165.         canvas.blit(textSet3,(20,140))
  166.         canvas.blit(textSet4,(20,200))
  167.         pygame.display.flip()
  168.        
  169.         for event in pygame.event.get():
  170.             if event.type == pygame.QUIT:
  171.                 quitGame()
  172.             if event.type == pygame.KEYDOWN:
  173.                 if event.key == pygame.K_p:
  174.                     life = MAX_LIFE
  175.                     score = 0
  176.                     start = time.time()
  177.                     player.rec.centerx = WIDTH/2
  178.                     player.rec.bottom = HEIGHT - BAR
  179.                     return
  180.                 elif event.key == pygame.K_q:
  181.                     quitGame()
  182.  
  183.     pygame.display.flip()  
  184.     clock.tick(60)
  185.  
  186. while True:
  187.     theGame()
Advertisement
Add Comment
Please, Sign In to add comment