View difference between Paste ID: dQqMjJLx and r7bSqptv
SHOW: | | - or go back to the newest paste.
1
import pygame
2
3-
from Platform import Platform
3+
from paddle import Paddle
4-
from Ball import Ball
4+
from ball import Ball
5-
from Brick import Brick
5+
from brick import Brick
6
7
# height and width of the screen
8
SCREEN_WIDTH = 1024
9
SCREEN_HEIGHT = 800
10
level = 0
11
lives = 3
12
13
# pygame settings
14
pygame.init()
15
pygame.font.init()
16
17
# font, display, clock and background objects
18
font = pygame.font.SysFont('Comic Sans MS', 24)
19
display = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
20
clock = pygame.time.Clock()
21
background_image = pygame.image.load('images/background.png')
22
23
# game levels
24
level1 = [
25
    [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
26
    [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
27
    [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
28
    [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
29
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
30
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
31
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
32
]
33
level2 = [
34
    [0, 0, 1, 2, 3, 3, 2, 1, 0, 0],
35
    [0, 1, 1, 1, 2, 2, 1, 1, 1, 0],
36
    [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
37
    [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
38
    [0, 0, 2, 0, 0, 0, 0, 2, 0, 0],
39
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
40
    [0, 2, 0, 2, 0, 0, 2, 0, 2, 0]
41
]
42
level3 = [
43
    [2, 3, 2, 2, 2, 2, 2, 2, 3, 2],
44
    [2, 1, 3, 1, 1, 1, 1, 3, 1, 2],
45
    [2, 3, 1, 3, 1, 1, 3, 1, 3, 2],
46
    [3, 2, 2, 2, 3, 3, 2, 2, 2, 3],
47
    [0, 0, 2, 2, 3, 3, 2, 2, 0, 0],
48
    [0, 0, 2, 0, 3, 3, 0, 2, 0, 0],
49
    [0, 0, 3, 0, 3, 3, 0, 3, 0, 0]
50
]
51
52
bricks = pygame.sprite.Group()
53
54
55
def add_bricks():
56
    loaded_level = None
57
    if level == 0:
58
        loaded_level = level1
59
    if level == 1:
60
        loaded_level = level2
61
    if level == 2:
62
        loaded_level = level3
63
64
    for i in range(10):
65
        for j in range(7):
66
            if loaded_level[j][i] != 0:
67
                brick = Brick(32+i*96, 32+j*48, loaded_level[j][i])
68
                bricks.add(brick)
69
70
71
add_bricks()
72
73-
platform = Platform()
73+
paddle = Paddle()
74
ball = Ball()
75
76
# main game loop
77
game_on = True
78
while game_on:
79
    for event in pygame.event.get():
80
        if event.type == pygame.KEYDOWN:
81
            if event.key == pygame.K_ESCAPE:
82
                game_on = False
83
        elif event.type == pygame.QUIT:
84
            game_on = False
85
86-
    # platform controls
86+
    # paddle controls
87
    pressed_keys = pygame.key.get_pressed()
88
    if pressed_keys[pygame.K_a]:
89-
        platform.move_platform(-1)
89+
        paddle.move_paddle(-1)
90
    if pressed_keys[pygame.K_d]:
91-
        platform.move_platform(1)
91+
        paddle.move_paddle(1)
92
93
    # checking if all bricks have been destroyed
94
    if len(bricks.sprites()) == 0:
95
        level += 1
96
        if level >= 3:
97
            break
98
        ball.reset_position()
99-
        platform.reset_position()
99+
        paddle.reset_position()
100
        add_bricks()
101
102
    # ball update
103-
    ball.update(platform, bricks)
103+
    ball.update(paddle, bricks)
104
105
    # checking if ball has touched the lower edge of the screen
106
    if ball.lost:
107
        lives -= 1
108
        if lives <= 0:
109
            break
110
        ball.reset_position()
111-
        platform.reset_position()
111+
        paddle.reset_position()
112
113-
    # updating bricks and the platform
113+
    # updating bricks and the paddle
114
    bricks.update()
115-
    platform.update()
115+
    paddle.update()
116
117
    # display background
118
    display.blit(background_image, (0, 0))
119
120
    # display bricks
121
    for brick in bricks:
122-
        display.blit(brick.image, brick.position)
122+
        display.blit(brick.image, brick.rect)
123
124
    # display the player and the ball
125-
    display.blit(platform.image, platform.position)
125+
    display.blit(paddle.image, paddle.rect)
126-
    display.blit(ball.image, ball.position)
126+
    display.blit(ball.image, ball.rect)
127
128
    # render score
129
    text = font.render(
130
        f'level: {level+1}, Lives: {lives}', False, (255, 0, 255))
131
    display.blit(text, (16, 16))
132
133
    pygame.display.flip()
134
    clock.tick(30)
135
136
pygame.quit()
137