Hi, Nice to see you back.
This lesson is a little different and that we're going to set up the LIBRARY that we use to build our Game. It's called PY GAME. PY Game is a set of Python code called a library designed for developing video games.
For today's lesson, go on over to www.replit.com and then on the top right corner, click the “+” sign and a pop up should appear. Select the language drop down bar to PyGame and name the file PY_Intro_1, then click Create Repl.
Now let's move forward and import the code from the PY Game library. IMPORT is used to get code from another file. That file is called a LIBRARY. There are lots of libraries out there for Python. But the library we're going to be using today is the PY Game library.
To access the code in the PY Game library, we create an Object. This is called INITIALIZATION in Python speak. This code allows us to import and initialize the PY Game library. Our other code would not work without this, so type the code on the screen in your editor.
The next step is to set up the drawing window. We do this by initializing the window or computer display. Yeah, you're right. Without this, we wouldn't see anything on the screen. So go ahead and create a Variable and Initialize a window or screen for displaying by typing this code into your editor.
Now, let's start to build some of the elements that we need for a Game. Let's start with a very basic element, turning the Game off and on. How might we do that? Any ideas from our previous lessons or concepts we could use? How about a Loop? And what about ELIF? Isn't that Boolean values?Type the following code into your editor. The first line creates a Variable which stores a Boolean as true or false. The second line begins a While Loop. While “running” is equal to true, the code will continue to Loop until “running” is equal to false. So the While Loop will continue to run until the user asks to quit or exit the Loop.
How can we do this? We don't want our code to run forever. Remember the FOR LOOP that we introduced in Lesson 6.0, we can use the For Loop to go through all the EVENTS happening within PY Game. The Event we want to listen for is clicking the X in the top right corner of the Game screen. The first line is a For Loop that listens with an IF Statement for the X on the Game window to be clicked. And when that's done, the Boolean variable “running” is set to false.
Take a moment and type that into your editor.
Using the for Loop, we've created an Event which when encountered sets the variable “running” to false. We'll cover Events in detail in later lessons, so don't worry about them now.
Next, let's create a background for our Game and fill the screen with a solid color using RGB format. RGB means red, green, blue. Those are the three primary colors. Using red, green and blue, we can create a wide spectrum of colors that are specified using a code from zero to 255. Now the specification for White is 255-250-5255. What do you think the specification for Black is?
Now let's utilize some of the code in the PyGame Library to create a shape on our screen. We want to draw a solid blue circle, and we want it to be positioned in the center of the screen. So we have four Parameters that we need to specify, can you identify them. The first is where we want the shape to appear. The second Parameter is the color using the RGB color code. The third Parameter is the position of the circle on the screen. We define the center of the circle to indicate its position using two numbers, x and y that specify the
position on the screen. The fourth parameter is the radius of the circle. The radius of the circle should be measured from the center. Please note that nothing will be drawn if the radius is less than two.
So we have four parameters that we need to specify the size, color position and where the shape should be displayed. And here's why. Go ahead and type this code snippet into your editor.
Our last step is to refresh the display by typing this code into your editor after the circle code we have to quit PyGame using the Quit method now let's execute the code. You should see a window like this image.
Great work today.