Pygame Platformer Part 1: Setting Up

KidsCanCode · Beginner ·🌐 Frontend Engineering ·10y ago

Key Takeaways

Sets up a basic Pygame platformer using Python

Full Transcript

Welcome back to game development with Pygame. Uh we are going to start on a new game this time. I hope you're excited. We're going to make a platformer. Uh platformers are really popular. Everybody knows Mario. This is going to be a game where you have a character that can jump around and stand on things. And we'll see where we go from there. Uh but the first step is we're going to spend a little time uh talking about how to set up the project uh and make things organized um so that as we move forward and add things it'll be a lot easier to keep track of uh of what we're doing. Okay, before we start I want to talk a little bit about what our plan's going to be. So I opened up the Pygame template which is which we made in the first video of the series. Um, if you need to refresh your memory, there's a link below for um, the video on how we created this and what the different pieces of it are. You know, the game loop and the pieces of the game loop and all that kind of stuff. Okay. And this is a great beginning uh, this great starting point for any kind of Pygame project. But if as you might have noticed if you if you followed along with our last game, uh once the game gets big and has a lot of pieces in it, um this the file can get big and have a lot, you know, there's a lot of scrolling and there's a lot of lines of code. So, uh what we're going to do is organize this a little better. Um which is it's easier to do at the beginning. If you start out organized, it's easier to stay organized. If you wait until you've got a whole bunch of stuff and you've got a lot of different things in your update section and a lot of different things in your draw section, it gets harder to make it organized once you've made it. Um, so we're going to start out planning for this to be a more organized um, project. Okay. So, what I've done is I made a folder. Okay. I made a folder called platforms for our platform game. Okay. And in that is where we're going to put all our files for the game. And that keeps us separated from any other projects you might have uh that you're working on. Okay. And I went ahead and made the image and sound folders, although I don't have anything in them yet. They're empty. Um and here's the Pygame template that we made. Okay. And we're going to rename this in a minute. U but first of all, let's talk a little bit about how we're going to organize it. One really good way to organize things is to break them up into separate files. So, for example, um all of these things right here, which are kind of the settings, these constants that we're setting up for the um the options of the game, um you know, that you want to be able to change in one place and it affects the whole game. We're going to put those in a separate file. So, I'm going to make a new file. Okay. And now this settings. py file is we're going to put all this stuff in here. So, I'm going to cut that out and I'm going to paste it here. Oops. I'm going to paste it here. And then this is going to be the game options and settings, whatever we want to call it. Right now, this file is fine. I've saved it. It's got all this stuff. But now over here in our Pygame template, now we're going to get errors, right? Because it doesn't know what width is, and it doesn't know what height is, and it doesn't know what FPS is, right? So, what we want to do is we want to import this file into this file. Now, there's a couple ways we could do that. We could say import oop import settings, right? Import settings will tell it look for a file called settings py, which it does, and run the code that's in that, right? So, it would go over here, it would look for this file, and it would run this code, which makes initializes all these variables, right? But if we wanted to refer to the variables, we would have to say settings width and settings.height because they come from the settings file. And that gets kind of tedious. So that's one way you could do it. Another way you could do it is you could say from settings import star. And that says import all the things from that file, but it doesn't make you append the or prepend I should say since you're putting it in front the name of the file you imported it from the settings. So now we get all of these variables, variable names loaded into this file. And now when we want to make changes to things, we just have to know we go here and we change these things and they'll change in our in our game. Okay. Um, oh, and one other thing we'll do real quick is I'm going to just make a a variable for the title so that we can set that. And we're going to fill that in later. I'm just using that for a placeholder. Right. So now our our this file will run just fine. Right. There's our game window. We can close and exit. It did the width and height and frames per second and all that like it was supposed to. So that's good. So now what about all of this? Okay. So for for organizing this, I'm going to make another new file. Okay. And I'm going to call that file main. py. Okay. Now, you can name this whatever it there it a lot of people like to call it main so you know that the this is the main file, right? Settings.py isn't the one you want to run to run your program. Main is, right? And so, we're going to start moving stuff from the template over into main. And what exactly do we want to move? Well, to make this a little easier, I'm going to go up here to view and I'm going to click on PES and I'm going to say split, right? And what that lets us do, I make this window wider, is have two files right next to each other. Okay, so I'm going to have my main.py here and my Pygame template here because we're going to start moving stuff over here to get it organized. It just makes our life a little easier. I'm going to hide the file list there so we have plenty of space to move things, you know, and get them where they where we want them to be. All right. Now, the imports, those are a no-brainer. Uh we know we're going to want those over here. Okay. And I'm actually cutting and pasting because I'm going to throw this file away. This is just a copy of my template. If uh if you're using your original template, you know, you don't want to destroy it. So, you can uh cut and paste. Um, so this is going to be, let's go ahead and put our comment up here. This is going to be our uh jumpy platform game. Okay. And we'll fill in the other stuff there later. So, what do we need to do? Well, we're going to make a class game. Okay. And class game is going to have a bunch of pieces in it. What are the pieces it's going to have? Well, it's going to need to have an init, right? And in the in it is going to be the stuff that needs to happen when the game first starts up or the program first starts up. Um, and then it's going to need to have a um game loop, right? So, we'll call that run. So, when we run the game, it's going to run the game loop. You see how I'm just putting pass here? If you haven't seen that before, pass is a Python command that just says do nothing. Okay, because I'm not ready to write the code that goes in in it. I'm just using that as a placeholder. If I put nothing there, uh, Python would give me an error message because it would say, oh, you said defaf and then you didn't actually write any code inside there, right? So, pass help lets us just um just put a placeholder there, right? And we can we should almost we should also comment these things just so we know what they're for, right? initialize um game window and other things, right? We'll fill that in as well. So, run is going to be the the actual game loop. Okay. And then what else are we going to need? Well, we're going to need um to have a update an update section, right? This is the this is the this is the game loop update, right? And we're going to need to have a event section. This is the game loop events. Remember these are the pieces that the game loop every game loop has to have in it. These uh these three things, right? Update events and uh the draw. This is the game loop draw. And then that's going to be pass. Oops, I left off the pass here. Okay. So, you see how I'm filling these in or I'm I'm putting them there and and waiting to fill them in once I've uh once I figured out exactly what I uh what order I want to do them in. Okay. Uh what else do we need? Oh, um we're going to want to be able to have a def new. The new section is the that's to start a new game, right? So, once you once you've ended the game, you you lost whatever, game over, you want to be able to start again. New is the is going to be the code that resets the game, sets the score back to zero, etc. initializes everything. Um or I should say initializes the game, not initializes the whole program, right? Which is the difference between these um start a new game. Okay, so how is this going to work? Well, this class game just is going to define all these pieces of the game. And then when when we start our program, what we're going to do is we're going to make a an instance of the game object, right? There's G for short, right? And then we want to and then we want to run it, right? We want to have a loop that runs the game. And in fact, what we could do is we could have actually have a a start screen, sort of like a um splash screen or or whatever you want to call it, right? And then we're going to have some sort of loop. Okay. And I'm gonna leave that for a second. Okay. Um and then we're going to say start a new game, right? And that's going to actually run a game. And as you're playing the game, you're going to do whatever you do. When the game ends, then we will drop to here and we can show our game over screen. And the game over screen should just, you know, tell us that the game is over. whatever things we want to show the player. Um, and wait for them and wait for them to press a key to play again, right? Which goes back to new, right? And if that loop ends, then we're going to then we're going to quit. Okay. And actually, here's a good point too where we can save ourselves some typing. Right up here where we import Pygame, let's change that to import Pygame as PG. Right? The as in an import lets you basically rename this. So we're importing Pygame, but we're going to say call it PG. So that way here I can actually just load PG run PG.quit instead of Pygame.quit. You think of all the times that we have to type the word Pygame. This is going to help make things a little easier to read and and type. Okay. So what controls this loop? Well, we're going to make a a variable. We're going to make a game variable. So, we're going to have to initialize that, right? So, that's going to be, right? So, now we have a a game object with a variable called running that controls whether this loop happens or not. Okay. And we're also going to need a show start screen and a show game over screen. right now. Most of those things will probably wait until uh until much later for us to fill in, but we can have them here. Um so they're ready to go. Okay. Okay. So, part of the reason I've done this is you would see that if we ran this program, it would run with no errors. It's not going to do anything, right? It's not going to do anything because there's no code to tell it to do anything, but it doesn't give us an error, right? If we hadn't define show game over screen with pass, then this line would give us an error and we would have to not do that until we were ready to do it. this way. We have these blocks here and they're ready to go. We can hide them, right? Like this because I'm nowhere near ready to do the code for my start screen, but it's ready. The place for it to go is ready. So whenever I am, I can go and drop it in there. And the same with all these other pieces. Okay, so let's start talking about how we move things over. Okay, so this stuff here, this is useful, right? This is the stuff that creates the game window um and gets everything started. So that pretty obviously needs to go into the init section. Right now we are going to want to replace pygame with pg. So I have a few places there where I can reset that. Right. Um these are object uh class variables. So, we're going to need to say self dot for the screen and self dot for the clock. So, now the game has a screen a screen and a clock item uh assigned to it. Um, we're going to probably want to create this all sprites group uh in the new, right? Because when you create the group, it creates an empty one, right? So that would let us do that. Okay. Now, so we've got our game loop and our game loop is going to be this, right? So this stuff is all going to go over here, right? What are the parts of our game loop? Well, we know we have to do the clock bit, right? We have to say self.clock.tick, right? And then we also want to do the pieces of our game loop, right? And actually what we really want to do is we want to have this game loop start and end kind of to be our um to be whether we're playing the game or not. Right? That's how we know that this when we start our new game it should end. Correct? So so we're going to need a variable um to control that. And we can say playing equals true, right? And if we set playing equal to false, the game ends. So while playing, what do we need to do? Um, that's where we need to take the clock. I put that in the wrong place. Um, and then we need to do self.events, which we've defined, self do.date, which we've defined, and self.draw, draw which we've defined. Right? So there's our game loop. A nice little compact game loop. Your game loop is to just have a a loop that takes the clock, checks for events, checks for executes the updates, executes the draw, and that's it. That's our def run. All right. So I'm going to hide that. And what else do we have? So we have our events, right? Events is going to go into this event section. So let's grab this and put that in the uh event section. Right? So for event inp.event.get if it's a pg.quit then what do we want to have happen? Well we want to end everything right? So if you are playing then we want to say uh we want to set playing to false and then we also want to set running to false because running is the command that ends this loop. Right? So you stop playing the game, you also stop running the whole program. you get out of everything. And that's our event section. So, as I fill these in, I'm just going to close them, right? Um, now update. We want to update the all sprites group, right? So, we'll put that into update. Uh, now these need to be named self. And that's our whole update section, right? Nothing else to put in there. And then we have our draw section. Our draw section just does these things, right? So, if I stick those over here, I have uh a couple of uh self.screen fill black all sprays. And then we want to uh do PG.flip. flip. Okay, so now we have all the pieces in here and our game should actually run. Oops, we have a little error. Says game doesn't have an attribute running. Uh, well, we said here, so here's where we set it to true. Ah, hopefully some of you noticed this typo. the init uh the init method needs to have underlines on both sides. It's double it's called a dunder in it double underscore. Okay, so actually I'm going to go ahead and close that since we're done with that. Um and I just noticed one other thing we forgot. Um running is what runs the game, right? So we there's two ways we could think about doing this. Right now, we're calling new to set the new game up, but we also need to say run the game. Now, you could do that or you could put um self.run here and say whenever a new game starts, it should just run itself, right? And that keeps things simpler here, right? And that lets us start the game, actually start the game loop. Okay, so there we have our game loop. Press the X. Uh program ends. So hopefully this whole process uh made sense to you. Um if it didn't uh let's walk through this one more time. So when we start up our program, it you know imports pygame and we just abbreviated that to PG. Um import random because we've we've had that in there because we know we're going to we know we're going to need it. And then import everything from the settings file which is where we put all of these things. How big the window should be and what our frame rate and colors and that kind of thing. And then if we go down here, right, because class game, I'll hide that for a second. It goes down here and says make a new game object. Well, what does the game object do? Well, when a new game object is created, it does this, which is all the stuff we've done before. Initialize Pygame, create the game window, set the caption, create the clock, and make a variable called running. Okay. So after you've done that, you should show the start screen. Well, show start screen is here. It's passed. So it means show start screen does actually nothing. Okay. So this does nothing, but it just skips to here. And it says as long as the running variable is true, and we set the running variable to true to start with, then you should create a new game. Well, what does that mean? Create a new game is doesn't do anything right now except create that all sprites group which we're going to use once we start making sprites. And run the game. And run the game right here says create the game loop. Make a variable called playing that controls it. And then take the clock check for events. Right? Right. The only events that we have defined here are to check for the clicking the X, which will make those variables false. Do the update, which all we have in update is to update the sprites. We don't really have any, but they're ready to go. And then do the draw, which fills the screen, draws whatever sprites we might have, and flips the display. Okay. If you did click uh the X, then you end the running loop. So you go to here. Uh you see or I'm sorry, if the game ends, right? We don't have anything that ends the game because we're not playing. Then you're going to see a game over screen. And game over screen is empty right now. If you had clicked the X in the events, we have set running to false. So this loop will end. and we'll just drop to pg.quit and everything is over. Okay, so next video we will start using this starting point, this main.py py to start building our platform game.

Original Description

This is the second game in our Game Development with Pygame series. It will be assumed you already watched the previous videos. In each video in this series, we'll add another feature to our platformer until we have a full game experience with graphics, animations, sound, and much more! Code for this part: https://github.com/kidscancode/pygame_tutorials/tree/master/platform/part%201 Links: Creating the Pygame template: https://www.youtube.com/watch?v=VO8rTszcW4s Other helpful links: * Installing Python: http://kidscancode.org/python-install.html * Installing Pygame: http://kidscancode.org/blog/2015/09/pygame_install/ * Setting up Atom: https://www.youtube.com/watch?v=uve1tjVIQ6c&ab_channel=KidsCanCode
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from KidsCanCode · KidsCanCode · 34 of 60

1 Learning to Code with Python: Lesson 1.1 - What is Programming?
Learning to Code with Python: Lesson 1.1 - What is Programming?
KidsCanCode
2 Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
KidsCanCode
3 Learning to Code with Python: Lesson 1.3 - Variables
Learning to Code with Python: Lesson 1.3 - Variables
KidsCanCode
4 Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
KidsCanCode
5 Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
KidsCanCode
6 Learning to Code with Python: Lesson 1.6 - Functions
Learning to Code with Python: Lesson 1.6 - Functions
KidsCanCode
7 Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
KidsCanCode
8 Learning to Code with Python: Lesson 1.8 - Number Guessing Game
Learning to Code with Python: Lesson 1.8 - Number Guessing Game
KidsCanCode
9 KidsCanCode - Patreon Intro Video
KidsCanCode - Patreon Intro Video
KidsCanCode
10 Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
KidsCanCode
11 Learning to Code with Python: Lesson 1.10 - Secret Codes
Learning to Code with Python: Lesson 1.10 - Secret Codes
KidsCanCode
12 Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
KidsCanCode
13 Learning to Code with Python: Lesson 2.2 Simple Animation
Learning to Code with Python: Lesson 2.2 Simple Animation
KidsCanCode
14 Learning to Code with Python: Lesson 2.3: Animating More Objects
Learning to Code with Python: Lesson 2.3: Animating More Objects
KidsCanCode
15 Learning to Code with Python: Lesson 2.4: More Fun with Animation
Learning to Code with Python: Lesson 2.4: More Fun with Animation
KidsCanCode
16 Extra: Setting up the Atom Editor for Python
Extra: Setting up the Atom Editor for Python
KidsCanCode
17 Game Development 1-1: Getting Started with Pygame
Game Development 1-1: Getting Started with Pygame
KidsCanCode
18 Game Development 1-2: Working with Sprites
Game Development 1-2: Working with Sprites
KidsCanCode
19 Game Development 1-3: More About Sprites
Game Development 1-3: More About Sprites
KidsCanCode
20 Pygame Shmup Part 1: Player Sprite and Controls
Pygame Shmup Part 1: Player Sprite and Controls
KidsCanCode
21 Pygame Shmup Part 2: Enemy Sprites
Pygame Shmup Part 2: Enemy Sprites
KidsCanCode
22 Pygame Shmup Part 3: Collisions (and Bullets!)
Pygame Shmup Part 3: Collisions (and Bullets!)
KidsCanCode
23 Pygame Shmup Part 4: Adding Graphics
Pygame Shmup Part 4: Adding Graphics
KidsCanCode
24 Pygame Shmup Part 5: Improved Collisions
Pygame Shmup Part 5: Improved Collisions
KidsCanCode
25 Pygame Shmup Part 6: Sprite Animation
Pygame Shmup Part 6: Sprite Animation
KidsCanCode
26 Pygame Shmup Part 7: Score (and Drawing Text)
Pygame Shmup Part 7: Score (and Drawing Text)
KidsCanCode
27 Pygame Shmup Part 8: Sound and Music
Pygame Shmup Part 8: Sound and Music
KidsCanCode
28 Pygame Shmup Part 9: Shields
Pygame Shmup Part 9: Shields
KidsCanCode
29 Pygame Shmup Part 10: Explosions
Pygame Shmup Part 10: Explosions
KidsCanCode
30 Pygame Shmup Part 11: Player Lives
Pygame Shmup Part 11: Player Lives
KidsCanCode
31 Pygame Shmup Part 12: Powerups
Pygame Shmup Part 12: Powerups
KidsCanCode
32 Pygame Shmup Part 13: Powerups (part 2)
Pygame Shmup Part 13: Powerups (part 2)
KidsCanCode
33 Pygame Shmup Part 14: Game Over Screen
Pygame Shmup Part 14: Game Over Screen
KidsCanCode
Pygame Platformer Part 1: Setting Up
Pygame Platformer Part 1: Setting Up
KidsCanCode
35 Pygame Platformer Part 2: Player Movement
Pygame Platformer Part 2: Player Movement
KidsCanCode
36 Pygame Platformer Part 3: Gravity and Platforms
Pygame Platformer Part 3: Gravity and Platforms
KidsCanCode
37 Pygame Platformer Part 4: Jumping
Pygame Platformer Part 4: Jumping
KidsCanCode
38 Pygame Platformer Part 5: Scrolling the Window
Pygame Platformer Part 5: Scrolling the Window
KidsCanCode
39 Pygame Platformer Part 6: Game Over
Pygame Platformer Part 6: Game Over
KidsCanCode
40 Pygame Platformer Part 7: Splash & End Screens
Pygame Platformer Part 7: Splash & End Screens
KidsCanCode
41 Pygame Platformer Part 8: Saving High Score
Pygame Platformer Part 8: Saving High Score
KidsCanCode
42 Pygame Platformer Part 9: Using Spritesheets
Pygame Platformer Part 9: Using Spritesheets
KidsCanCode
43 Pygame Platformer Part 10: Character Animation (part 1)
Pygame Platformer Part 10: Character Animation (part 1)
KidsCanCode
44 Pygame Platformer Part 11: Character Animation (part 2)
Pygame Platformer Part 11: Character Animation (part 2)
KidsCanCode
45 Pygame Platformer Part 12: Platform Graphics
Pygame Platformer Part 12: Platform Graphics
KidsCanCode
46 Pygame Platformer Part 13: Improved Jumping
Pygame Platformer Part 13: Improved Jumping
KidsCanCode
47 Pygame Platformer Part 14: Sound and Music
Pygame Platformer Part 14: Sound and Music
KidsCanCode
48 Pygame Platformer Part 15: Powerups
Pygame Platformer Part 15: Powerups
KidsCanCode
49 Pygame Platformer Part 16: Enemies
Pygame Platformer Part 16: Enemies
KidsCanCode
50 Pygame Platformer Part 17: Using Collision Masks
Pygame Platformer Part 17: Using Collision Masks
KidsCanCode
51 Pygame Platformer Part 18: Scrolling Background
Pygame Platformer Part 18: Scrolling Background
KidsCanCode
52 Pygame Platformer Part 19: Wrapping Up
Pygame Platformer Part 19: Wrapping Up
KidsCanCode
53 Gamedev In-depth Topics: 4-way vs. 8-way Movement
Gamedev In-depth Topics: 4-way vs. 8-way Movement
KidsCanCode
54 Gamedev In-depth Topics: Time-based vs. Frame-based Movement
Gamedev In-depth Topics: Time-based vs. Frame-based Movement
KidsCanCode
55 Gamedev In-depth Topics: Non-integer Movement
Gamedev In-depth Topics: Non-integer Movement
KidsCanCode
56 Tile-based game Part 1: Setting up
Tile-based game Part 1: Setting up
KidsCanCode
57 Tile-based game Part 2: Collisions and Tilemap
Tile-based game Part 2: Collisions and Tilemap
KidsCanCode
58 Tile-based game Part 3: Smooth Movement
Tile-based game Part 3: Smooth Movement
KidsCanCode
59 Tile-based game Part 4: Scrolling Map / Camera
Tile-based game Part 4: Scrolling Map / Camera
KidsCanCode
60 Tile-based game Part 5: Player Graphics
Tile-based game Part 5: Player Graphics
KidsCanCode

Related Reads

Up next
How To Build A Twitter Clone - React Next JS - Appwrite Crash Course
Adrian Twarog
Watch →