Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from Pad import Pad
- from Ball import Ball
- from Brick import Brick
- #globalne
- SCREEN_WIDTH = 1024
- SCREEN_HEIGHT = 800
- Level = 0
- Lives = 3
- #ustawienia
- pygame.init()
- pygame.font.init()
- myfont = pygame.font.SysFont('Comic Sans MS', 24)
- screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
- clock = pygame.time.Clock()
- background = pygame.image.load('images/background.png')
- #poziomy gry
- level1 = [
- [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
- [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
- [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
- [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- ]
- level2 = [
- [0, 0, 1, 2, 3, 3, 2, 1, 0, 0],
- [0, 1, 1, 1, 2, 2, 1, 1, 1, 0],
- [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
- [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
- [0, 0, 2, 0, 0, 0, 0, 2, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 2, 0, 2, 0, 0, 2, 0, 2, 0],
- ]
- level3 = [
- [2, 3, 2, 2, 2, 2, 2, 2, 3, 2],
- [2, 1, 3, 1, 1, 1, 1, 3, 1, 2],
- [2, 3, 1, 3, 1, 1, 3, 1, 3, 2],
- [3, 2, 2, 2, 3, 3, 2, 2, 2, 3],
- [0, 0, 2, 2, 3, 3, 2, 2, 0, 0],
- [0, 0, 2, 0, 3, 3, 0, 2, 0, 0],
- [0, 0, 3, 0, 3, 3, 0, 3, 0, 0],
- ]
- bricks = pygame.sprite.Group()
- def addBricks():
- loadedLevel = None
- if Level == 0:
- loadedLevel = level1
- if Level == 1:
- loadedLevel = level2
- if Level == 2:
- loadedLevel = level3
- for i in range(10):
- for j in range(7):
- if loadedLevel[j][i] != 0:
- brick = Brick(32+i*96, 32+j*48, loadedLevel[j][i])
- bricks.add(brick)
- addBricks()
- pad = Pad()
- ball = Ball()
- #glowna petla
- running = True
- while running:
- for event in pygame.event.get():
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_ESCAPE:
- running = False
- elif event.type == pygame.QUIT:
- running = False
- #sterowanie platforma
- keys=pygame.key.get_pressed()
- if keys[pygame.K_a]:
- pad.move(-1)
- if keys[pygame.K_d]:
- pad.move(1)
- #sprawdzenie czy wszystkie klocki zostaly zniszczone
- if len(bricks.sprites()) == 0:
- Level += 1
- if Level >= 3:
- break
- ball.resetPosition()
- pad.resetPosition()
- addBricks()
- #aktualizacja kulki
- ball.update(pad, bricks)
- #sprawdzenie czy kulka dotknela dolnej krawedzi
- if ball.lost:
- Lives -= 1
- if Lives <= 0:
- break
- ball.resetPosition()
- pad.resetPosition()
- #aktualizacja klockow i platformy
- bricks.update()
- pad.update()
- #wyswietl tlo
- screen.blit(background, (0,0))
- #wyswietl klocki
- for brick in bricks:
- screen.blit(brick.surf, brick.rect)
- #wyswietl gracza i kulkę
- screen.blit(pad.surf, pad.rect)
- screen.blit(ball.surf, ball.rect)
- #wyswietlenie wyniku
- textsurface = myfont.render(f'Poziom: {Level+1}, Życia: {Lives}', False, (255, 0, 255))
- screen.blit(textsurface, (16, 16))
- pygame.display.flip()
- clock.tick(30)
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement