-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrickSpritesMarch4_2.py
More file actions
193 lines (147 loc) · 4.57 KB
/
BrickSpritesMarch4_2.py
File metadata and controls
193 lines (147 loc) · 4.57 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
import math
import pygame
import random
red = (255,0,0)
green = (0,255,63)
purple = (238,130,238)
white = (0,0,0)
# collision between ball and wall different between ball and bottom wall, ball and brick
class Nonmoving_brick(pygame.sprite.Sprite):
def __init__(self, color, xpos, ypos):
super(Nonmoving_brick, self).__init__()
self.width = 80
self.height = 30
width = self.width
height = self.height
self.image = pygame.Surface([width, height])
self.image.fill((red))
self.rect = self.image.get_rect()
self.screenheight = pygame.display.get_surface().get_height()
self.screenwidth = pygame.display.get_surface().get_width()
self.rect.x = xpos
self.rect.y = ypos
# self.image = pygame.Surface([width, height])
# self.image.fill(red)
# self.rect = self.image.get_rect()
# #def draw_bricks(self):
# rect = pygame.Rect((80,0),(80,30))
# pygame.draw.rect(self.image, red, rect, 2)
class Ball(pygame.sprite.Sprite):
def __init__(self,color):
super(Ball, self).__init__()
self.width = 20
self.height = 20
width = self.width
height = self.height
self.image = pygame.Surface([width, height])
self.image.fill(purple)
self.image.set_colorkey(purple)
self.rect = self.image.get_rect()
pygame.draw.ellipse(self.image, color, [0,0,width, height])
self.rect = self.image.get_rect()
self.screenheight = pygame.display.get_surface().get_height()
self.screenwidth = pygame.display.get_surface().get_width()
# self.x = 320
# self.y = 400
self.reset()
def reset(self): # initial random conditions
self.x = random.randrange(10,630)
self.y = 50.0
self.direction = random.randrange(-45,45)
def bounce(self):
self.direction = (180-self.direction)%360
def update(self):
width = 640
height = 480
direction_rad = math.radians(self.direction)
self.x += math.sin(direction_rad)
self.y += math.cos(direction_rad)
if self.y < 0: # this is the top!
self.y = 0
self.direction = (self.direction + 90)%360.0
if self.y > self.screenheight: # this is the bottom!
self.direction = (self.direction - 90)%360.0
self.y = self.screenheight - self.width
self.rect.x = self.x
self.rect.y = self.y
if self.x < self.width:
self.direction = (360 - self.direction)%360.0
if self.x > width - self.width:
self.direction = (360 - self.direction)%360.0
class PlayerBrick(pygame.sprite.Sprite):
def __init__(self):
super(PlayerBrick, self).__init__()
self.width = 50 # width of bottom brick
self.height = 20 # height of bottom brick
self.image = pygame.Surface([self.width, self.height])
self.image.fill((green))
# pygame.key.get_pressed() = key # this is possibly completet bullshit
self.rect = self.image.get_rect()
self.screenheight = pygame.display.get_surface().get_height()
self.screenwidth = pygame.display.get_surface().get_width()
self.rect.x = 320
self.rect.y = 400
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.rect.x -= 20
if keys[pygame.K_RIGHT]:
self.rect.x += 20
if self.rect.x > self.screenwidth - self.width:
self.rect.x = self.screenwidth - self.width
if self.rect.x < 0:
self.rect.x = 0
pygame.init()
screen = pygame.display.set_mode([640,480])
pygame.display.set_caption('Brick Breaker')
font = pygame.font.Font(None, 36)
Background = pygame.Surface(screen.get_size())
ball = Ball(red)
balls = pygame.sprite.Group()
balls.add(ball)
player = PlayerBrick()
players = pygame.sprite.Group()
players.add(player)
moving_things = pygame.sprite.Group()
moving_things.add(ball)
moving_things.add(player)
# nonmoving_bricks = Nonmoving_brick(red, 60, 40)
BRICKS = []
for i in range(0,5):
non_moving_brick = Nonmoving_brick(red, 90 * i, 60 )
BRICKS.append(non_moving_brick)
to_be_broken = pygame.sprite.Group()
# to_be_broken.add(nonmoving_bricks)
to_be_broken.add(BRICKS)
score = 0
clock = pygame.time.Clock()
done = False
exit_program = False
while exit_program != True:
clock.tick(100)
pygame.display.update()
screen.blit(Background, (0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit_program = True
if score == 10:
done = True
if not done:
player.update()
ball.update()
if done:
text = font.render("Game Over", 1, (200,200,200))
textpos = text.get_rect(centerx = background.get_width()/2)
textpos.top = 50
screen.blit(text, textpos)
if pygame.sprite.spritecollide(player, balls, False):
ball.bounce()
score +=1
if pygame.sprite.spritecollide(ball,to_be_broken,True):
ball.bounce()
score+=1
screen.fill(purple)
moving_things.draw(screen)
to_be_broken.draw(screen)
pygame.display.flip()
pygame.quit()