Learn to develop video games using python and pygame!! This is episode 7 in an intro to Pygame series from LeMaster Tech. This episode covers creating code to handle progressive difficulty that will increase the speed of an object to be dodged as score increases, creating an increasingly harder game as the player advances!
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 and welcome back to the channel
today's episode will be uh part seven in
our pie game with python tutorial series
where we're learning to build a video
game from scratch
and if you haven't been following along
with this series so far
um what we've done is we've built a game
where you're essentially controlling
this orange rectangle player
and trying to dodge a ball and then we
have
put in code for
collision detection for game over
as well as space for restart and high
score tracking and previous score
tracking
so that's pretty cool we've really got a
playable game put together already and i
recommend go ahead and check out those
previous episodes
if you haven't seen that but if you're
just here for the concepts in this video
we'll be looking at
making a progressively more difficult
game as your score advances so the way
the scoring works in this game is when
the ball collides with a wall
um we increme um we add one to the score
every time
and what we want to do is make it to
where the ball also speeds up
after a
certain
score
so what we'll do is we'll go down into
our game loop and we'll create a new
function which i'm going to call check
difficulty
okay
and let's go ahead and create this
function and what we're going to do is
be constantly running this
and checking
uh
f check
difficulty and we'll be checking inside
this function to see if the score has
gone high enough that we should
increment the
the speed so a few variables that we're
going to need so i'm going to go ahead
and make them global we're going to
check against the total score
and we're going to need to check uh
we're going to need to modify
the circle x and circle y direction
because those are um those are our total
speeds so uh
i'm going to create a few local
variables in here that i'm going to call
x mod
i'll put an rs going there so this is a
modifier in the x direction and just so
that we don't change it exactly
uniformly
i'm going to go ahead and do that score
uh floor division by nine when you're
dealing with stuff that has to be
integers it's best to use that double
division that's floor division that
means it'll round down to the nearest
whole integer
so i'll do x mod and then i'll do y mod
and for this one we'll do score divided
by 10. just so they're a little bit
different when you start getting up into
higher scores
it'll change it by different things and
that just kind of creates for a more fun
game dynamic you could make them the
exact same thing
that's totally fine
so then what we're actually going to do
for this code is we're going to start
saying if the direction is positive
right then our modifier is going to be
circle x direction
and actually it's going to be 4 which is
what we made the initial x direction
ball speed
plus our x modifier that we just created
so this is saying if it's positive get
more positive by the x modifier
and what we're going to do this should
feel fairly intuitive is we're going to
say if it's negative then it's going to
be negative 4 minus the x modifier so
think about what we had previously was
the direction
the change of direction made it either
positive or negative but the numbers
stayed the same
so we want uh if it's moving to the
right we want to move to the right
faster by this x mod and if it's moving
to the left we want to move left faster
by the x mod so
shouldn't be a surprise we're going to
do the exact same thing with the y
direction
and again if you wanted to have them
both increase in speed
by the same amount you just wouldn't
need a second modifier variable which is
totally fine
but then
that should do it so actually this we
made it five
so we want it to be
five plus or minus the y modifier
and um now since we're calling that in
the game loop we should be able to go
ahead and run this and it might look a
little
funky it might be hard to tell i'll try
to actually do well enough at the game
we notice a difference but at nine so
there the at the x um
at nine the x speed
increases and then at 10 the y speed
increases so then oh i didn't do very
well
so you maybe noticed it pick up a little
bit there but you really start noticing
after the speeds kind of increased by
two or three
so let's see if i can get a score a
little bit higher or if you're following
along with this tutorial on your own oh
my gosh i'm so bad at this game
if you're following along on your own it
should be pretty apparent it can be a
little hard to capture in the screen
recording i'm sure
just because the frame rate of the
capture is only like 20 fps so it might
be a little hard to gauge exactly how
fast this is uh
going
um but they're actually you can probably
see now like it's clearly picked up
quite a bit of speed in
both the x and y direction the ball's
really jumping around quite a lot
um
so that's pretty cool i mean that's a
pretty basic function that we put in um
just to
uh just to make the game progressively
more difficult so you know if you get
good at just controlling your guy and
avoiding the ball at base speed there's
really no way for you to run up the
score now because the game eventually
will become
essentially unwinnable
and so if you're gonna like put a
difficulty modifier on you could make
this 9 or this 10 a variable as well and
you can play around with these settings
so if you're like well this is lame i
want someone to be able to get to close
to 100 or close to 200.
you can just start ramping these
variables up or down to make that speed
increase faster or slower as you want
and this is a pretty typical
function you could create for
any modifier so if you wanted speed to
increase if you want sized increase if
you want color to increase you could
just run a checker
referring to
you know your total score or whatever
condition you want to check against to
see if it's time to
modify the game or not so that's a nice
short video there we just
increase the difficulty of the game as
your score gets higher and that's pretty
cool so if you have any questions or
comments on that or would have done it a
different way go ahead and let me know
in the comments below otherwise if you
found this or any of the content on the
channel helpful i really appreciate a
like and subscribe it helps the channel
out a lot
and as always good luck with your code
and thanks for watching bye