Advertisement
giganciprogramowania

główny projekt

Jan 20th, 2022
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.17 KB | None | 0 0
  1. import pygame
  2.  
  3. from Pad import Pad
  4. from Ball import Ball
  5. from Brick import Brick
  6.  
  7. #globalne
  8. SCREEN_WIDTH = 1024
  9. SCREEN_HEIGHT = 800
  10. Level = 0
  11. Lives = 3
  12.  
  13. #ustawienia
  14. pygame.init()
  15. pygame.font.init()
  16. myfont = pygame.font.SysFont('Comic Sans MS', 24)
  17. screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
  18. clock = pygame.time.Clock()
  19. background = pygame.image.load('images/background.png')
  20.  
  21. #poziomy gry
  22. level1 = [
  23.     [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
  24.     [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
  25.     [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
  26.     [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
  27.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  28.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  29.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  30. ]
  31. level2 = [
  32.     [0, 0, 1, 2, 3, 3, 2, 1, 0, 0],
  33.     [0, 1, 1, 1, 2, 2, 1, 1, 1, 0],
  34.     [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
  35.     [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
  36.     [0, 0, 2, 0, 0, 0, 0, 2, 0, 0],
  37.     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  38.     [0, 2, 0, 2, 0, 0, 2, 0, 2, 0],
  39. ]
  40. level3 = [
  41.     [2, 3, 2, 2, 2, 2, 2, 2, 3, 2],
  42.     [2, 1, 3, 1, 1, 1, 1, 3, 1, 2],
  43.     [2, 3, 1, 3, 1, 1, 3, 1, 3, 2],
  44.     [3, 2, 2, 2, 3, 3, 2, 2, 2, 3],
  45.     [0, 0, 2, 2, 3, 3, 2, 2, 0, 0],
  46.     [0, 0, 2, 0, 3, 3, 0, 2, 0, 0],
  47.     [0, 0, 3, 0, 3, 3, 0, 3, 0, 0],
  48. ]
  49.  
  50. bricks = pygame.sprite.Group()
  51. def addBricks():
  52.     loadedLevel = None
  53.     if Level == 0:
  54.         loadedLevel = level1
  55.     if Level == 1:
  56.         loadedLevel = level2
  57.     if Level == 2:
  58.         loadedLevel = level3
  59.  
  60.     for i in range(10):
  61.         for j in range(7):
  62.             if loadedLevel[j][i] != 0:
  63.                 brick = Brick(32+i*96, 32+j*48, loadedLevel[j][i])
  64.                 bricks.add(brick)
  65. addBricks()
  66.  
  67. pad = Pad()
  68. ball = Ball()
  69.  
  70. #glowna petla
  71. running = True
  72. while running:
  73.     for event in pygame.event.get():
  74.         if event.type == pygame.KEYDOWN:
  75.             if event.key == pygame.K_ESCAPE:
  76.                 running = False
  77.         elif event.type == pygame.QUIT:
  78.             running = False
  79.    
  80.     #sterowanie platforma
  81.     keys=pygame.key.get_pressed()
  82.     if keys[pygame.K_a]:
  83.         pad.move(-1)
  84.     if keys[pygame.K_d]:
  85.         pad.move(1)      
  86.    
  87.     #sprawdzenie czy wszystkie klocki zostaly zniszczone
  88.     if len(bricks.sprites()) == 0:
  89.         Level += 1
  90.         if Level >= 3:
  91.             break
  92.         ball.resetPosition()
  93.         pad.resetPosition()
  94.         addBricks()
  95.    
  96.  
  97.     #aktualizacja kulki
  98.     ball.update(pad, bricks)
  99.  
  100.     #sprawdzenie czy kulka dotknela dolnej krawedzi
  101.     if ball.lost:
  102.         Lives -= 1
  103.         if Lives <= 0:
  104.             break
  105.         ball.resetPosition()
  106.         pad.resetPosition()
  107.  
  108.     #aktualizacja klockow i platformy
  109.     bricks.update()
  110.     pad.update()
  111.    
  112.     #wyswietl tlo
  113.     screen.blit(background, (0,0))
  114.  
  115.     #wyswietl klocki
  116.     for brick in bricks:
  117.         screen.blit(brick.surf, brick.rect)
  118.    
  119.  
  120.     #wyswietl gracza i kulkę
  121.     screen.blit(pad.surf, pad.rect)
  122.     screen.blit(ball.surf, ball.rect)
  123.  
  124.     #wyswietlenie wyniku
  125.     textsurface = myfont.render(f'Poziom: {Level+1}, Życia: {Lives}', False, (255, 0, 255))
  126.     screen.blit(textsurface, (16, 16))
  127.  
  128.     pygame.display.flip()
  129.     clock.tick(30)
  130.  
  131. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement