Learn to develop video games using python and pygame!! This is episode 8 in an intro to Pygame series from LeMaster Tech. This episode covers creating code to allow the player to collect speed boost objects after a certain score is achieved to act as power up objects for improving gameplay as difficulty also increases!
If you want to see anything specific on the channel or have technical questions about anything leave a comment on our of our videos and we will get back as soon as we can!
Thanks for watching and supporting the channel!
hello welcome back to the channel uh
this episode will be part eight in our
python pie game uh tutorial series and
this may be the last episode i actually
haven't made up my mind yet or not um
because we're getting pretty close to a
completed game
so if you've been following along you
know this but if you're new to the
channel we have built this game from
scratch where you're essentially an
orange rectangle dodging the ball
and uh you have four directional control
when you collide with the ball uh you
get game over
you can restart it it tracks your
previous uh score as well as your
all-time high score
and um so there's a lot of features in
here they're pretty cool uh we haven't
really spent long making this
aesthetically pleasing as you can
probably tell
um
but uh
what we're gonna do today is we're
actually gonna so in the last video we
made the ball increase in speed as your
score got higher and higher and today
what we're going to do is we're going to
create kind of um the ability to have
player power-ups so we're going to
create a
speed booster inside of this game
and we're going to make it an object
that appears on the screen that you have
to go and grab and once you grab it
it'll increase your speed by one so the
game gets more difficult as the ball
increases in speed but then the player
can also now grab speed upgrades so this
is kind of a cool way to look at player
modifiers
so what we're going to go ahead and do
um we're actually going to create a few
new variables right off the bat
because i'm picturing for the speed
um for the speed boost object just a
little rectangle so a lot smaller than
the player
um
but for this we're gonna do something
we're gonna create a boolean to check
whether or not
a speed boost available
um
whether it's even true or false so we're
going to check based on the score since
we last grabbed a speed boost
to see if it's time for another one so
every like 10 or 15
points that you get one of these will
appear on the screen
so we're going to start off saying it's
false
and then
this initially since we want it to not
be available
the coordinates for the rectangle one we
created initially i want to be off the
screen so my speed x and speed y
eventually will become the placement for
the speed boost
but when this thing first gets created i
want it to be
inaccessible so i'm going to put it off
the screen
and i'm going to make a variable called
last grabbed i'm going to use this to
track what the score was
the last time we got a speed boost
to check if it's time for another one or
not yet
so these are a few variables that i
think we're going to need when we do
this
so let's go ahead and get right into it
kind of like we always do down in the
main game loop i'm gonna create a new
function called speed boost
check and in here we're gonna take a
look and see if it's time for a speed
boost or not
um
which means we gotta come back up where
we've defined all of our game
functions
and let's create a new one called define
speed
boost and
check we called it
and actually this uh it's it sounds kind
of simple but there's going to be a lot
of globals that we want to pull in so
that we can just mess with everything we
have to and i do a lot of my functions
um
with pulling in globals from the outside
world you totally could write all of
these functions by passing the values in
um and then returning whatever data it
is you want to return i do it this way
um
they're both totally valid but there's a
thousand ways you could write all of
this code i mean that's the cool thing
about programming is
you get to create a unique solution and
decide what works for you
so i'm going to pull in speed boost
available to check whether or not it's
time
the total score and the last time we
grabbed one
and then
well
our x and y coordinates because if it is
time for a speed boost we're going to
use this function to overwrite the
negative 100s and
then we are going to pull in player
speed because that is what we are
actually going to increase if there is a
collision
okay
so in here what we're going to say is if
the score minus
last grabbed
is greater than
we'll say 10 to start
and
there's not a speed boost available
speed boost available
so this is when we say it's time to
create one
then what we want to do is make
so we're going to set speed boost
available
equal to true here
um so this should be fairly intuitive
but we're saying okay it's been more
than 10 points since we even had a speed
boost on screen for you to go after
so now it's time for the speed boost to
be available
and then we're going to change the x and
y coordinates
and so this is our first exposure in
this series to random
but this is a really useful tool in the
game building universe a little bit of
random number generation
and since rand int comes from a package
called random
you'll need to use python's import and
from random import rand int we're using
integer because we're doing coordinates
and coordinates have to be
coordinates have to be integers
so i'm going to make speed x and speed y
both a rand int
between 0 and 580 pixels
and this should so speed is a little bit
maybe misleading of a thing to call them
i mean that this is going to be the x
and y coordinate of
um the x and y coordinate of our speed
boost but i didn't feel like typing out
speed boost x and speed boost y
um but so this is going to return this
is going to place a block which i'm
going to define as a 20 by 20 rectangle
anywhere between the left edge and the
right edge of the screen and the reason
i did 580 rather than 600 is it's going
to be 20 wide so again to avoid it
sticking off the screen to the right you
want to stop a little bit early
um
so this is only going to run once
because the conditions for it to be true
immediately make it not true by saying
this bit equally true so then we can
come in here and
say uh
let's see we're going to be checking for
collision with the rectangle here but
for that to make sense we probably want
to go make the rectangle first
um yeah let's do that we'll come back to
our speed boost check function um but i
want to actually
create this down below
so let's go into our
function
and down here where we're drawing things
um when we draw
all the scores and objects onto the
screen i want a new thing that we only
draw
if
speed boost is available right so that
bit we just set inside of our function
to say that hey it's time for a speed
boost
if that bit's true
then we want to create a rectangle that
i'll call speed boost
and that is going to be
a high game dot draw
dot rect
and um
let's put it on the screen
and we'll make it
we'll make it white for now just because
um
and then we need to give it some
coordinates and this is where our speed
x and speed y come into
play and then
i want it to be 20 by 20. so that's
length and width and you got a spell
speed correctly
and then inside of that we want to say
if
the player collides with erect so this
is the first time we're seeing python
has a built-in collide rect function
where you check if an object collides
with us a given rectangle
and since we created the speed boost as
another rectangle python is going to
handle the collision detection for us
here
and if that's the case then we get to
say
player speed
add one
speed x
is
going to go back to its invisible off
the screen position same with speed y
we are going to at this point update the
last grabs to whatever the current score
is
and then we will set that speed boost
available
back to false
so we're saying once you've collided
you're picking up the object increase
your speed and then send the guy back
off screen to be invisible
and this is kind of cool but it's also
something where it'll be
more useful to understand what's going
on if we actually display it so
i want to
display speed
and we're going to put this in the top
right of the screen
so we're going to say
speed and then
because we start the speed at 3 just
because it's a little painful to be all
the way at 1 when the game starts
to display it i'm going to do
player speed
minus 2 so that way it doesn't look
weird having the
game start with with a speed of three
and you're kind of like well how did i
get here
so that's why the player speed minus two
and then we're going to blit this onto
the screen
display speed
and i'm going to put it over on the
right side so 450 should be good for
that and we'll put it back up at the
regular score height
um
let's see
so
oh that's right wit so we also um need
to incorporate some of this into our
game over logic because if you have
um
a last grabbed value that's not zero
you want to reset it back to zero so
that your
and then you also want to reset player
speed so these are just things that
you want to reset everything when you
restart the game to go back to how it
was
when the game started
so player speed and then we'll actually
if it was on screen
we'll shoot it off screen just to be
safe
and let's see
let's go ahead and run it and see what
we got
so i need to be at least good enough at
my game to dodge until we get to 10 and
hopefully we'll see a rectangle on
screen
and if i go and collide with it up there
in the top left let's see if i can get
to it
and yeah so we see there i collided with
it i've got speed of two and hopefully
when i restart the game my speed goes
back to one
yep so let's see if i can make it a
little bit longer and maybe we can get a
higher speed
and see the ball kind of increment in
speed as well
um there we go let's grab that
so i do feel a little faster i'm just
quite bad at this game that i've built
um
sometimes you can design them but you
can't win them
oh boy
okay speed two oh my goodness this is uh
hopefully if you've been building along
um you have you're testing the game
yourself and you're doing a little bit
better than i am
um
it's a it's always a good sign when you
build a
challenging game but it's not usually
good when the person who
makes it can't get
past
a fairly low point
so um kind of cool you can see the uh
actual it's like if you're watching
along the next speed boost does not
appear until 10 after whenever you
actually grabbed it because obviously
the ball score
um increments while you're going and
getting it and so if it took you
10 whole points to go and get the
speed boost another one would be
immediately available it's a little
cooler to make you
have to wait so we wait 10 after you
actually grab it to let your score go up
and in that previous run through in
particular um
it should have seen the ball got going
pretty darn fast too but as the player's
speed also increases you have a much
better chance of dodging it it's like a
key to designing a fair
and fun and like replayable game is you
don't need any crazy complex mechanics
particularly if you're building a game
in pie game
you're not you're not developing complex
3d worlds and stuff you want something
that's challenging
something that rewards players for
increasing in skill and something that
has a lot of replayability
and even though you know you could spend
all day improving the aesthetics of this
and i may do another video after this
purely on how to upgrade your game
aesthetically
but this is the basics of a replayable
game it can be frustratingly difficult
or you can get really really good and i
mean if you get to the point where your
speed is 15 20 you're flying around the
screen and the ball is whipping
everywhere like that's pretty fun to
play
so
that really does it for the basics of
building a game in pie game i think
there's a lot of cool stuff here if
you're really strong in python already
it's a fun tool to kind of apply what
you know about python
with a little integrated develop
development module
that'll let you
create games so if you have any
questions about what you saw in this
video this whole series
or
anything that you've seen on this
channel that you have questions about or
would like
to see something in particular in the
future drop a comment and let me know
and if you found this useful or anything
else useful just uh i really appreciate
a like and a subscribe it helps the
channel out a lot and as always thanks
for watching and good luck with your
code thanks bye