overactive

Untitled

Aug 16th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.00 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.  
  11. MIN_ENEMY_SPEED = 4
  12. MAX_ENEMY_SPEED = 6
  13. PLAYER_SPEED = 3
  14. MIN_BALL_WIDTH = 20
  15. MAX_BALL_WIDTH = 30
  16. ENEMY_COLOR = WHITE
  17. PLAYER_COLOR = PINK
  18. DIFFICULTY_LEVEL = 10
  19. MAX_LIFE = 100
  20.  
  21. obstacles = []
  22. canvas = pygame.display.set_mode((WIDTH,HEIGHT))
  23. background  = pygame.Surface((WIDTH,HEIGHT))
  24. pygame.display.set_caption('Budger')
  25. clock = pygame.time.Clock()
  26. pygame.init()
  27.  
  28. def drawBorder(y):
  29.     pygame.draw.line(canvas, WHITE, (0, y), (WIDTH, y), 2)
  30.  
  31. def quitGame():
  32.     pygame.quit()
  33.     sys.exit(0)
  34.  
  35. def randomizeEnemy():
  36.     x = random.randint(0, WIDTH - 50)
  37.     y = random.randint(-700, -10)
  38.     w = random.randint(MIN_BALL_WIDTH, MAX_BALL_WIDTH)
  39.     enemySpeed = random.randint(MIN_ENEMY_SPEED, MAX_ENEMY_SPEED)
  40.     return x, y, w, enemySpeed
  41.  
  42. def drawScore():
  43.     global life
  44.     global score
  45.  
  46.     font = pygame.font.SysFont("Calibri", 30)
  47.     life_text = font.render("{} / {} ".format(life, MAX_LIFE), 1, WHITE)
  48.     rect_life = life_text.get_rect()
  49.     rect_life.x = 30
  50.     rect_life.centery = HEIGHT - 30
  51.     canvas.blit(life_text, (rect_life.x, rect_life.y))
  52.  
  53.     score_text = font.render("{}".format(score), 1, WHITE)
  54.     rect_score = score_text.get_rect()
  55.     rect_score.right = WIDTH - 30
  56.     rect_score.centery = HEIGHT - 30
  57.     canvas.blit(score_text, (rect_score.x, rect_score.y))
  58.  
  59. class Enemy():
  60.     def __init__(self, x, y, w, enemySpeed):
  61.         self.x = x
  62.         self.y = y
  63.         self.w = w
  64.         self.speed = enemySpeed
  65.  
  66.     def create(self):
  67.         self.rec = pygame.Rect(self.x, self.y, self.w, self.w)
  68.        
  69.     def update(self, player):
  70.         global life
  71.         self.create()
  72.         self.y += self.speed
  73.        
  74.         if self.rec.bottom > HEIGHT - 60:
  75.             self.x, self.y, self.w, self.speed = randomizeEnemy()
  76.             self.create()
  77.  
  78.         if self.rec.colliderect(player.rec):
  79.             life -=1
  80.             print(life)
  81.  
  82.     def show(self):
  83.         pygame.draw.rect(canvas, ENEMY_COLOR, self.rec, 0)
  84.  
  85. class Player():
  86.     def __init__(self, x):
  87.         self.rec = pygame.Rect(0, HEIGHT - 90, 30, 30)
  88.         self.rec.centerx = x
  89.         self.speed = PLAYER_SPEED
  90.         self.direction = 0
  91.    
  92.     def update(self):
  93.         self.rec.x += self.speed * self.direction
  94.         if self.rec.left <= 0:
  95.             self.rec.left = 0
  96.         elif self.rec.right >= WIDTH:
  97.             self.rec.right = WIDTH
  98.  
  99.     def show(self):
  100.         pygame.draw.rect(canvas, PLAYER_COLOR, self.rec, 0)
  101.  
  102. for i in range(DIFFICULTY_LEVEL):
  103.     x, y, w, speed = randomizeEnemy()
  104.     obstacles.append(Enemy(x, y, w, speed))
  105.  
  106. player = Player(WIDTH/2)
  107.  
  108. life = 100
  109. score = 0
  110. start = time.time()
  111. while True:
  112.    
  113.     player.direction = 0
  114.     keys = pygame.key.get_pressed()
  115.     if keys[pygame.K_LEFT]:
  116.         player.direction = -1
  117.     elif keys[pygame.K_RIGHT]:
  118.         player.direction = 1
  119.  
  120.     for event in pygame.event.get():
  121.         if event.type == pygame.QUIT:
  122.             quitGame()
  123.  
  124.     canvas.blit(background, (0,0))
  125.     drawBorder(HEIGHT - 60)
  126.     drawScore()
  127.  
  128.     for enemy in obstacles:
  129.         enemy.update(player)
  130.         enemy.show()
  131.     player.update()
  132.     player.show()
  133.     pygame.display.flip()  
  134.     clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment