To create a Platformer game in python, one of the most important things is being able to quickly create, design build and edit your levels for the game!!
Believe it or not, a level editor is a really cool project on its own, and pretty fun to use once created!! You go through the same steps of loading in all your images for the different tiles and setting up a PyGame game board as you would in your actual game, but the only 'player' controls you need for a level editor are the ability to change tile assignments using the mouse!
We then set it up to display the board you've just created to the console window so you can copy good levels into your main game!
The full platformer using the tiles you see in this video is coming in the next week so be sure to check out the channel for tons of great content, and subscribe and turn on the bell to make sure you don't miss any great future projects!!
TIMESTAMPS: 00:00 Intro and Project Showcase 03:08 Basic Pygame Project Set up 06:23 Platformer Tiles Needed 08:22 Loading in the images 12:35 Creating tile and image lists 16:00 Set Up Your Main Game Loop 17:43 Draw Board Tiles onto the screen 25:00 Event handling for tile changes 31:35 Export board level array to window 36:01 Outro
Thanks for watching and goodluck with your code!
what's up you guys welcome back to the
channel and in today's video i'm going
to show you how to quickly create a
level editor in python using pygame for
a platformer style game
[Music]
so i'm working on a full pie game
platformer tutorial but i really want to
be fully fleshed out with a lot of
levels and features so i don't have that
video ready quite yet for this week's
tutorial
but i thought would be really useful is
if i showed this tool that i made to
actually design the levels that's a
little bit faster than going through and
entering numbers into an array one by
one so this level just to talk briefly
about it is really just a series of
tiles it's broken up into eighteen
squares of a hundred pixels by nine
squares of a hundred pixels
as you go across you have zero through
17 and as you go down you have 0 through
8.
and so in the level you have the ability
to make any one of those tiles
any any tile that you design so i in
particular i have like acid and then i
have just solid ground and then i have
walkable like top soil and then i have
these floating platforms um and then
yeah obviously this acid is like
what what hurts you if you step in it
and then i have these collectible key
cards that are your objective you want
to collect these key cards to unlock
certain portals
and then all the different portals which
are the actual ways you progress through
the levels you get key cards they unlock
portals and it would actually take a
really long time if i just had a
numerical value dedicated to each of
those and i was sitting looking at an
array and planning out where each thing
should be it's much better to do this
visually
so to show you kind of what this level
that you're looking at right now would
look like in
array format it's this text down here so
this is what we were just looking at
because zero is just empty blank sky
one is just plain ground two is top
ground that you can walk on four is acid
three are platforms um five is the
player's starting position six through
nine are keys 10 through 13 or doors it
would be so complicated to try to make
something like this for every level and
every phase in your platformer just by
looking at the numbers and then if you
want to change anything it's a big
hassle to go through and change anything
so the way to do this and the best way
to create a platformer that has a lot of
unique levels is to create this level
editor program which is sort of like the
platformer game but all you do in this
game
in this version of your program is you
just quickly design levels and i'm going
to show you how i created mine how you
can create your own talk about best
practices tips and tricks and then a few
ways you can modify it if you want to
take this the extra mile okay so without
any further ado let's get into the
actual tutorial all right so let's get
going on this platformer level editor
this is something you're going to want
to do for your program um i will talk
about it again when i get to the full
programmer tutorial but i'm not going to
show you how to build a level editor on
the full platformer video so
this is great uh great to know ahead of
time um and so because it's all driven
by pygame we're going to start just like
we were building the actual game and
import pie game and then do pi game dot
in it if you don't have pie game already
installed you might have to run pip and
install pygame but make sure that's your
step one in step two and then
we're just like we're setting up a game
we're gonna set up the pie game
um
window with a lot of set variables and
we'll make it pretty big so we're gonna
make it with 1800
height
900 uh which will be the end result that
i'll make the actual game dimensions as
well
and then i'm going to use this tile size
variable that non-tile based games
obviously don't need but that's going to
say
how we're dividing the screen up okay so
then screen equals pi game dot display
dot set mode and then we just give it
width and height just like that
um and then pick an fps just like always
and we'll say 60 set up a timer
pie game dot time dot clock
just like that
and now let's go ahead and make a
caption pie game dot display dot set
caption and we'll just call it
level
editor
okay
and uh now let's go ahead and
start
sort of laying out the level so let's
start by just creating a level that's
basically as a default empty sky
um
and then we'll give it one layer of just
rock and then one layer of like top soil
to walk on so to do that we're gonna use
this kind of quick
and easy shortcut um
for writing uh some initial for loops
where i'm gonna say zero which is empty
sky for underscore in range 18 which
just means it's going to be a row of 18
zeros um and then for underscore in
range seven so it's gonna give me um
basically my first seven rows are just
gonna be empty sky and then what i'm
gonna do is this level dot append and
i'm gonna add uh i'm gonna add
sort of this style of row but now i'm
gonna make it all
walkable floor and then i'm gonna make
one that's all like underground so
i don't need a comma there so this is
going to say make it all walkable floor
and then this is going to make it all
underground that's going to make more
sense to you
when we get to the point where we're
actually drawing in each different tile
and what that means
but that's how i set up my initial board
just so i have something to
create from you absolutely could just do
for underscore and range nine and start
from all blank squares if you want
i just know most of the time i'm gonna
want like a legit floor
so we'll start there and now we're gonna
load in images and this is the important
part of the design it actually needs to
start before you get into the python you
need to have your images ready
so for the purpose of this game um if i
go into assets and then images in my
project folder i have a whole bunch of
things already created so i have my
player this astronaut that i made i
think he's kind of cute
i have four frames and that's how i'm
going to control him walking basically
back and forth
but that's in the folder astronaut and
then in key cards i have these four
really cool looking pass cards that get
you into different colored portals shout
out to my patreon supporter dale he made
the key cards for this game they look
awesome so thanks dale if you want to be
involved in projects like this in the
future be sure to check out the patreon
link in the description of this video
and we'll collaborate on some cool
projects in the future as well
so then for portals the actual doorways
that you go through as you go through
the level i have four there in my
portals folder and then if you want to
follow along with this tutorial i have a
few things for acid
i'm actually going to use this low flat
one because i don't want it to take up
the full tile space i don't want to have
all this black unnecessary space
and then just a platform which is about
half size of the ground and just the
rock that's underground so those are the
tiles i'm using and you definitely uh
can grab those from the github link in
the description of this video as well
and beyond that the only other images
i'll be using are this lock
which i'm not going to actually need
that until i design the real game and
then this space background file i just
made this really quickly it's going to
be the background of my game those are
all available on the github as well
so feel free to feel free to grab that
if you want to
um follow along and i'm just going to
start loading these images in and this
is going to take a while here so
pygame.image.load is the basic format
you need to follow to load in a file and
then just point it towards where it is
in your program directory so mine are
all going to be inside this assets
images folder and uh because i scaled my
background to be the right dimensions it
is 1800 by 900 i don't have to scale
that one but what we're gonna do for the
rest of these is this additional pi
dot game.transform.scale
so we need to have this outside of this
pygame dot image dot load so the whole
line is going to look kind of like this
where we do now this instead of assets
images its assets images tiles and then
rock dot png
and then i need to scale this because i
made my tile size 100 by 100
so this one needs to look like this but
this is taking in my tile which i think
i made 300 by 300 originally or maybe
500 by 500
and it scales it down to 100 by 100 so
this is what you want to do to make sure
all of your
all of your tiles are
fitting nicely in the window and they
scale down to the size you actually need
them to use
okay so we do the same thing for rock we
do the same thing for the ground and
then for the platform because that one
is only half height
we actually want to change the scaling
on that one to 100 by 50.
and then the acid is actually a lot like
this except it is only a quarter height
almost forgot to update that
plat form.png and then the acid i'm
going to use actually the image acid 2
for the tile like this
and you'll see when we get to the full
platformer game tutorial we actually
just want a quarter of the height to be
for acid so that's how we'll um scale
that one and now uh we need to pull in
the four images for our colorful keys
and our colorful doors so to do that
let's just start with blue key equals
and it's going to be very similar to
this only it's now the keys aren't in
tile anymore so
uh it's assets
images
key cards and then this is i think
keyblue there we go
and uh for scaling on these this was
kind of a guess and check for me so if
you're using my tile pack 60 by 100
looks pretty good but if you're using
your own keys just you might have to
play around with drawing them on the
board a little bit and figuring out what
works for you but okay let's go ahead
and do the rest of the keys really
quickly blue
green
red
and yellow
or gold it's kind of gold but
we'll say yellow
green
key green and then key
red
and then key
yellow just like that and now we're
going to do something very similar for
the doors except they're going to be
scaled 100 to 100 again that's kind of
the size i made them so let's start with
blue
door
equals
oh
what have i done
looks like i accidentally deleted the
whole thingy that's okay
all right and then this is pie game
transform scale this one's going to be
100 by 100 and it's no longer in key
cards key yellow it's now in portals
i guess i called it portals i could have
called it doors but that's life
and then this one is blue
okay and so now i'm just going to copy
this one three more times as well so i
know the beginning of this video is
going to be pretty tedious because uh
we're just loading in all of our
different images
so if you did grab the program from the
github hopefully you found like the time
stamps on this video and you skipped
ahead of where we loaded in the files i
hope i set the video up that way
i can't always tell what future pete's
gonna do that guy's unpredictable
um
okay but so now we've got all the doors
all the key cards all the platforms
loaded in um and i'm going to create a
few lists because that's how i'm going
to address them later on so i'm going to
create a tiles list
that i'm just going to give like the
first and last characters of it so we're
gonna say zero is actually just an empty
string
because i want like tile one to
correlate to tiles at index one so um
like rock ground
platform
acid where if on your
if on your uh like array for the level
you have a zero i don't want it to be
affiliated with any tile so then i want
everything above that to be associated
with a specific tile so i'm going to
create this list that looks like this
and then i'm going to create a keys list
that's going to look we're just going to
go and order blue
green
red
yellow i think that's how we're doing it
so i'm just grouping them together in
keys because once we draw everything
you're going to see the next phase of
this drawing
project is actually going to go really
fast because we're setting this up so
intelligently
so blue door green door
red door
yellow door keeping that order the same
is actually going to make your life a
lot easier later on trust me
okay and then let's just say uh frames
equals this empty list
and then we're going to say uh so we
have four player frames right so we'll
say for underscore in range and then i'm
gonna say one to five because actually
if you look at the name of those frames
i gave the astronaut they're one two
three and four not zero one two and
three and so what we'll do here to make
this a little bit easier is we're gonna
append add to our frames list
that same pie
game.transform.scale kind of thing we've
been doing
so
pygame.transform.scale image.load and
then these are in images
and then astronaut
and then forward slash and then they're
all uh they're all just named
whatever
index they are dot png so what we need
to do is make this one a formatted
string and put underscore in here so now
this is a little for loop that's going
to go through and it's going to add one
two three and four to my frames list
without me having to type it out four
times
it's just showing you a handy little
trick and then actually the scaling on
my player is a little bit different so
i'm going to show you by saying this
this player scale that i'm going to uh
call 14 that i figured out through guess
and check but basically i want the
player to be a little bit bigger than um
one tile so uh his scale is basically
he's um five wide for every eight tall
that the player is it doesn't have to be
a guy i don't know why i said
he um it's an astronaut it's genderless
so uh basically what i've said here is i
can play around the way he looks it
looks make it a little bigger a little
bit smaller as long as he's it is five
uh five wide for every eight tall then
we're going to have no problem
all right so that is the super boring me
loading in files starting part of the
whole project but the rest is going to
go really quickly from here
and it's really pretty cool so uh thank
you for sticking with me let's get into
now setting up the main game loop as i
always call it okay
so let's come down here and below this
let's make a variable run equals true
and let's say wall run
the first thing you want to do with any
while loop is uh figure out how to exit
your infinite loop so for event
in pygame dot event dot get
if event event.type
is equal to
pygame.quit just like this
then we want to
set run equal to false
and that's going to basically say if we
hit the little x button that pops up
when we load in our game
then we want the uh game to exit so
that's how we handle getting out of an
infinite while loop and now two more
things you want to do in the very
beginning timer.tick at fps
and then we're going to do screen dot
blit and we're going to put the
background image
at zero zero so we're basically filling
the screen with our background image and
then to get that to actually display we
come down to the bottom we say pygame
dot display dot flip like that
and then we just say
pygame dot quit like that okay so this
should just let me open up a big screen
that doesn't do anything but it has my
background on it pretty cool right
so we have the basic uh game loop set up
so now we can put event handling in here
but there's really no reason doing any
event handling until we are drawing the
board so let's go ahead and
draw
board
tiles okay and to do that we're going to
create a function that we will call draw
board just like that
and we will show the uh we'll pass in
the level list so the game knows what to
draw
define
the drawboard function and now this is
going to be pretty easy because we have
all of the tiles in pretty nice lists so
just to remind you what we've got zero
is
empty sky
one is
plane rock
two is
surface rock so like you know
to be walked on
three is
platform
four is acid five we're going to make
player start position
and then six is going to be uh
six through nine are all going to be
keys six through nine keys of various
colors
66 6 to 9 nice
10 to 13 are going to be doors portals
okay so those are all of the numbers
that we might have in our list so
basically now in here
and we want to pass in level
so now what we say is we just set up a
two
two tiered for loop we say four
and now i'm not going to use i because i
use it in the outside world so i'll use
q and
probably p
but for q in range
length of the board which is checking
how many total rows we have in the board
and then for p
in range
length it's not bored though it's level
i'm gonna call it board i guess that
makes more sense
and then for p in range length of
board
q so how long each row is
which should be 18 so technically
putting 9 and 18 in these should work
the same way but this is going to work
no matter what size you make your board
so this is a little bit better
and then what we're going to say is if
bored at q p meaning the specific value
that we're looking at
is not equal to zero so it's not empty
sky that means we want to display
something on
the screen then what we're going to do
is we're going to set value equal to
board qp so i don't have to type in
board qp every time
and i'm just going to call that value
now we need to check which list we
should be drawing something out of based
on what the value is so let's start by
zero to four which are just the ones in
our tiles okay so
if value is zero one two or three or
four
then we want to draw
whatever's in the
tiles package but the thing about four
is acid is a little bit different in
size so we're actually going to draw
that one
in its own entry so let's start with
zero to four because they're all the
same size tiles it's super easy it's
just screen dot blip
tiles at a specific value
and then
j times
tile size and then
uh not j
let me see so this should be your x
coordinate which is going to be p
times tile size and then q times tile
size and so basically we're saying
whatever our x and y position are
multiplied times the tile size and
that's where we want to blit this image
in so then we'll say okay what to do if
it's exactly 4 because it's not the full
100 by 100 we want to actually sit down
a little bit more
well i'm going to take this same screen
dot blip because it's not totally wrong
and all we're going to do is we're going
to say well for the acid we know it's 3
4 of the tile down so we're going to say
plus and the best way to write this
would be 3 divided by 4 times tile size
so basically if you scaled the thing to
be
3 4 of the size you would want to say
0.75 times the tile size or you could
just replace this whole three-fourths
times tile size with a plus 75 if you
don't plan on playing around sizing too
much
now then what we're going to say for
five is uh we actually want to draw the
um we'll just draw the first frame which
is basically the player idling
so frames and we'll just say frames 0
but this is where the player is going to
spawn in so it's just really easy to see
um on the screen if we just make it look
like the player that makes it pretty
clear what you're looking at
and because the player is a little extra
tall we're just gonna scoot him up a
little bit it up a little bit in the y
direction
i think that's a good way to go about
that
um and now let's go ahead and check l if
and then
six that's their equal value plus
they're equal so now let's take a look
at the keys
so basically six up to nine
and i'm just showing this with a greater
than or equals and then a less than if
you want this to be less than or equal
to nine you could do it that way as well
i don't really care how you do it
there's a ton of right ways to program
everything so
all right now we have to get a little
bit more intelligent with how we address
this because we're looking at keys so we
can look at our keys list but it's not
just value like before it's value minus
6 to know exactly where we're putting it
and then because keys do not fill
they're 60 not 100 wide we just want to
scoot it a little bit to the right so
that it still looks like it's pretty
much in the middle of the tile
and then we're gonna do the same thing
for doorways except doorways do fill the
whole thing so we don't need that action
but hopefully
if you understand why we did value my 6
it's going to make sure it's going to
make sense why this needs to be value
minus 10.
because our keys list
takes the
value
which is 6 through nine and it needs to
address a specific thing that's inside
of our list which has zero one two and
three so six needs to be zero seven
needs to be one two needs to be
the next number eight so hopefully it
makes sense that we need to subtract six
to get from value uh which value is just
a linear list of all of our things right
it's this zero through 13. um for our 13
and 14 different characters that could
be in the game
and so we need to relate that to our
different lists by subtracting out this
6 or this 10.
all right so now if i boot this up
you'll see we don't have any way of
changing anything but here are those two
rows i made here's the bottom row which
is all just
plain rock and then here's the row
that's meant to be walked on okay so
that's cool it's loading in our default
uh our default menu
so the next and most important part of a
level editor is now we need to figure
out how to control what each tile is
doing
and how to increment it based off of
that
okay and for that we are going to need
to
uh come down into our event handling and
we're going to need to create some new
event handling that basically says event
handling for changing
changing tiles using the
mouse okay now uh to do this we're going
to need a new type of event so we're
going to say if event dot type is equal
to pi game dot and then mouse button
down and the really cool thing about
mouse button down is it registers your
left and right clicks and if you click
on the scroll wheel but it also
registers if you scroll the wheel
forward or backwards um all is mouse
button downs and it stores it in this um
event that is or or this uh
what would you say this variable this
event variable that's basically what
button was pressed okay so what we can
say is if
event dot button
is equal to things that want we want to
increment so we want to go from zero to
one that would be the left button or
going forward on the scroll wheel so or
event dot button is equal to four then
we're going to increment the tile that
we're on plus one so this will be
plus one for the tile and then what
we'll say is if you use the
left click or you scroll back on the
scroll wheel so that is 3 4 a
sorry left click is 1 right click is 3
scrolling backwards on the scroll wheel
is five and scrolling forwards on the
scroll wheel is four
so uh basically we're gonna say three
and five will increment the tile
backwards one and four will increment it
forwards
but to do that we've got to know what
tile the mouse is over when we click
something so what we say is we have this
chords variable that is
get pygame.mouse.getpaw
but now get pause is giving us
coordinates in x and y pixel um format
so if we were over like pixel 300 and
down 200 pixels it's giving us 300 comma
200
but the tiles have uh have coordinates
that are that divided by a hundred so to
convert these it's really easy we just
say the first one which is the x
coordinate
and then floor division by a hundred
which is basically taking um
is basically taking the pixel variable
here and it's rounding down to the
closest whole 100. so from 0 to 99
that's going to be a zero and then from
100 to 199 that's going to be a one
which is great because that's when
you're over that certain tile so this
really should be divided by
tile size
i don't want to take any shortcuts
let's say tile size well i would take
shortcuts if they benefited me
all right so now we actually use that
to
keep track of what tile 2 increments so
what we're going to say here is okay
so we want to go up well that means that
level at
coords and then you actually have to be
careful with this it's chords 1
and then
coords
zero because the first coordinate is um
is the
row that it's in which is actually your
y coordinate so here we take x and y and
we store them in that order x then y
but for looking up where that is in the
array it's actually your y which is what
row you're in followed by your x
position
okay so i hope i didn't confuse you too
much there and we're going to say plus
equals 1 but this isn't quite enough
because what happens when we get to the
north end of our list what happens when
we get to 13. so we need an extra check
in here that's basically if this is less
than 13
then we want to increment it
up one and then we'll say else meaning
it's somehow greater than 13 or equal to
13
then we will set it back equal to zero
so that's how we'll make it to where you
can cycle um in either direction all the
way
so i'm going to copy all three of these
because even though we have to change
the specific values down here it's
pretty close to what we just did
only now what we're checking as we
subtract one while we go backwards is
that we're greater than zero because if
we're not greater than zero and we're
trying to go backwards then we need to
increment it up to 13. um so this
is all we need to do to get it to where
we can change the level on the screen so
let's go ahead and look at this
okay and just by scrolling my scroll
wheel forward or i'll try uh clicking
left and right um so just by clicking on
the screen now or scrolling the scroll
wheel which i always find once you're
familiar with what all your different
tiles do the scroll wheel is actually a
lot easier to use
um so i've got the
left key
and the right key
working
but why do i have so many
empty values it looks like my portals
aren't working
let's go ahead and check out why we
aren't getting the portals gang
a little bit of troubleshooting never
hurt anyone
yellow door doors doors doors
portals
portals
so then ah it's because we don't have
anything for 10 to 13. there we go
good troubleshooting everybody you guys
probably caught that way before me all
right and now you can see we get all
these portals which is awesome this is
more portals than you would ever need
but i'm trying to show you how cool the
level editor is so just like that we
have this quick and easy way to change
the tiles that are on the screen now if
you're just using it for yourself
you could just basically finish this
thing off by adding in
an additional
event type so basically if event dot
type
is equal to pi game dot key down
and then
i'm just going to check if the enter key
is pressed then i'm going to print the
current level as an easy to read array
to the screen so i'm going to say if
event dot type is equal to pygame dot
key down
if event dot key is equal to and then
the thing is pi game dot key they don't
actually call it enter they call it
return if you've seen older computers a
lot of them said return not enter on
that key
so what we'll do is we'll just say if
you've pressed return okay i'm going to
print out the
i'm going gonna print out your level for
you but if you were to print that out on
one row it would be super long kind of
hard to read and then it wouldn't look
good when you brought it back into your
python file
um
so what i'll say is i'll start with this
board string which i'll make empty
right in the beginning
and just say for i
in
range
length of
level
then uh what i'm oop don't need a
semicolon there this isn't c plus plus
and actually i'm gonna start this i
think with the opening of a list because
this is a list of lists right so
um then for i in range of level
i want to
range length level
i want to
say board string plus equals so add to
it the string concatenator of
level
at i
and then
uh what would probably be the easiest is
just putting new character in here so
backslash n and to do that you need to
put it in
single quotations like that
and now we're just writing a
new string conversion of each row of the
level
um into that list
and then that's the end there and so
what we'll do at the end again
oh my goodness i'm having such a hard
time typing today
at the end here we'll say board string
plus equals and then just close it out
with a close list just like that and
then print
board
string
okay so basically now i can design a
level in here
and then when i press enter it should
show me the level so i'm going gonna
press enter once now just to show you
what we've got here we've got the let
the list that we created to start the
level off at right seven rows of just
empty sky and then two and then a one um
and then if i let's add some stuff in
here let's just add i'm not gonna spend
a ton of time designing uh i just want a
whole bunch of numbers in here so we can
see how different they all are um okay
so i'm making a really terrible looking
board
but i've got a little astronaut there in
the middle
and something important to note is i'm
putting a lot of stuff on here that you
really would only want like one of like
you only want to be able to collect a
key card once you only want one player
spawn position so this doesn't make a
lot of sense as a level here
but because i'm assuming the level
editor is being used by a designer who
knows what they're doing and is trying
to build a fun game i'm assuming a
little bit that you're going to use this
correctly but okay so i'm pressing enter
on this level and now you can look and
see this is what we started from and in
just a little bit of messing around we
got to this whole level and the nice
thing is about this format that we're
showing it in here
is if you were to copy this and then you
were to go into a spot in your python
file where you were actually keeping
your levels like this is where i have my
levels for the actual game
it's already in the correct format so
you just come in and you say like okay
uh this is whatever level 10 equals and
then you copy what you just printed to
the board now you have a playable level
that uh that once you load it into your
main platformer game you can use
okay so that's the basics of a level
editor the version that i put on github
has some extra features because i want
it to be a little clearer if you grab my
version of the code
and you use it i put some instructions
in there i put some additional stuff
where you could say i want to be a
certain level i want to be a certain
whatever
so i hope this helps if you have any
questions about what you saw here be
sure to let me know in the comments i'll
get back to you as soon as i can be sure
to subscribe to the channel hit the bell
for notifications because the full pie
game platformer tutorial will be coming
out in the next week or so and uh we're
going to be doing a lot of cool projects
in the coming months if you like this
content if you'd like to collaborate
with me on future projects if you'd like
my direct link for messaging then be
sure to check out my patreon link in the
description of this video all the
support i've been getting on the channel
really helps and i really appreciate
everyone who leaves likes nice comments
subscribes to the channel um supports me
on patreon so thank you everybody and
until next time good luck with your
projects and thanks for watching thanks
bye