In This Video I Challenge myself to make as complete a brick breaking 'breakout' game as I can using Python and PyGame in under an hour. This style of video shows what real time troubleshooting and error debugging looks and feels like while creating complex games!
Timestamps! 00:00 Intro + Explaining the Challenge 01:34 Start Setting Up Pygame 4:30 Drawing the player, ball and board 11:00 Adding Controls for Movement 17:01 Checking for Ball Collision with Paddle and Bricks 24:15 Check the direction of collision 30:13 Add Game Over Conditions + Enhance the Controls 34:43 Adding Scoring 37:00 Collide With the Ceiling and Resetting on victory 41:00 Generate random boards every game 46:45 Improving Collision 48:40 Style Upgrade the Game 53:50 Outro
what's going on you guys welcome back to
the channel and in today's video i'm
gonna do a live coding challenge where i
recreate the classic brick breaking game
breakout in python using pygame live
under an hour
[Music]
so before i begin huge shout out to tech
with tim he has a great python tutorial
page on youtube and i saw his
minesweeper in one hour live coding
challenge video i thought it was a
really great way to showcase how writing
code on the fly and debugging errors as
they arise
really feels since a lot of tutorials on
youtube are pretty polished and edited
by the time they get to youtube it
doesn't necessarily reflect the real
experience of creating complicated
programs so good job tim thanks for the
inspo thank you for watching the video
before we dive into it please subscribe
do it join the coolest 447 people on the
internet
and i will personally deliver high
quality python tutorials to your youtube
feed weekly anyway i chose breakout
because almost everyone else has played
this game in one form or another
and because the basic form of the game
is pretty simple but there are lots of
opportunities to make it more advanced
so i thought it would be a great
challenge but also pretty doable if you
want a copy of this code yourself the
github link will be in the description a
link to tim's video be in the
description as well as a link to the
channel will be in the description so
check those out if you want enjoy the
tutorial and let me know if you have any
questions or want to see anything in a
future video in the comments below
thanks enjoy
all right let's do this it's 1 42 p.m so
we'll give ourselves until
uh 2 42 p.m to get however far we can i
think we can build a pretty good
um version of breakout in that time but
let's just see how far we get so uh to
start i'm gonna do kind of all the
things i do in every one of my long form
tutorials uh pygame.time.clock
and i'm gonna do it pretty quickly so uh
if i don't explain it as well as i
normally would check out any of the
other long form tutorials
and
i'll explain kind of why i put all these
things in the beginning
and you can kind of get your questions
answered there or just ask why i do
something in a certain order in the
comments because once i post this video
the time is obviously off
so
while run so you can get all your
questions in there so we'll do
timer.tick at our fps
and we will do
let's just do the event handling right
now for event in pygame.event.get
and whoops i did not put in
in event dot get if event dot type is
equal to pi game
dot
quit all caps
then
run equals
false okay and now i really just want to
do kind of the bare minimum
uh to get
pygame.display.flip to get the game to
where it runs and showing on the screen
and then from there
uh i think i called it clock it should
do timer um once i get it on the screen
so we'll do screen dot fill and we'll
make a few colors here real quick too so
white is all of the color
and then black and gray
just so we have a few options while we
draw the initial stuff
one two eight one two eight one two
eight okay and so let's see that should
be screen.phil we haven't made a screen
yet that would be a good thing to do
well um i'm thinking we'll make this
about the size of like a phone game
because i that at least for me that's
how i
grew up playing uh breakout was on like
a mobile device so then screen equals pi
game.display.set
mode and then all this needs is a width
and a height and i think it's square
brackets we'll see when we load it in if
we've done something horribly wrong but
i'm hoping this gives us a window
yeah that's a little too tall though
isn't it
uh looks pretty good
but
too tall how hot i made that
1200 let's just do 900 see how that
looks that's pretty good that's what i
want okay
um so i'll make this font a little bit
bigger hopefully if you want to follow
along uh it just makes it easier to see
let's go ahead and draw the player on
the screen and so we'll say player is
going to be equal to pi game.draw.rect
and then we'll put it on the screen
let's make the player black since we've
got a great background that should show
up pretty well
and i'm going to give it the rectangular
coordinates of a variable player x that
we'll have to go and define
for player y i think let's just make it
height minus like 30 so that it'll be
off the bottom of the screen let's do
height minus 20. so it'll be off the
bottom of the screen and then for width
we've got um we can play around with
this but we've got a width of 500 so 120
150 somewhere in that dimension should
be good and then for how tall to make it
let's make it like 18 or 15 just so that
it's off the bottom of the screen
and then we'll make it solid and i'm
going to make it a rounded rectangle
because those look sweet and let's just
make an initial player x value that'll
put it somewhere around the middle of
the screen 500 divided by 2 250 minus 62
put it
190 that should put the player right
around the middle of the screen and
let's just draw a ball initially that's
going to be pygame.draw.circle
we'll put that on the screen we'll make
it white and all this needs is an x and
y uh for its
radius or for its center we'll make two
new variables we'll call them ball x and
ball y and then we'll give it a radius
of ten so ball x
we can probably make just width divided
by 2
and then that would put it
right at the middle and then
ball y we want it to kind of be hovering
right above this platform so height
minus 20 plus 10 we'll make that height
minus 30. that should look pretty good
let's see if we've got a ball and a
paddle we do and that looks pretty sweet
so next thing how about we make um just
a board and this is probably not how
we'll leave it but i'm just gonna
scramble and make a board that has five
rows initially
and what i'm thinking we'll do is we'll
make
the board essentially anytime the ball
hits a spot on the board will subtract
one from that board spot and then once
you get down to zero that'll show that
you uh that'll show that you totally
eliminated
the rectangle and so the game will
basically get played until the board is
all zeros
okay so let's do that and then to get
the board actually drawing on the screen
let's go ahead and create a function
that i'll call drawboard
and i think we will want to pass in
board there we go and let's just come
above our game loop and make that
function
so define draw board
and uh let's go ahead and say here
we want to pass in the board so we'll
say
board uh it maybe isn't best practice to
use board again here but it's not gonna
hurt anything so we'll say for i in
range
and then length of board so the length
of board is going to tell us how many
rows are in the board and then we'll
save for j in range and then length of
board at i so i know we made it all
uniform 5 and i think that'll look the
best anyways but this is going to
procedurally draw the board regardless
of how many pieces are in it and then
what we'll say is pygame.draw.rect
and we'll put on the screen and i
actually want to color code the
rectangles so i'm going to make a list
called colors and i'm going to have it
look up the position of board at i
j
so essentially i want a colors
dictionary that's going to go from one
to five to say how tough the thing a
colors list that's going to say one to
five how tough the thing to break is
going to be and then we're going to draw
a different color based on that so we'll
make the colors list next but let's
think about where we want these on the
screen
if we say that every row is going to be
5 pieces wide then the x position
can just be j times 100 and then the y
position can be i
times
however thick we want to make them so 40
would probably look good so let's just
do 40 as a beginner how wide we want to
make it um
let's
just say 100 for now and then let's say
well that's going to look bad let's put
a little gap in there let's say 98 for
now and 38
um just to see what that gives us and
now let's make a list of colors here so
i think to make it
um maybe moderately rainbow we'll do
like red orange green blue
purple as we count down of course now we
got to make those
and here's where if you're
not real familiar with rgbs you might
want to read
what i say next orange
you might need to play around a little
bit with these but i don't really have
time so i hope i get close i think
orange is like red some green and no
blue
green
is obviously pretty easy r g b
blue
is gonna be zero zero
two five five
and you know what i definitely should
have put up here
started
1
42 p.m
because there's no way i'm gonna
remember that off top my head
okay so uh blue and i think what i say
purple so purple's gonna be like
some red and some
blue if by a miracle i'm done early we
can come back and tweak these colors
later but these will give us stuff to
play with but actually um if you're
seeing what i did here i said the number
of like bricks remaining before you
finished it and so colors at that index
we would want to do this
plus one
i think
so
let's see if that draws a board
two two two list index out of range
colors board i j oh right right right
minus one
let's check that out
okay that's actually pretty sweet so you
can see these top bricks are going to
take five to break and then as you go
further down they're only going to take
one to break uh i think that looks
pretty good
so let's kind of start building in how
to play the game and then we can kind of
tweak the style later if we want to so
um to start the game i'm going to set
this variable that i'm making active
equal to false and we're going to put in
some new events so if event.type is
equal to pygame dot
uh
it's it's button down but it is
keyboard key
key down there we go i knew it was in
here um
if
uh event dot key is equal to pi game dot
k space
and not active so we'll use the space
bar when the game is not active to start
the game so we'll say active equals
true
now we're going to say if event dot key
is equal to and we'll use the left and
right keys
to control where the player goes so
we'll say and active so now we're only
going to let you move the paddle if if
active but we'll say player
direction
is equal to
one so that'll be to the right and then
we'll say if event dot key is equal to
pi game dot
k underscore left and it's active then
we'll say player
direction is equal to negative one and
let's go ahead and just take this whole
section and we'll kind of modify it here
for what we want to do on release so
opposite of key down is key up we'll get
rid of the space bar
and then we'll say if the key is right
and it's active then the direction is
equal to zero and actually we won't
worry about and active because if you
release the right key it should stop you
moving or the left key
and now what we'll do so that that's
continuous is we'll say that the player
x that we made so the where the
rectangle is getting drawn plus equals
and then player direction
times player speed so we'll make a new
variable here player speed that'll
control how fast the player can go and
this is a good way to adjust how
difficult your game is so player speed
and we'll set equal to eight because i
like moving kind of fast here and let's
see that should be all we need to do
uh
player direction is not to find right to
find that up in the beginning too
player direction and when we load in the
game we'll just make it zero so you're
not moving
all right so right now i can't move the
paddle left or right but i hit the space
key and now i can and it looks pretty
good feels pretty smooth
all right so i think it's time to get
the ball moving so we'll need a few new
variables for that we'll want to say
and the ball is going to move in the x
and the y direction so we'll want ball x
direction to be equal to zero when the
game starts up and we'll say ball y
direction also equal to zero when the
game starts and then we'll say uh ball
x speed and i'm gonna do ball x speed
and ball y speed as two different
variables because
i think in normal breakout you can
actually change the x speed and y speed
by which direction you collide with the
platform and so i want to get to that
hopefully i have time in this one hour
so let's see here so all right we've got
x direction y direction
uh x speed and y speed for the ball
so what do we want to say once active
let's go ahead and say that we also want
the ball
y direction to be equal to and should we
say
we should probably say negative one is
up because of the pi game coordinate
system so we'll say ball y direction is
equal to minus one and then let's say
ball y plus equal
ball y direction
times
and we want to say ball
y speed and then we'll say ball x
plus equal ball x direction
times ball x speed and these are going
to get tweaked as we get a little more
into it but we need to also initialize
the ball going off in one direction or
another so let's say ball x direction is
going to be equal to and we'll say
random.rand
actually i think it's random.choice yep
and then we give it a little list here
and it can either be
left or to the right so i guess it would
look better as minus one or one
but no matter what you'll move in the
left direction or the right direction uh
and so far this is is gonna as soon as i
press space i'm pretty sure the ball's
gonna disappear
yep pretty rapidly
but you can see it move off in the right
direction so let's go ahead and
let's do bouncing off the x and right
walls first because i think that'll be a
little bit easier again i would put a
lot of this in little sub functions if i
wasn't racing against clock but i am so
i'm just going to do it the fastest way
i can think of so what we'll say here is
if ball x
so if it's position
plus
speed um
let's just say if ball x is less than or
equal to
less than or equal to 10
then we want uh ball x direction
times
minus one so basically if it hits the
left wall we want to flip
and then let's say
or ball x
is greater than or equal to what would
be width minus 10. so that means the
ball has gotten to the point of the left
wall and the right wall and let's see if
we can get it
bouncing left and right now so left wall
looks good right wall looks good
obviously didn't collide with the
platform yet
but i think that looks pretty good so
we'll just leave that for now because
really that's all we need to control the
left and right directional bounce
and let's start taking a look at what
should change the y direction so what
we'll want is if player
dot collide um let's do ball collide so
if the ball dot collide wrecked with the
player paddle so now
um this is going to be one way that we
want to change the y direction so we'll
say ball
y direction uh times equals minus one
and now to make it easy uh i think from
our draw board we'll want to return all
those rectangles so that we can check
and see if uh
if anything
um collided so let's return something
that we'll call squares and we'll return
uh
i'll call it board squares in the
function just so it's obvious what's
different about it
and what i would like to do inside of
this function is um every time we call
it we will make a new empty list called
board
squares
and spell it right if we can
squares as seconds i wasn't counting on
wasting and how to spell and what we'll
do is we'll do board dot squares or
underscore squares
uh dot append
and let's just add uh i'll call this guy
piece
so this is the piece and we'll append
the piece so that we can check it but
then i also want to put something else
in this append that we do um i'd like to
put in a tuple that is the ij
coordinates so in the outside world we
have the i and the j coordinates of each
piece so that we are able to decrement
the board at that piece when we collide
with it so that'll make sense once we do
this next piece but we do need to make
sure so squares is what we've got
what we want to do is say for i
in
range
length of squares so we have this list
of all of the squares
and we want to check
if ball
dot collide wrecked with
what is going to be at squares
i but that's also the first item inside
of i because i is going to be a tuple
square's i is going to return tuple the
first one's going to be a rectangle the
second one is going to be a coordinate
um so
it would be just like this so if ball
collide wrecked then the first thing we
want to do is we want to say
and you know what i think this would be
easier if we return it as a list so
append
uh can we do that
that should be fine i don't really know
that that would be
better or worse than what i just did
let's see if it works uh this is what i
have in mind so if the ball collides
with any of those rectangles then first
thing we'll do is we'll just flip the y
direction
uh and actually i'm thinking we're gonna
have to flip either the x or the y
direction depending on whether you
collide with a square from the left and
right or from the top and bottom but
we'll take care of that in a second
first let's just get this sorted out
because the second piece we want to do
is say board at and then the
first piece of that is going to be
squares
i
1
and then i think we can do this so the
first one would be
and then we want this same piece here
ctrl c
ctrl v but then 1 and we want to
subtract 1 from it so let's see if
that's legit hopefully we can bounce up
and down now
oh the board did not draw correctly
figure out that in a second i'm sure i
did something stupid
okay
four i in range
for j in range
oh yes we want to do row
equal to
hm
that would probably be the right way to
do it wouldn't it
let's make a new blank row for each of
uh each eye and then let's add each of
these entries to the row
uh we want to return board squares
outside of the for loop that might have
been it
but then we'll do board
squares
dot append each row and we'll do that
outside of this for loop
so um we want to draw the board that way
let's just start it up and print the
board that may work but let's just
print the board so that i can see or
actually print squares so i can see what
we're returning real quick
let's just run this real quick
argument must be wrecked style object
right we're gonna have to change it now
uh so but you can see each item inside
of our list we've got by row but we
actually don't need that now
um
so let me
let me get rid of these sets of brackets
and then we're going to have board
squares
each
i
is going to tell us how many rows and
then we'd have j
so i actually don't think we want to do
the rows i think that was a step in the
wrong direction pete you dummy
so we will just say
board squares dot append and that should
catch every piece and let's just try
that now
list a pen takes exactly one argument
i know that
so
let's say it's a tuple actually that
should be fine
this is uh
taken a minute
all right the board looks okay i'm
printing that thing which isn't great
let's just start it
all right bounce up bounce down hey
but it did go from red to purple
and purple i think is supposed to be
five and red's supposed to be one
let's see if i can get one to hit a
second time here it is at least bouncing
back to us which is good
um but those i think should be going to
zero
oh except we don't have any condition in
here so aha smart not smart but
what we want to do is say if bored at i
j
greater than zero
so this should take care of
this should take care of once we hit a
piece it should just go invisible so let
me see
let's run that
i should just have to hit one piece i've
got a bunch of ugly stuff there we go
and maybe i'll be able to get through to
the second row now and i should be able
to knock them down to red
so
we're doing okay on time we're about 20
minutes in
um
alrighty let me see can we hit can we
hit
can we hit something in the second row
please all right it goes to red that
looks pretty good um i'd ordinarily
probably test it a little bit more at
this step but we don't need that print
statement anymore
what i think we do need to do though is
we need to return the left edge right
edge top edge and bottom edge because
you're actually going to be able to
collide a piece in a few different ways
and we don't want to flip its y
direction
in any case we want to do it only if it
hits it from the bottom or the top
so i think maybe this isn't the best way
to do it but we're going to define some
rectangles
top bottom
left
and right
and this is going to be pie game dot
wrecked rect.rect so for this it's a
little different than doing draw.rect we
are just going to go um
in here and basically grab
our coordinates for this rectangle and
we're going to kind of shave off the
thing into just a few rectangles
but the normal way to do rec dot rect is
two tuples one for the uh starting
position and then one for the width and
height
so let's see the top of it we're going
to want to keep j times 100 and i times
40. those are going to be the same but
we'll also keep the same width but we'll
just make it a height of 1. so this is
going to be a super skinny rectangle at
the top
and then bottom
we've made it 38 high so this will be i
times 40 but then we'll add
30
8 to it or 37 because it'll be one thick
so that'll be a skinny little rectangle
at the bottom and then the left
rectangle the starting position will be
the same but then we're going to want to
make it
37 tall
but only one wide so that looks good
and then the right rectangle so we're
basically pulling off edges but but
still making them rectangles so that
we're able to do
uh pygame
dot collide rect which is a nice
built-in collision detection tool
so
j times 100 and then we will add the
width of the thing so it's 97 and then
we will make it 37 tall and one
wide okay so i think that's sweet we
need commas between these two tuples
and what i want to do now is actually
want to instead of these being
tuples i would like to return
let's just do it in this
same order top
bot
left and right okay so
now
top left right we don't actually need
this piece but i'm going to leave it as
is for now
if we go look at our squares we have top
bottom left right and then coordinates
uh and we're checking against that down
here so i will say top
bottom left right cords just so i don't
forget
so
if we are colliding with squares at zero
or
so this is good control c
or
and we'll test this in a second so top
or bottom then that means we want to
flip the y direction but then
if we're colliding with the left and
right which will be
two and three
then we want to actually flip the x
direction but we still want to decrement
the same piece so this code can stay the
same and that hopefully should give us a
lot more accurate
um that should give us a lot more
accurate
collision
but let's go ahead and
so right when the game boots up we don't
tell the player how to start the game so
why don't we say
start text is equal to
um
font dot render and the text we want is
spacebar to start the game
and we will
do screen dot blit
and we'll put it
um start text oops
start text and then let's put this
roughly in the middle of the screen so
i'm guessing that's probably about 100
maybe 400
and we only want to do this if not
active and the reason i'm doing this now
is i want to put in kind of the
conditions for failure here pretty soon
let's see spacebar i guess is two words
so it starts up start text is font
render function takes oh right right
this is uh not complete on its own
we also have to say true and then color
for the text we'll say black
okay
let's try that
spacebar start game yeah so that's too
far to the left we'll go with 40.
maybe that'll look good
uh that looks okay we're not gonna worry
about it too much now so i hit space
start the game uh we bounce up and
list indexes out of range oh yeah yeah i
didn't update the coordinates so this is
no longer
i
one zero this is now going to be
what do we do zero one two three four
zero so four zero four one four zero
four one that should take care of that
and start the game
okay
so
we've got uh red
we've got red and a clever person
watching the game right now is probably
going to pick up on the fact i think
this game is going to move exactly the
same way every time
because the way the player interacts
with the ball all they have to do is
make sure to hit the ball
but it doesn't impact the direction it's
moving at all
uh so
that's kind of a boring game this is
cool i mean i think we got fundamentally
pretty close here
that was kind of weird right there it
hit the bottom and it didn't flip
hit the bottom it didn't like quit
because we haven't put in losing
conditions yet
but let's see here
all right
all right so we've got a decent core
here going on um
but let's go ahead and put in some
conditions for losing so what we'll say
is i think the biggest way you can lose
is just um
if ball
y
is less than or equal to
negative 10
and then we'll say active equals false
but then what we need to do this is game
over so we also want to
we also want to reset everything's
position so i'm going to come up here
and grab these initial things like
xspeed y speed x direction y direction
player positions i'm just going to copy
all this down and actually the board too
so we're gonna grab all of these
everything except width and height
and uh if the ball is less than or minus
is less than or equal to uh we want to
do height minus 10
then we'll basically reset the game and
that'll be how you lose so let's see if
i can test that real quick i'll just
oop
ball is less than or equal to height
minus 10. oh it's got to be greater than
or equal to
after all that talk you do about
knowing what the pi game coordinate
system is
all right
so maybe rebound it once and let's let
it hit the ground
and it resets the game so that's pretty
good uh if you wanted it to sit on the
bottom until you hit spacebar to reset
it then you could um you could freeze
the positions you could set active equal
to false but not do all of this
until the space bar was hit so you could
put that up in the event handling for
sure i kind of like it like this
but let's do something that i'm pretty
sure regular breakout does which is if
you collide with the ball while it's
moving in a certain direction and you're
going in the same direction you can
speed it up and if you do the opposite
you can slow it down so let's see if we
can do that in this collide rect player
stuff
let's check if player direction is equal
to ball direction
and so if player direction is equal to
ball x direction then let's add ball x
speed plus equal one and then let's say
l if player direction is equal to
negative bollocks direction so this
would be you're going in opposite
directions and let's add a condition
this one because we never want going
perfectly vertical
and
ball x direction
is
or ball x speed
ball x speed is greater than one
and so let's say uh that means that it's
at least two let's do ball
x speed minus equals one and then let's
do an l if and they're opposite and it's
equal to one
and ball x speed is equal to one then
let's do ball
x direction and let's actually
flip it so times equals minus one
actually bollocks
yeah ball x direction there we go so now
we're saying okay if you are the paddle
and you hit the ball going the same
direction then let's speed it up and if
you're going opposite direction slow it
down and if it's already going really
slow and you hit in the opposite
direction you're actually going to be
able to change its direction using the x
and y
direction of the paddle so let's see if
that actually works
okay
so i think i was able to hit it in the
same direction there and it looked
pretty good let's try it again yeah it's
definitely speeding up
uh horizontally let's see if i can slow
it back down nope i missed
uh let's see if i can slow it back down
now
yeah that for sure slowed it down and
that's going to change the way it's
played every time which is pretty cool
it gives the player a lot more control
and let's see if we can get to flip it's
probably at like 2 in the x direction
right now so it should be 1 so i'm
hoping when i hit this it'll flip
directions yeah i think it did i think
it did there and i should be able to do
it again
yep yeah it for sure flipped when i did
that so that's pretty cool and then you
can speed it up by going the same
direction that's really cool it feels
the way it's supposed to feel
let's add an element of score tracking
to this the best games are telling you
what your score is at all times so when
we restart we'll say score is equal to
zero and then let's go down to
resetting everything and we'll say score
is equal to zero actually let's do that
when active is pressed so you can still
see your score after you die so
um
space and not active and we'll set reset
the score there and what i'll do is i'll
basically take this whole if not active
statement
and i'll add an if active statement and
we'll put the score on screen so we'll
say score text is equal to
and let's make this a formatted string
so that we can put score in there too
score
and then the score variable
and then score text and let's put it
like in the top
left so x position of 10 and y position
of like
five would look cool this should be
moderately out of the way it'll kind of
be in the top uh it'll be in the first
rectangle by a bit
but it should be in one of the easiest
places to avoid while the game is being
played but we haven't done anything
yet that's going to um
increment your score so let's check when
the ball collides with any of the
squares that's what we want to count as
one so we'll say score plus equal one
in both of these and so now we should
keep track of the score let's test that
out
spacebar to start looks pretty good
um
score two that's cool let's see now if i
die
um
it should move everything back to
starting positions oh it's not resetting
the score but actually we don't need
that if active we can show the score all
the time so
let's get rid of that and then let's
move those back here
two maybelline inclines i guess i don't
care about that
so score zero let's go ahead and get one
and then die it'll keep showing you the
previous game score and then once i hit
start it resets it so that's working
perfectly i really like that
um i think we've done quite a lot of
good things here if it hits the bottom
we lose
i guess technically
we haven't done anything for winning and
we also haven't told it if it hits the
top of the screen to bounce back down so
let's do that
let's see so if do do do we have a few
things checking if it goes off screen if
ball x or ball y
now let's see
where do we check if it hits the ground
right here we should add
we should add the checking if it hits
the ceiling right above this so we'll
say
if ball
y is less than or equal to 10 then we'll
just want to
ball y direction
times
equals minus one
and actually before we even do that i
think we should set ball white equal to
10 so that's basically saying if somehow
because of speed we moved past
10
we're going to put it back to 10 so that
it's not a problem where it's like
bouncing back and forth above the screen
and we should probably have done that
here too but that's game over so it
doesn't matter
that should work uh let's see let's see
let's see game over so we want game over
to set it equal we want all of this
but i guess it would be cool to have
something if you won but doesn't really
matter does it so
um
let's see game over is basically when
squares is going to be empty because
this list will have nothing left to draw
when it's completely empty
so why don't we check
if
squares
let's just check if length of squares
is equal to zero
um and then let's do the actually let's
just put it in here so or
there
and so this is saying now there's
nothing left on the board uh ready
or length of squares is equal to zero
then we reset everything and i think
that's pretty cool let's try that
uh this would take a really long time to
beat the game so maybe actually what
i'll do is just um
i will just make the initial board just
a bunch of ones so
and let's make a dummy board that's
gonna be equal to
uh one one one one
uh let's just make it three it's
probably already gonna be hard enough
for me to hit three
three ones let's see if that even craps
out the system okay
all right i want to see it hit the
ceiling real quick and i'd like to
finish the game and see it reset so
let's see
all right i might get lucky i might hit
all of them
hit the ceiling
okay bouncing off the top is working
that's good now come on hit the hit the
last square
oh man this can be the most frustrating
part of actual breakout ah damn it
let's try that again
okay
all right please pete hit it quick
because we are wasting precious time
oh my god
all right i'm just gonna make it one
square we're gonna do this
we're gonna beat the game with one
square
that is cool though once the game was
over it reset it to uh
reset to no squares all right i'm
actually gonna have to use some skill
here it's not just gonna fall into my
lap this is really bad
let's speed it up maybe
oh my gosh that's like exactly what just
did this is the biggest waste of a
coding challenge ever but i hate to just
assume my stuff is working
because it takes a really long time to
finish a game
this is so bad
i even gave oh man all right
i'm speeding up the x direction as much
as i can because i would think the
faster it's going the x direction the
better but like there i was going the
opposite direction
oh man
so bad
all right come on all right if it
doesn't if it doesn't get in the next
like two bounces i will uh i will
legitimately call it come on hit
okay all right it reset that's why i
want to make sure we got a new game um
so the other piece is look the board is
going to be exactly the same right now
every time which maybe that's what you
want especially if you do levels you
could create your boards pretty easily
in this kind of format and then you
could make like a list of all the levels
and just increment the levels make them
progressively harder
or you could do what i think
i'm about to do and create a new
function that's going to create a
randomized board so let's just start
with board being an empty list
and
do let's make a new function right when
this starts up so even before we draw
the board
let's say board is equal to
and we'll say create
new
board
hmm create new board that's a long
function name but uh it's fine we're
going quickly and we'll pass in the
existing board i guess actually we don't
even care about the existing board so
we'll just pass in the existing board
and we'll say
if create new is true so we'll make a
new variable that'll control when it's
time to make a new board and so since
the initial board is empty we'll want
create new to be true right away and
then when we do our reset
which is in here rather than this uh
board we'll just say create new is equal
to true
okay
and let's make the function that we
called what do we call it create new
board i think
we're pretty good on time 40 minutes
create new board
okay
so define
cree 8
new
board
and we don't have to pass anything in
but we do have to make sure we return a
board so uh let's go ahead and make it
and what i think we should do is say
um all right board is initially empty
and then let's say uh rows so how many
rows we want to be will be random dot
and then we'll pick a random integer i
think we should have a minimum of four
rows per level and maybe a maximum of
eight
and then we'll say four
i in range
rows
we'll set uh we'll make this empty row
and then we'll say dot append and we
will pick a random
dot rand int
between one and five so that's how many
that's what we just decided the thickest
block could be
and then
for i in range rows
row is black row append
uh oh but then we want to do a 4j in
range and five because we want every row
to be five that's how it'll fill up the
screen
so we'll want row to be empty
up here and then we'll want to say
row.append
[Music]
yeah like this
and then in each
out here
we'll want to say board dot append
each row
i think if i'm reading this correctly
then what we'll do first is we'll pick a
random number of rows between four and
eight and then for each of those rows
we'll create an empty row and then we'll
go through and pick five random numbers
and add those between one and five to
each row and then for each row that we
create will append to the board with
that so
maybe that's bold but let's see if it
worked that was a lot of code
oh boy
uh i don't think we ever turned create
new off so
that was sweet though that would give
you a headache to play with for sure
um so what we want to do is as soon as
we create new which we do
do right here we want to turn create new
off so that we only ever do this once at
a time so create new is equal to false
and let's check that out all right this
is definitely a random board it doesn't
look the same as the previous board and
it's still we can still play it and it
obeys our rules we're still getting a
score which is pretty cool
and
all right i'm not going to spend too
long playing the game but i do just want
to see okay
so the dynamics maybe are a little funky
around the edges um i'm not totally
convinced that our left and right
collision is working properly the bottom
and top definitely is but let's see
if we can pick that up again i was
looking and it kind of looked like it
hit an edge and did the opposite of what
we wanted it to do
but that's kind of a hard thing to catch
because it hits the bottoms a lot more
often than it hits the sides
it's definitely fun and it's a little
bit challenging which is what you want
out of a basic game just kind of a time
waster game
all right man this is really hard to
prove okay well we got a new board and
every time we die actually i'll go ahead
and die here we got four rows this time
and we got four rows again um i think
that first one that loaded in was five
let's see
yeah this one's five we should be able
to get like seven or eight let's see
here
it's random so yeah there we go we got
six that looks pretty good you can
clearly see it's totally randomly
generated
which is pretty sweet let me check out
that x and y that side collision real
quick
so if the player direction if you're
going left
and the ball is going left
then we want to increase the speed
that's right no i said i was going to
check on collision didn't i
so if you collide with the left or the
right edge
i think what we want to do
is only
change the direction you're going
if you're going left and you collide
with the
right edge so let me see here because i
think it may be possible if it hits
right on a corner
to um be going left and hit the left
edge and that should not flip you so
let's see and
ball x direction
is equal to and so this would be hitting
the
left edge zero one two left edge and so
if you hit the left edge of a square you
would have to be going right
and then let's say and
or ball collides with a rectangle
and you are going
so this would be left and i guess we'll
move that on to the next line since we
have an or there
um although that shouldn't have errored
so we might need a backslash here
and then we just want no that should be
fine
i think i did something goofy
with my formatting
what the heck oh did i not close a
parenthesis that's pretty likely
yeah close that parentheses
okay that's why my formatting was a
little jacked up didn't need that right
now
uh so that looks okay
i think that's what we want to do though
i think we only want to let it collide i
mean by the same token we should be
doing you can only hit the bottom if
you're going up uh but we're
um at like 45 minutes and i think we've
got a pretty good game here but what do
i do argument must be
style oh damn it yeah i put it in the
wrong spot
okay so that's squares and ball
direction i need to close this first and
close this
out here that should do it stupid little
formatting trying to rush
all right i don't actually know what i
was hoping to get here it's going to be
too hard to test that but i think that's
an improvement
um let's go ahead and do some style
upgrades so i mean this is a pretty fun
game right now it's basically what i set
out to accomplish so for those keeping
score at home at 47 minutes i said
officially i feel like this version of
breakout is playable and fun
um but i think we can improve the style
just by adding some outlines to things
and so let's go into our draw board and
start there
so
i'm just going to do pygame
dot draw.rect and i'm going to put on
the screen and i'm going to say that
these outlines will be black and i'm
gonna put them uh as the exact same
rectangle as this rectangle
and then i also want to make these i
think i'll do a width of three and i'm
gonna make them rounded rectangles so
i'm gonna give them a border of three
and so even though these initial
rectangles i wanna be solid i'm still
gonna make them rounded just so they
match and this should give every piece a
black border which is pretty cool
actually let's go ahead and give it a
width of five and round them by five i
think that'll look better
yeah i think that looks pretty fun kind
of cartoony um but maybe you're seeing
score doesn't look that great now
because it's black on what can be some
dark colors so let's go down to our
score text
and let's do what i think is kind of a
cool i call it like shadow box effect
let's draw a white score over the black
score and let's just move it a little
bit to the right a little bit down and
now you should have white text with
black background so score equals zero
uh maybe we move it one more pixel to
the right and one more down that's a lot
more than one pixel there
okay
score is equal to zero that's pretty
cool maybe it looks better with white
behind it honestly let's try that we'll
see
let's move it back there and let's move
it up and to the left so eight and three
because i think black does pop more
especially on like the gray once it's
done
square equals zero yeah that's okay
that's okay we'll leave it like that for
now
um i also think we can improve the way
the paddle looks and the way the ball
looks with outlines so let's go ahead to
where we draw the ball
and actually i don't even need to say i
don't need to give these variables we're
not using them
so
pygame.draw.rect screen will make the
outline black and then again we're going
to give this ball x and ball y for the
center
center and then we will
give it a width of three this one
doesn't need to be
as wide as the other ones
but actually program we did draw direct
and we want to do circle uh okay so we
first need the radius we'll do 10 and
then we'll do three and that should be
good we should have a cool
ring around our ball yeah which looks
pretty neat uh if i hit some blocks real
quick yeah i mean that score is a little
bit tricky to read but we are doing a
speed run so i'm not gonna get too
obsessed with the details
uh you know what there i think actually
saw the ball hit the bottom as it was
coming downward and there it hit the top
as it was going upward i do not want
that to be able to
affect this
and it weirdly looked like it traveled
through the side of it so i may have
messed up my left and right collision
let me do the last outline that i think
would look good
um i'm going to create a little inner
rectangle inside of the player so let's
say pygame.draw.rect
and we'll put this on the screen we'll
make this white it's going to go inside
of the player rectangle so we'll take
this whole thing ctrl c
and what we'll do is we'll make it a
width of three a rounded rectangle of
three and we will also
say player x plus five we'll move it to
the right and we will move it
down
minus eighteen and then we'll also say
110 and then we'll say 11. so let's see
that should give us a white rectangle
that's basically inside our paddle yeah
that looks pretty good
okay so now let me play it a little bit
see if we can get those gameplay errors
and maybe iron that out yeah so it's
definitely it'll hit right on a corner
and it'll kind of behave weirdly
um the good news is when it hits from
underneath which is definitely the
majority of the game that appears to be
working pretty good
um but
let's see can i hit something from the
side please
oh that worked pretty well it hit right
on a corner and it got all three just
once which i think is what we want
this is pretty fun and i think it looks
pretty good and obviously
speed running it is not necessarily
ideal but i think we got a pretty good
product here
now that looks pretty good
okay um well i'm gonna say
one hour uh didn't even need the full
hour 52 minutes and that looked pretty
good
so it is
done
at 2
35
and i
may i don't think i want to add i don't
think i want to add any more
functionality to this it looks like it's
working pretty good uh i hope you
enjoyed this video i don't want to do
that
i hope you enjoyed this video
if you would can if you did enjoy it um
consider checking out uh tech with tim's
i mean he's not affiliated with the
channel at all but this was his idea he
is a huge python channel i am a little
python channel so maybe consider
dropping me a subscribe i really could
use the support and i really appreciate
all of the people who do comment uh
subscribe follow me
um all sorts of things like that i
really appreciate you guys it helps me
out a ton
i love being able to make these kinds of
tutorials and videos so let me know in
the comments below what you thought of
this video what you'd like to see in a
future video obviously this is not
necessarily a 100 percent ironed out
game with all the bugs fixed but this is
pretty darn close hope you had fun hope
maybe you learned something i will
probably drop this code into a github
and leave the link in the description
below as well as probably a description
to the inspiration video for this from
tech from tim tech with tim's channel um
so thank you guys so much for watching
uh until next time i really appreciate
it good luck with your projects and
thanks for watching thanks bye