Advertisement
ZEdKasat

Block Game

Feb 12th, 2022 (edited)
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.80 KB | None | 0 0
  1. import pygame
  2.  
  3. BACKGROUND_COLOR = (0,0,0)
  4. ball_color = (0, 255, 0)
  5. paddle_color = (255, 0, 0)
  6. block_color = (
  7.     (0, 0, 255),
  8.     (0, 255, 255),
  9.     (255, 0, 255),
  10.     (255, 255, 0),
  11. )
  12. ball_speed = 5
  13. paddle_speed = 5
  14.  
  15. block_length, block_height = 90, 60
  16.  
  17. pygame.init()
  18. WIDTH, HEIGHT = 900, 600
  19. window = pygame.display.set_mode((WIDTH, HEIGHT))
  20. run = True
  21. clock = pygame.time.Clock()
  22.  
  23. class Paddle(pygame.sprite.Sprite):
  24.     def __init__(self):
  25.         super().__init__()
  26.         self.image = pygame.Surface((150, 30))
  27.         self.image.fill(paddle_color)
  28.         self.rect = self.image.get_rect()
  29.         self.rect.center = (WIDTH//2, HEIGHT-30)
  30.  
  31.     def update(self):
  32.         keys = pygame.key.get_pressed()
  33.         self.dx, self.dy = 0, 0
  34.         if keys[pygame.K_LEFT]:
  35.             self.dx -= paddle_speed
  36.         if keys[pygame.K_RIGHT]:
  37.             self.dx += paddle_speed
  38.         self.rect.move_ip(self.dx, self.dy)
  39.  
  40. class Ball(pygame.sprite.Sprite):
  41.     def __init__(self):
  42.         super().__init__()
  43.         self.image = pygame.Surface((30,30))
  44.         self.image.fill(ball_color)
  45.         self.rect = self.image.get_rect()
  46.         self.rect.center = (WIDTH//2, HEIGHT//2)
  47.         self.dx, self.dy = ball_speed, -ball_speed
  48.  
  49.     def update(self):
  50.         if self.rect.top < 0:
  51.             self.dy = ball_speed
  52.         if self.rect.left < 0:
  53.             self.dx = ball_speed
  54.         if self.rect.right > WIDTH:
  55.             self.dx = -ball_speed
  56.  
  57.         self.rect.move_ip(self.dx, self.dy)
  58.  
  59. class Block(pygame.sprite.Sprite):
  60.     def __init__(self, hits, x, y):
  61.         super().__init__()
  62.         self.hits = hits
  63.         self.image = pygame.Surface((block_length, block_height))
  64.         self.image.fill(block_color[hits-1])
  65.         self.rect = self.image.get_rect()
  66.         self.rect.center = (x, y)
  67.    
  68.     def update(self):
  69.         if self.hits <= 0:
  70.             self.kill()
  71.         self.hits -= 1
  72.         self.image.fill(block_color[self.hits-1])
  73.  
  74. player = Paddle()
  75. ball = Ball()
  76.  
  77. elements_group = pygame.sprite.Group()
  78. blocks_group = pygame.sprite.Group()
  79.  
  80. elements_group.add(player)
  81. elements_group.add(ball)
  82.  
  83. for y in range(10, HEIGHT//5, block_height + 5):
  84.     for x in range(10, WIDTH, block_length + 5):
  85.         b = Block(1, x, y)
  86.         blocks_group.add(b)
  87.  
  88. while run:
  89.     window.fill(BACKGROUND_COLOR)
  90.     clock.tick(90)
  91.  
  92.     for event in pygame.event.get():
  93.         if event.type == pygame.QUIT:
  94.             run = False
  95.    
  96.     if player.rect.colliderect(ball.rect):
  97.         ball.dy = -ball_speed
  98.  
  99.     b = pygame.sprite.spritecollideany(ball, blocks_group)
  100.     if b:
  101.         ball.dy *= -1
  102.         b.update()
  103.  
  104.     elements_group.update()
  105.     elements_group.draw(window)
  106.     blocks_group.draw(window)
  107.     pygame.display.update()
  108. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement