Python Pygame Tutorial - Episode 1! Getting Started, Creating Screens and Drawing Objects!

LeMaster Tech
183 followers
25 Video Views·Apr 23, 2025
Learn to develop video games using python and pygame!! This is episode 1 in an intro to Pygame series from LeMaster Tech. This episode covers importing the pygame module, defining a game screen, creating a running game loop and drawing objects on the game screen!
Be sure to check out our channel for tons of additional python tutorials and practice problems and be sure to like and subscribe if you find our content useful!!
https://www.youtube.com/channel/UCV5Ab39YnXvTZ6Grar9URxQ
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 uh
today's gonna be the start of a new
tutorial series on this youtube page um
and this will be an introduction to
pygame so if you're unfamiliar with
pygame it is a module you can import
into python that has a lot of built-in
functions for building games and other
sort of
computer
based applications where you might want
a user interface to operate kind of like
a game
so let's get right into it
first thing you're going gonna do always
is import pygame
uh you have to call it out it's not just
built into python um if you don't have
it installed as a module you might need
to run pip to install it
i'm not gonna cover how to do that here
there are plenty of tutorial series on
how to um install modules with pip you
can check those out
and then once you've imported it the
first thing you need to do with
pygame is initialize it just by typing
in pygame.init
and then uh next thing you'll always
want to do kind of is define your screen
um where you're gonna actually be
playing and so we'll do display dot set
mode and we'll give it
oh this uh now we're
defining the overall uh width and height
of the screen we'll go ahead and call it
600 by 600.
that's going to give us a 600 by 600
game window
and i always like to do this i just
define some colors
everything inside of pie game when it
asks for a color it needs a color in rgb
format with three numbers zero to two
five five
and
i always like to make a few
colors right at the beginning
just so if you if you want to call red
rather than have to type in
25500 every time you want something to
be red you can just type in red
but there's not a built in
color table so we just go ahead and
define those
and this way also if you have stuff
that's a specific shade of red or a
specific shade of blue and you want to
kind of tweak them you can do that up
here in the variable rather than doing
it every time it appears so
i like to define some
to find some colors right up front and
then
this is something you'll always want to
do in python you create your game loop
so you start with this variable i call
it running
people will say active anything it's
it's just while
running
is true
and this is actually
just the same as uh
so
while running is true and this is the
same as just saying while running
due to python's shortcut with booleans
so i'm just going to leave it as while
running but what we're basically saying
is while running equals true
and now we're
inside the game loop so first thing you
have to do so you don't get an infinite
loop is for event
in
pie game dot event dot get
um first thing you have to do is check
if
someone quits event dot type
and equals to pie
dot quit some of this syntax is specific
to pi game so
um you're just going to kind of need
code to reference it the first couple
times but eventually you'll just kind of
remember what stuff is
but what we're going to say in here is
if someone quits which you'll see when
the window pops up that's just the x
it's
that's when you want running to equal
false so you exit the
you exit the game loop and then we're
going to go ahead and actually create
the screen and we'll go ahead and
screen.fill
and um let's make another variable and
we're saying the color of the background
so background
equals you could make it black or white
to kind of show why i like doing it with
variables let's go ahead and call the
background blue
and so we're saying fill the screen with
the background
and let's put an object on the screen
let's do pygame dot
draw dot circle
and
when you use pygame's built-in draw
function it starts asking for a few
parameters python will give you
the uh
they'll give you the conditions so it'll
say
surface for union so we're saying we
want to draw this onto the screen we
defined and then the second thing it
asks for is a color so let's go ahead
and call it red
and then it asks for position on the
screen and so we'll put it about in the
middle
and that's for the center point of the
circle
and then next thing it asks for is
radius and everything after that is
optional but like the next one would be
a line width so if you don't want this
to be a solid red circle you can define
a line width of 5 and what we should see
is just an
outer diameter circle of radius 30 about
in the middle of the screen
drawn in color red
and then the last thing we need to do
since we used um the pie game
set mode the display set mode up here to
actually get everything drawn onto the
screen you need to do display dot flip
and this is actually going to push
everything we drew to the front and put
it on the screen
and then at the end you just do your pie
game dot quit and that's how you
actually
end the uh and the loop so this is a
pretty quick introduction to pie game
but
if we go ahead and run this we would be
expecting a 600 by 600 screen with a
blue background and a red circle drawn
about in the middle so let's go ahead
and run it
and that's what we get so i mean nothing
crazy we haven't made anything move yet
um you know but we have defined a window
and we've drawn an object and real
quickly to kind of show you why i do
this stuff with variables instead let's
say if we want
the circle to be green run it again
well that took me about three seconds to
change red to green and
immediately you've kind of changed the
look of your game so
it's just a handy reason to not just
punch in i mean this is the exact same
code as if you went and did zero two
five five zero and fully defined in rgb
value inside of these parameters
but
it's just it's better to uh use as many
variables as possible so that's a brief
introduction to pygame in the next
couple episodes we'll be looking at more
things like actually making the object
move um creating a user
and making them move and creating an
objective in a scoring system
we'll really build a game from start to
finish in this series so
drop a comment if you have any questions
about what you saw here or if there's
anything specific you'd like to see
going forward and if you found this or
any of the content on the channel useful
i'd really appreciate it if you left a
like and subscribe to the channel and as
always good luck with your code and
thanks for watching thanks bye
