Learn to create an idle click style game in the model of the classic and popular adventure capitalist style of app by using python and pygame! This is episode 2 in a 4 part series that covers start to finish how to create this entire game from scratch! We cover the basics of python and pygame code creation while also diving into more advanced programming concepts in this series!
Episode 1 covers the basics of defining the board game space, as well as importing the module, defining screen dimensions and creating a library of colors. Additionally we create a function to draw the tasks onto the screen rather than defining each individually. We then cover conditions for exiting out of the game as well as displaying text for the value of each event onto the buttons.
Episode 2 in the series handles the actions from the player of actually clicking on each button and updating the money or score the player has accumulated so far! This includes getting the position of the mouse upon clicking and checking if the coordinates are aligned with any of the defined event buttons.
Episode 3 includes the drawing of the buttons for purchasing more of each event commodity and displaying the manager buttons that would allow the player to automate each task. Additionally we block transfer (blit) text onto the screen showing the cost of each element and round all numerical value to only two decimal places.
Episode 4 concludes the series by handling the actual purchase and automation of each task when the managers are purchased, as well as increasing the value of each event and the cost of purchasing more of each event.
Be sure to like, subscribe, and leave a comment on what you found useful or what you'd like to see next! Thanks!
welcome back to the channel and part two
in our uh creating an idle clicker
adventure capitalist ty uh type app game
in python using pygame so uh this video
is gonna be a continuation in what we've
already created which is we've already
drawn out this board which has what's
going to be the five buttons displaying
the cache value of each task and then
we've drawn the outline of what will
become the loading bar for each task so
if you want to follow along line for
line i recommend you either pause this
video
on this code so that you can copy it
down or go back and check out part one
in the series but without any further
ado let's get into it so the next thing
i want to do is create it to where if
you click on one of these buttons
a loading bar of different speeds
because obviously the more valuable a
task is the slower the bar should load
but i want to make it so when you click
the circle that bar goes across the
screen from left to right and then once
it finishes it's going to
add money to your overall score
so
to do that we're going to need to
kind of modify our draw task
function here
and um to do that we're gonna need a few
new variables so we're going to need a
variable that's going to be true false
boolean telling you whether or not it's
time to draw the rectangle so when you
click the button that's what will turn
true we'll say okay it's time to draw
the
time to draw the button and then what
we'll need to do is pass in a value
saying how long to make that loading bar
because what we're really doing is once
per frame we're going to update the
length of that loading bar and that's
how we'll control the appearance of a
continuous rectangle expanding from left
to right
and then we'll want a third one that
i'll call speed and that's how we're
going to determine
which ones are the fastest and which
ones are the slowest and so if we create
all of this code inside of our draw task
function we don't have to do it five
times for each of the different colors
we can just do it once for um for all of
it as long as we write it generically
so now that we're passing in draw length
and speed let's go ahead and take a look
at what we're actually going to do with
those
so we're essentially saying
that if the
draw condition is true
and length is less than 200 because
that's the length of the full bar is 200
well if those are true then what we want
to do
is we want to add speed
um to the length so length is going to
start out at 0 for all of these because
it's going to start out unclicked
and
we're going to make the speed of like
green which is the top rectangle we'll
make it one so each iteration it's going
to add a value of 1 to the length but
then that speed will go up to 2 3 4 5
actually i'm sorry i have that backwards
the fastest one will be green because
it's worth the least money and so we'll
modify that using
speed and then
we'll say
else
and we'll do l if length is greater than
or equal to 200
then what we need to do is we need to
turn that draw variable equal to false
and then we'll make length back equal to
zero and then we'll do one more thing
which is we'll actually add the value of
whatever
was just finished we'll add that to a
variable called score and since score is
going to be the same it's going to be
your like constant money pot that you've
made during the game we'll just make a
global variable called score rather than
do it as an individual
okay and so um the last thing is we're
going to draw a third rectangle and this
is actually going to be the loading bar
so we're going to do pygame.draw.rect
we'll put on the screen again this one
will be green just like the first one
that we made although sorry not green
it's going to be whatever color because
we're keeping it generic
and then this one is going to be
70
and then this will be y coord
minus 15 just like the first rectangle
and then the biggest difference between
the first rectangle and this one is it's
gonna have that variable length um as
telling us what uh what length to make
it so we're gonna need to pass a few
things back out now
and since we actually want to be able to
click on the circle we have to call it
something so we're going to return task
which is going to tell us where the
circle is so that we can click on it
outside of the function
and then we need to return the length
and we need to return
the draw variable so essentially
anything that we modified in here that's
going to be not global so score we don't
need to return but these three we do
because we're modifying them in the
outside world even though we're passing
them into a function
okay so now we obviously we need to make
um a lot of variables in the outside
world that we just called in this
function
so let's go up to our game variables
and we're going to have
five new
true false variables for drawing all of
our colors
we'll have draw
green red
orange
white and again whatever colors you want
to make it's definitely more fun if
you're following along if you make this
game your own change around some colors
change around some values make it fun
for you change up some speeds change up
some you know monetary values
whatever you want to do but essentially
these are going to be the variables that
say when something is active or when
it's not so like when you haven't
clicked the button yet this variable
sits at false but then once you click it
once we're going to draw that rectangle
once across the screen and then once the
rectangle is finished it'll go back to
false so that's going to be the draw
variable
and then we have to make
all of our length variables which are
going to start out as zeros because just
like i said um since everything is
false to start none of it's been clicked
we don't want that rectangle to even
really populate yet so we're going to
give them lengths of zero
orange
white and purple i really just picked
these colors because i knew they'd show
up okay on a black background you do
whatever you want and then the last one
we added was speed for each of them so
like i said the green speed needs to be
the fastest
and the
purple speed will be the slowest but
again you can
this is really one of the spots where
you can really make it your own game
play around with how quickly things
change different values whatever you
want
and that's going to
uh that's going to really change like
every time you play the game
how things modify and get changed will
be based on variables like speed and
value that you can modify
okay
so
those and then we want to make a global
score so to start it's going to be equal
to zero that's going to track your money
throughout the game
and then we're returning task length and
draw and we need to pass in those three
extra values
to all of these so let's see
i think it was draw
and then it was
length and speed
so what's cool we wrote that code to be
generic
and now
we can just add the variables to each of
them okay and we're
we're returning
task
we're returning length and we're
returning
draw
and so just like this
we can do this exact same procedure
for all three of these now so instead of
instead of task 1 green length and draw
green it's going to be task 2
red length and draw red
and then the next thing that we'll do as
well and instead of green value
draw green green length green speed we
need to just whoops
we need to just put all of that in terms
of red so
you can follow along if you want you can
speed forward a little bit if you see
where this is going because i am just
going to take the next minute here
and add these
task length draw all these new variables
too
each of them
just takes a minute there's nothing too
novel to say at this point
but then the other thing you might be
wondering i just kind of made that task
thing where we passed the button
the circle back and we're assigning it
to this variable task whatever
the next thing we're going to do because
really all this stuff we're doing right
now is meaningless until you are
actually able to click the button so
that's what we're going to be doing next
is taking that task variable that we're
passing back
and we are going to
use that to actually
do something when you click on that task
we are going to make the rectangle start
gang drawn so that's what we'll do next
and task five here we go
you may be looking at this and seeing a
lot of duplicate code um and saying well
hey i could
i could
write like a for loop put these in a
list and iterate through the list
definitely you could do that if you want
to optimize this code a little bit
i think this is kind of easy to see
everything laid out and just understand
where it's all coming from but
you definitely could eliminate some
lines of code if you structured this to
be
like in a for loop and run five times
throughout the
for loop
so
i'll just
go ahead and run this make sure i don't
have any errors but like i said it's not
going to run
yeah it's not going to run yet because
we can't click on the
buttons yet so now we're going to go
into our
event handling code here and we're going
to add some stuff based on no longer if
event type is quit
now we're checking to see if event type
equals and this is going to be pi game
dot mouse button down
so this is when you click the mouse
and the reason i passed those tasks now
hopefully this is what's
what you've been waiting for if you can
use a pie game built in
collide point function as long as you're
getting a pie game object so we're
pulling that circle back out of the
function that we drew
and now we can use collide point with
event dot pause so that event
event.position is something you pull
back um anytime you click
so event type equals mouse button down
the event position is going to have an x
and a y coordinate based on where you
clicked
and so what we're saying is if wherever
you clicked was um on that green circle
then we want green
to be equal to true okay and we can copy
this exact code and we'll do it for all
five tasks
so task 2 collides with a mouse click
then we're going to draw red if task 3
collides with a mouse click we're going
to draw orange
if task 4
gets clicked on we will draw
white
and if task five is clicked on
then we will draw purple okay so now
let's run it and i'm hoping when we
click this
all right we get a
a bar of different speed so i'll go
ahead and run a few of them and you can
see they run at very different speeds
green is way faster than purple
i can start red after green and they'll
finish at the same time or red before
green and they finish at the same time
because green is faster
so again if you're unhappy with the
different speeds you can change them
around you could make green a 10 and
purple a 1 and everything else in
between you can modify them
play around with it as much as you want
uh then the last thing i want to do in
this video is um actually track your
money so we made that score variable but
we haven't put on the screen anywhere
and uh i think that's what we should do
next so it actually will show you what
like what the benefit of getting money
is right because
um we're currently we're we've made it
so that in our function it adds to your
score every time one of those things
completes but we haven't shown that on
screen anywhere
so i'm gonna use that text again and to
concatenate text uh you want to
do a string and some of this as we like
get into it is going to start adding
decimals it can get really weird
with like um
rounding so i'm just gonna round this to
two decimals for when we do start making
it to where you can buy more and it
starts giving you like fractions of
dollars just round so it doesn't give
you like 11
decimal points
but then so the first thing is to define
what text you want and then after that
you say true and we want this to be
white text on what we'll call a black
background and then we need to put this
on the screen somewhere so i'll put it
at the very top i will say screen dot
blit blit is block transfer i believe is
what it stands for but it is just pie
games
built in
like okay now draw it onto the screen
function so
that's what it means
and let's see if we run this now
okay we get money zero let's see we had
one dollar we had two dollars three all
right sweet so that's working and now in
just two videos we have created
basically the the basic concept of like
an idle clicker um so in the next videos
we'll add the buttons to where you can
buy more of the product which will
increase the value of each task and then
we'll also add the buttons for the
managers which are going to automate it
once you buy them for like a hefty
upfront cost they'll run each task
automatically for you
that's a big piece of idol clickers is
setting up the tasks to sort of be
automated so hopefully you found this
fun and useful and informative if you
did i really appreciate a like on the
video subscribe to the channel it helps
me out a ton if you have any questions
on what you saw here today or want to
see anything specific in the future be
sure to let me know about in the
comments below be on the lookout for the
rest of this series coming out in the
next few days and as always good luck
with your code and thanks for watching
thanks bye