Python Game Development Using Pygame | Majd Hailat | Skillshare
Drawer
Search

Playback Speed


  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

Python Game Development Using Pygame

teacher avatar Majd Hailat, Hello! I teach magic and python dev.

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

    • 1.

      Intro

      0:29

    • 2.

      Setting the Screen

      4:38

    • 3.

      The Paddles

      5:29

    • 4.

      The Ball

      7:12

    • 5.

      The Score

      4:19

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

438

Students

4

Projects

About This Class

In the class, we will be making pong in python using the pygame library. This is an excellent introduction to programming in python. Whether you already have some basic programming knowledge or you have never written a line of code, this is the perfect project for you to get your feet in the water and start building your own programs.

Anyone can get started with this class, and ZERO programming knowledge is required. 

You will learn:

  • How to add a background to your window
  • How to add objects to the screen and set their size position and colors 
  • How to move these objects around
  • How to get user input through the keyboard
  • How to display text on the screen 
  • How to check for collision between objects
  • Other python basics such as loops, conditional statements, etc.

If you do not already have Python and/or Pygame installed, there is a text file under the "resources" tab with instructions on how to install Python and/or Pygame.

I apologize for the slight cracking in the audio in the first lesson. The other lessons do not have this issue. I will be remaking the first lesson soon to fix the audio.

Meet Your Teacher

Teacher Profile Image

Majd Hailat

Hello! I teach magic and python dev.

Teacher
Level: Beginner

Class Ratings

Expectations Met?
    Exceeded!
  • 0%
  • Yes
  • 0%
  • Somewhat
  • 0%
  • Not really
  • 0%

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. Intro: The awesome thing about making games is that you get to see your progress and interact with it instantly on the screen. In this class, you will be creating pom pom touches on many different aspects of making games in pie game . So I just had to move objects on the screen through user input and how to check for collision between objects. Whether you're completely new to programming or you already know some of the basics, this class is a perfect way to learn how to use pie game and to get some experience in developing games in. 2. Setting the Screen: All right, Welcome toe lesson one of making pong in python. So in this lesson, we're just gonna be setting up our screen. So here I have idle open, and we're gonna create a new file by going up here to the top left and clicking file and you file it should be similar on windows, and we're gonna go and save it by clicking command us or coming to the top and clicking file again and save as I'm gonna name it Pong. And you want to make sure to add the dot p y to specify that it's a python. Fine. Okay, so the first thing we want to do is we want to import pie game. Pie game is the main library that we're gonna be using. Teoh make this game. So we're gonna do from pie game import Astra's, which means import everything in that library. Now we're gonna define our screen with which I'm going to make 900 you can change it, whatever you want. So everything that I do, I'm gonna do it a certain way. But you can feel free to go ahead and change an experiment and do your own your own things , and I'm going to find the screen height as being 600. Now we're gonna define our screen. So screen is display and we wanna add in our with here and our height, and you want to make sure that you include these bubble brackets. Those are important in this case. Now we're going to find some colors that we're gonna be using throughout, so we'll start off with white, and I like to use all capital because they're constants. Thesis variables are not gonna change. So white is just to 55 to 55. That is the RGB code for white, and I'm going to define our background color. So the background color that I'm gonna be using is sort of a bluish green color. But you can feel free to be whatever rgb color you would like. So it 0 to 20 end 1 16 and now I'm going to define blue, which is gonna be represented by the player. One paddle, and I'm going to be using a later blue on. That's the RGB code for it. Or actually it's 230 and red, which is 230 50 and 100 side. Okay, so those are the colors that we're gonna be using now, we also have to define this variable that I'm gonna kick call running and we're gonna set up a clock. So the clock ensures that the program only runs 60 frames per second, no less and no more. So that's what the clock does. Now we're going to start our main loop. So while running and notice how running is set to true. So this is the condition that has to be met for our loop to keep going. And this is the main loop for our game. This is where on our game logic is gonna go is gonna be Rin. So the loop is gonna keep going as long as running is set to true and we can end our game by changing running to false. So that's what we're gonna do right now. So we're gonna set up our event listener, and that's just like that. And we want to check if the player closes the game. So that's gonna be event. That type equals quit. And what that means is that the player closed out the game by clicking the expert in top. And if the player does do that, we're gonna go and end our loop by setting running to false And of course, that's going to stop our law running loop. Okay on. So we got our main loop set up. Now we have to fill our screen. So we have to make sure that our background color is constantly being refreshed onto the screen so that if, for example, a paddle moves, the old rectangle for the paddle is not gonna go away unless we repaint the screen. So think of it as repainting every frame. So that's what we're gonna do enough so screen dot fill BG color. So that ensures that we are repainting the screen with a fresh set off the back on color, every single frame on Finally, we have to at this piece of code, and this just should make sure that our screen is being refreshed as well offend. We're gonna update our clock and finally outside of the loop, we're gonna add quit. So what this does is when the loop ends, this is gonna get called, which closes the program. All right, that's all for lesson one 3. The Paddles: All right, Welcome back to lesson to of making Pong and python. So this is where we left off, and I forgot to run the program last time. But this is all it is. Just a blank, blank window with our background color. So in this lesson, we're gonna be setting up our paddles, and we're also gonna allow the player or actually both players to move the paddles using W and s for player one and the Iraqis for player, too. All right, so we're gonna start off by defining our variables. There's a few more variables that we have to define. So the 1st 1 is the player One Why variable. So this is the why coordinate of the player one's paddle. And this is, of course, going to be changed based on how the player interacts with the paddle. And the 2nd 1 is, of course, the player to paddle the white. So I'm gonna set this at 2 50 which is roughly in the middle of the screen. So again, these are the white coordinates of the paddles on. I'm also gonna define the paddle with which is gonna be around 30. Of course you can change that to your likings and the paddle height, which I'm gonna go. Ah, height. Alright. Can't spell on. I'm going to define that as 100. Okay, so now we're gonna define our paddle rectangles. So this is an object in pie game, and this is how you created. So we're gonna go ahead and do clear one paddle, which is a wrecked object, and we have to pass in the x coordinate. So the X coordinate of our paddle is we're gonna set it for 50. That's the 1st 1 and you can play around with that. But I think 50 works out, and the Y coordinate is, of course, our P one. Why the with is gonna be our paddle with and the height is gonna be our paddle height. Okay. And that's our player. One paddle. Now we're gonna one copy this, and we're gonna go ahead and rename it to player to paddle. And the only difference is that instead of the X coordinate being 50 now it's 8 50 the y coordinate is gonna be player to what the width and the height remain the same. Okay, Now, for the second part of this tutorial. We're going to start getting the responses. The key presses from the players. So the way we're gonna do this as we have to listen for keys so we're gonna do keys equals key. Don't get underscore pressed. So this key this variable is getting the most recent key presses. Okay, so of course we said the player one is gonna be w and s. So we're gonna check if the w keys being pressed. So this is how we're gonna do that. And the second thing we have to check is if the player one paddle is not already at the top of the screen. So of course, right now we're just checking if the player one is pressing the Y, the W key, which is supposed to make his player his paddle go up, but we don't want it to go up if it's already at the top of the screen. So the way we're gonna do this is we're to check if the player one why is also greater than zero. So we only allow it to go up if the Y coordinate is greater than zero. Okay, so if that's the case, if the player. One is allowed to go up. We're gonna go to subtract five from the Y coordinates. Of course, zero is at the top. And as we go down, the values increasing. So subtracting five will make the pie, it'll go up. And now we need to make our LF statements so otherwise, we want to check if the player is pressing the s key, and we also want to make sure that it's not already at the bottom of the screen. So that way we're gonna do this is we will take the y, coordinate, and we're gonna add our height. So our paddle height and we're gonna check that That is less than the screen height. And that's how we can make sure that it's not already at the bottom of the screen. And if this is the case, we want to add five to our Y coordinate. So five is essentially the speed of the paddles, and you can change that to whatever you want. I think five works out, though, So now we're gonna copy this code, and we're gonna do the same thing for the player to So instead of w for players to it's actually gonna be the up and down arrow keys. And we also want to check if player to why is great and zero. And we want to check here Player to wise. Well, okay. Oh, and we want to change thes as well. So again, we just copied it. What we changed is up and down. We changed player to here, player to hear, player to hear and player two here. Okay, so the last thing we have to do is we have to actually draw our paddles. So this just defines the rectangles, but we're not actually drawing them onto the screen so we can come down here and we can do draw, direct, And the first parameter is the screen. So we're gonna go in type screen, and then we're gonna type of color. So we said, player one is gonna be blue, and we're gonna go ahead and send player one paddle. That is direct tingle that we're going to be drawing. Now we're getting to the same exact thing for the player to accept. This is gonna be read, and we want to change that to it too. All right, that's it. So let's go ahead and run our code. And there we go. So we have our player one, and we have our player to. And as you can see, it cannot go past the screen borders. All right, that's it. 4. The Ball: All right, welcome to lessen three of making Pong and python. So this is where we left off and we have our paddles and we can control them using W and s for the left, Paddle in the arrow keys for the right paddle. In this lesson, we're gonna be building our ball, and we're also gonna be programming the collision of the ball. All right, so let's start off by defining the X and Y coordinates of the ball so the X coordinate is gonna be 4 50 and the wide coordinate is gonna be 300. And this is roughly the center of the screen. Now we're gonna define the horizontal and vertical components for the ball's velocity on This is essentially just a ball speed. So we're gonna call those body acts and baldy y, And in order to get the ball to move, we are gonna repeatedly within the loop at the horizontal velocity component onto the X coordinate in order to get the ball to move horizontally and the vertical I'm velocity component onto the y corn in order to get the ball TEM of vertically. So note that when the horizontal component is positive the ball will move to the right because adding a positive number will move it to the right. And if the horizontal component is negative, adding a negative number well decreased the X Corps, the X coordinate and it will cause the ball to move to the left. Same applies with the vertical component. If the D Y is positive, adding a positive number two the white corn, it will increase the balls y coordinate so the ball will move down. And if Baldy y is a negative value, adding a negative value to the balls, what coordinate will cause the war according into decrease, making the ball move up? All right, so now we're going to get the ball to move are. Actually before we do that, we should probably define the ball. So we're gonna call the ball. It's a wrecked object, and the X coordinate is ball axe. The white corn is ball. Why and the with and the high our boat tens. Of course, you can change the size of the ball and also you can change the speeds to your liking. I think four is a pretty decent speed, but if you feel like the ball should move faster. You can change those now we're gonna get the ball to move. So in order to get the bottom of we have to odd baldy x to the X coordinate and odd ball D y to the y Coordinate. Note that this happens every single time the loop goes through. All right, now we're gonna write the code for the balls collision. So the first thing that we have to check collision for is the ball colliding with the paddles. So we're gonna start off by checking collision with the player one paddle or the paddle on the left. So we want to read in if statement and we want to check if the ball collides with P one title. And if it does, what we want to do is we want to just forgot a bracket. If the ball collides with the paddle on the left, we want to create, or we want to set the balls horizontal velocity component to be moving to the right. So that means we want to set it to be positive. And the best way to do that is to take the absolute value of it. So we're gonna do Baldy act is equal to the absolute value of ball DX. All right, now we got to do the same thing, except we want to check collision with the paddle on the right, so all right. And if it does, what we want to do is we want us to take we want to set Baldy X to be the negative value, because if it's negative, the balls start moving to the left. So we're gonna do the same thing. We're gonna take the absolute value. But now we want to multiply it by negative one. All right? And that should send the ball moving to the left. And actually, that should be enough. All right, now we want to check the collision with the ball and the top of the screen. So we want to check if the balls why coordinate is less than or equal to zero. And that would mean that the ball is at the top of the screen because the top of the screen here is has a y coordinate of zero. And if that does happen, we want to set ball d y to be a positive value, and I will send the ball going downwards. And now we want to check the ball collision with the bottom of the screen. And we can do that by checking if the ball the Y value of the ball Oh, not D Y just fall wide is greater than or equal to the screen height. Oh, it's about that time. And if it is, we want to make Baldy y and negative value, and so we can take the absolute value of it and multiplied by negative one. All right, The last thing that we have to check collision off is with the ball and the right and left sides of the screen. And if the ball does collide with the left or right side of screen, that means one of the players got a point. And so what we want to do is we want to reset the X and Y coordinates of the ball so that the ball starts in the middle again, and we also want to reset the paddles of Player one and player two so that they're also in the middle. All right, so we're gonna check if the ball on the X coordinate is greater than or equal to the screen with, and that will tell us if the ball ISS hits the screen on the right side or the ball. X coordinate is less than or equal to zero. And that will tell us if the ball hit the left side of the screen and inside of here. We want to set the X coordinate of the Baltic for 50 the y coordinate. We want to set that to 300. We want to set player ones white, the one chord in it for the player. One paddle 2 to 50 and the Y coordinate for the player to paddle 2 to 50 as well. All right, so this is all the code for the collision. And so, just to recap, this checks the collision between the ball on the left paddle. This one is the ball in the right paddle. These are the top and bottom of the screen. And finally, these are the side of the screen. All right, There's one last thing for us to do, and that is to draw the actual ball onto the screen. So we're gonna do draw direct, and we're gonna make the ball white, and then we're gonna pass involved. All right, that's it. Let's go ahead and run our program and collides. Where did I mess that up? Collide? Direct. But sorry, that's my fault. So instead of collides, you actually want to set that to be collide. Direct. All right, and there we go. There's our ball and let's see if it works with the paddle. There we go bounces off the bottle. Same with this paddle and let's see if it goes on to the edge of the screen. And there go. The ball starts in the middle and our players reset. All right, that's it. 5. The Score: All right. Welcome to lessen four of making Pong and python. So this is where we left off last time. We have our ball moving and we set up all the collision for the ball. In this lesson, we're gonna be making a scoring system, and we're also gonna display the players score in the top of the screen. Okay, So the first thing we want to do is we want to set up a font so that we can actually display the score. So we're gonna type font in it to initialize our fund. And we also want to define a font. Now you can use any fun to like or any font that's insult on your computer. I'm gonna be using Calibri. So, Khalid Revolt 35 is the name Opus about around Calibri. Bold 35 is the name of the variable that's gonna be holding the font, and it's a system front on. Then. This is where you would type the name of the fund. So I'm using Calibri bold, and it is size 35. All right now, we have to define a few more variables. And the 1st 1 is the player one points which we're gonna set to zero and player two points , which is also zero. Okay, now what we have to do is we have to go all the way down here to the collision section. And this is what we check the collision for the ball and the sides of the screen. Now, So far, all we're doing is we're just checking if the ball collides with either the left or the right. But now we want to know specifically which side it collides with in order to give the players the points that they deserve. So we're gonna check if the ball collides with the right side by seeing if the X coordinate is greater than the width of the screen. And if it collides with the right side of the screen, that means player one got a point. So in here, we want to do player. One point plus equals one. All right, now we want to check if it collides with the left side. And if it does Clyde with the left side, that means player to got a point. So we're gonna do a player. Two points plus equals one. Yeah. All right. So now we have our points variables and their increasing as each player gets a point by the ball hitting the side of the screen. Now, the last thing for us to do is actually display the scores in the top of the screen. So we're gonna come down here and we're gonna define our player one points text. This is gonna be the actual text that gets rendered. So it's gonna be using the Khalid Bree Bold 35 front that we defined earlier. And we are going Teoh, right Player, one points. This is the actual text that we're gonna be using and we can add a semi or a colon. I mean, and we want to add our actual points. So we have to have to convert it to a string, cause as of right now, it's an indigent and so we have to convert it to a string and then we want type true here. And finally we want to type the color that we want the font to be displayed in. So we're gonna be using blue and we want to do the exact same thing for player two points, except we have to change this toe to And we have to change this to a to and we have to change this to do. Of course, we have to change the color as well. So I'm gonna be using Red for player, too. Now, the last thing for us to do is actually place this text onto the screen. So we're gonna do Screen dog Bleidt player one points text, and I'm gonna put it at 100 and 30. Common 20. So that is the X and Y coordinates off the player. One points, text on. We got to do the same thing with player, too, and we're gonna put that at 620. Common 20. All right, that's all. Let's go ahead and runner code. And as you can see, our points show up. So if we allow player to Digger point as you can see the points increase and now let's get player. Want to get a point. And there we go. That's all