overactive

Untitled

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