1616"""
1717
1818import pygame
19+ import random
1920
2021
2122# Define some colors
@@ -40,11 +41,15 @@ def __init__ (self, screen, radius,x,y):
4041 self ._radius = radius
4142 self ._xLoc = x
4243 self ._yLoc = y
43- self .__xVel = 5
44- self .__yVel = - 3
44+ self .__xVel = 7
45+ self .__yVel = 2
4546 w , h = pygame .display .get_surface ().get_size ()
4647 self .__width = w
4748 self .__height = h
49+ def getXVel (self ):
50+ return self .__xVel
51+ def getYVel (self ):
52+ return self .__yVel
4853 def draw (self ):
4954 """
5055 draws the ball onto screen.
@@ -57,20 +62,24 @@ def update(self, paddle, brickwall):
5762 """
5863 self ._xLoc += self .__xVel
5964 self ._yLoc += self .__yVel
60- if self ._xLoc == self ._radius :
65+ #left screen wall bounce
66+ if self ._xLoc <= self ._radius :
6167 self .__xVel *= - 1
68+ #right screen wall bounce
6269 elif self ._xLoc >= self .__width - self ._radius :
6370 self .__xVel *= - 1
64- if self ._yLoc == self ._radius :
71+ #top wall bounce
72+ if self ._yLoc <= self ._radius :
6573 self .__yVel *= - 1
66- elif self ._yLoc >= self .__height - self ._radius :
74+ #bottom drop out
75+ elif self ._yLoc >= self .__width - self ._radius :
6776 return True
6877
6978 # for bouncing off the bricks.
7079 if brickwall .collide (self ):
7180 self .__yVel *= - 1
7281
73- # collision deection between ball and paddle
82+ # collision detection between ball and paddle.
7483 paddleX = paddle ._xLoc
7584 paddleY = paddle ._yLoc
7685 paddleW = paddle ._width
@@ -146,28 +155,31 @@ def remove(self, group):
146155 self .__isInGroup = False
147156 def alive (self ):
148157 """
149- returns true when this brick is belong to the brick wall.
158+ returns true when this brick belongs to the brick wall.
150159 otherwise false
151160 """
152161 return self .__isInGroup
153162
154163 def collide (self , ball ):
155164 """
156- collision deection between ball and this brick
165+ collision detection between ball and this brick
157166 """
158167 brickX = self ._xLoc
159168 brickY = self ._yLoc
160169 brickW = self ._width
161170 brickH = self ._height
162171 ballX = ball ._xLoc
163172 ballY = ball ._yLoc
164- radius = ball ._radius
173+ ballXVel = ball .getXVel ()
174+ ballYVel = ball .getYVel ()
165175
166- if ((ballX + radius ) >= brickX and ballX <= (brickX + brickW )) \
167- and ((ballY + radius ) >= brickY and ballY <= (brickY + brickH )):
168- return True
169176
170- return False
177+ if ((ballX + ball ._radius ) >= brickX and (ballX + ball ._radius ) <= (brickX + brickW )) \
178+ and ((ballY - ball ._radius ) >= brickY and (ballY - ball ._radius )\
179+ <= (brickY + brickH )):
180+ return True
181+ else :
182+ return False
171183
172184
173185"""
@@ -237,7 +249,7 @@ def collide (self, ball):
237249 return False
238250
239251# The game objects ball, paddle and brick wall
240- ball = Ball (screen ,25 ,350 ,250 )
252+ ball = Ball (screen ,25 , random . randint ( 1 , 700 ) ,250 )
241253paddle = Paddle (screen ,100 ,20 ,250 ,450 )
242254brickWall = BrickWall (screen ,25 ,25 ,150 ,50 )
243255
@@ -259,13 +271,13 @@ def collide (self, ball):
259271 # if you want to use this module.
260272
261273# message for game over
262- mgGameOver = pygame .font .SysFont ('Comic Sans MS' , 60 )
274+ mgGameOver = pygame .font .SysFont ('Comic Sans MS' , 40 )
263275
264276# message for winning the game.
265- mgWin = pygame .font .SysFont ('Comic Sans MS' , 60 )
277+ mgWin = pygame .font .SysFont ('Comic Sans MS' , 40 )
266278
267279# message for score
268- mgScore = pygame .font .SysFont ('Comic Sans MS' , 60 )
280+ mgScore = pygame .font .SysFont ('Comic Sans MS' , 40 )
269281
270282textsurfaceGameOver = mgGameOver .render ('Game Over!' , False , (0 , 0 , 0 ))
271283textsurfaceWin = mgWin .render ("You win!" ,False ,(0 ,0 ,0 ))
@@ -339,4 +351,4 @@ def collide (self, ball):
339351 clock .tick (60 )
340352
341353# Close the window and quit.
342- pygame .quit ()
354+ pygame .quit ()
0 commit comments