-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
271 lines (190 loc) · 7.48 KB
/
main.py
File metadata and controls
271 lines (190 loc) · 7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import pygame
import os
import random
pygame.font.init()
pygame.mixer.init()
WIDTH = 900
HEIGHT = 500
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
FPS = 60
SCORE = 0
SCORE_INCREMENT = 10
SLIDER_VEL = 7
SLIDER_LENGTH = 90
SLIDER_WIDTH = 5
BLOCK_WIDTH = 50
BLOCK_HEIGHT = 65
BALL_X_VEL = 5
BALL_Y_VEL = 5
BALL_WIDTH = 10
BALL_HEIGHT = 10
BACKGROUND = pygame.transform.scale(pygame.image.load(os.path.join('Assets','background.png')), (WIDTH, HEIGHT))
SLIDER_IMAGE = pygame.image.load(os.path.join('Assets', 'slider.png'))
SLIDER = pygame.transform.rotate(pygame.transform.scale(SLIDER_IMAGE, (SLIDER_WIDTH, SLIDER_LENGTH)), 90)
BLOCK_IMAGE = pygame.image.load(os.path.join('Assets', 'block.png'))
BLOCK = pygame.transform.rotate(pygame.transform.scale(BLOCK_IMAGE, (BLOCK_WIDTH, BLOCK_HEIGHT)), 90)
BALL_IMAGE = pygame.image.load(os.path.join('Assets', 'ball.png'))
BALL = pygame.transform.rotate(pygame.transform.scale(BALL_IMAGE, (BALL_WIDTH, BALL_HEIGHT)), 90)
font = pygame.font.Font('freesansbold.ttf', 64)
text_welcome = font.render('Press Space Bar to Begin', True, WHITE, BLACK)
text_welcome_rect = text_welcome.get_rect()
text_welcome_rect.center = (WIDTH // 2, HEIGHT // 2)
text_game_over = font.render('Game Over', True, WHITE, BLACK)
text_game_over_rect = text_game_over.get_rect()
text_game_over_rect.center = (WIDTH // 2, HEIGHT // 2)
text_winner = font.render('You Win!', True, WHITE, BLACK)
text_winner_rect = text_winner.get_rect()
text_winner_rect.center = (WIDTH // 2, HEIGHT // 2)
font_score = pygame.font.Font('freesansbold.ttf', 15)
text_score = font_score.render(f'Score: {SCORE}', True, WHITE, BLACK)
text_score_rect = text_score.get_rect()
text_score_rect.center = (WIDTH - 45, 20)
class block:
def __init__(self, x, y):
self.x = x
self.y = y
self.width = BLOCK_WIDTH
self.height = BLOCK_HEIGHT
self.isDestroyed = False
self.hitbox = (self.x, self.y, BLOCK_HEIGHT, BLOCK_WIDTH)
self.blocks = []
def draw(self, WINDOW):
if not self.isDestroyed: #only print when object has not been hit
WINDOW.blit(BLOCK, (self.x, self.y))
pygame.draw.rect(WINDOW, (255, 0, 0), self.hitbox, 2)
def append(self, elem):
self.blocks.append(elem)
def create(self):
y = self.y
for i in range(3):
x = self.x
for j in range(7):
self.blocks.append(block(x, y))
x = x + BLOCK_WIDTH + BLOCK_HEIGHT
y = y + BLOCK_WIDTH + BLOCK_HEIGHT
def update(self, ball): #Working vertically
if ball.y < self.y + BLOCK_HEIGHT and ball.y > self.y:
if ball.x > self.x and ball.x < self.x + BLOCK_WIDTH:
self.isDestroyed = True
ball.y_vel *= -1
class slider:
def __init__(self):
self.x = (WIDTH // 2) - SLIDER_LENGTH // 2
self.y = 480
self.vel = SLIDER_VEL
self.length = SLIDER_LENGTH
self.width = SLIDER_WIDTH
self.left_side = self.x - (self.length // 2)
self.right_side = self.x + self.length // 2
self.top_side = self.y + self.width
self.bottom_side = self.y - self.width
self.hitbox = (self.x, self.y, SLIDER_LENGTH, SLIDER_WIDTH)
self.waiting = True
def draw(self, WINDOW):
WINDOW.blit(SLIDER, (self.x, self.y))
pygame.draw.rect(WINDOW, (255, 0, 0), self.hitbox, 2)
def move(self, keys_pressed):
if self.waiting == False:
if keys_pressed[pygame.K_LEFT] and self.x - self.vel > 0: # Moves Left
self.x -= self.vel
if keys_pressed[pygame.K_RIGHT] and self.x + SLIDER_LENGTH + self.vel < WIDTH: # Moves Right
self.x += self.vel
self.hitbox = (self.x, self.y, SLIDER_LENGTH, SLIDER_WIDTH)
def get_left(self):
return self.x - (self.length // 2) + 40
def get_right(self):
return self.x + (self.length // 2) + 40
class ball:
def __init__(self):
self.x = WIDTH // 2 - 5 # - 5 only for appearance
self.y = 475
self.width = BALL_WIDTH
self.height = BALL_HEIGHT
self.x_vel = BALL_X_VEL
self.y_vel = BALL_Y_VEL
self.outOfBounds = False
self.waiting = True
self.top = self.y
self.bottom = self.top + BALL_HEIGHT
self.left = self.x
self.right = self.x + BALL_WIDTH
self.hitbox = (self.x, self.y, BALL_HEIGHT, BALL_WIDTH)
self.lost = False
def draw(self, WINDOW):
WINDOW.blit(BALL, (self.x, self.y))
pygame.draw.rect(WINDOW, (255, 0, 0), self.hitbox, 2)
def move(self):
if not self.waiting:
self.x = self.x - self.x_vel
self.y = self.y - self.y_vel
if self.x + self.x_vel + self.width < 0 or self.x + self.x_vel + self.width > WIDTH: # Ball bounces off both left and right sides of screen
self.x_vel *= -1
if self.x + self.width + self.x_vel > WIDTH: # Move away from wall to avoid being stuck
self.x -= 10
if self.x + self.width + self.x_vel < 0:
self.x += 10
if self.y + self.y_vel < 0: # Ball bounces off top of screen
self.y_vel *= -1
self.y += 10
if self.y + self.y_vel > HEIGHT:
self.lost = True
self.hitbox = (self.x, self.y, BALL_HEIGHT, BALL_WIDTH)
def stopWaiting(self, keys_pressed):
if keys_pressed[pygame.K_SPACE]:
self.waiting = False
def ball_bounced(ball, slider):
if ball.y == slider.top_side:
if ball.x > slider.get_left() and ball.x < slider.get_right():
ball.y_vel *= -1
def draw_window(player, ball_oop, blocks, welcome_message, winner_message):
WINDOW.blit(BACKGROUND, (0, 0))
player.draw(WINDOW)
ball_oop.draw(WINDOW)
for i in blocks.blocks:
i.draw(WINDOW)
if welcome_message == True:
WINDOW.blit(text_welcome, text_welcome_rect)
if ball_oop.lost == True:
WINDOW.blit(text_game_over, text_game_over_rect)
if winner_message == True:
WINDOW.blit(text_winner, text_winner_rect)
text_score = font_score.render(f'Score: {SCORE}', True, WHITE, BLACK) # Updates values
WINDOW.blit(text_score, text_score_rect)
pygame.display.update()
def main():
player = slider()
ball_oop = ball()
blocks = block(70, 20)
blocks.create()
clock = pygame.time.Clock()
run = True
welcome_message = True
winner_message = False
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys_pressed = pygame.key.get_pressed()
if keys_pressed[pygame.K_SPACE]:
ball_oop.waiting = False
player.waiting = False
welcome_message = False
ball_oop.move()
player.move(keys_pressed)
ball_bounced(ball_oop, player)
draw_window(player, ball_oop, blocks, welcome_message, winner_message)
for i in blocks.blocks:
i.update(ball_oop)
if i.isDestroyed == True:
global SCORE
blocks.blocks.remove(i)
SCORE += SCORE_INCREMENT
if len(blocks.blocks) == 0:
winner_message = True
if __name__ == "__main__":
main()