Window & Gameloop | Coding Terraria in Python | Pygame Tutorial Ep. 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello welcome to the first episode in a new tutorial Series where I'm going to be showing you how to create Terraria in Python this tutorial series is going to be using the pygame library for this um it's not going to be a Python tutorial it's going to be more of a game development and Pi game tutorial so I do assume that you have like a little bit of knowledge of python you don't have to be a master I expect you to know at least like beginner to intermediate level stuff anything above that I'm going to go ahead and explain for sure so yeah with that being said let's get into it so the first thing you want to do is install a pi game if you haven't already the way you can do that is by simply opening up a terminal and then in this terminal here you want to just run pip install piping and mine says requirement already satisfied but yours is going to do a ton of loading and downloading um in on Mac you do pip 3 install Pi game all one word for pip3 and if your pip is broken like mine was you can do PI Dash M install Pi game and that could work as well one of those three should work and now you have Pi game on your computer so very cool very cool now to get started so the first thing we want to do is we want to create our entry point which means where the program starts and we're going to also put our game Loop there so here we're going to create a main dot pi and this is going to contain our game Loop and stuff like that so first we want to do is import pygame so that we have we're able to use Pi game in this file we want to also import system and now we are going to use an object oriented approach to this game and the reason is because while it is under a lot of criticism I do still believe that for most people creating a video game the best way to do it is object oriented method I think that doing it functionally especially for beginners can be very very difficult so just using an object-oriented structure will make it a lot easier um and most games anyway still do that so with that being said let's go ahead and create our game class so so let's create a class called game and then we're going to create a Constructor which is the dunder method in it which means double underscore so definite press Tab and you'll have your Constructor here and now this has this arrow with none if you haven't worked with Optical into programming in Python this just means the return type it's being explicit it is not required you can just get rid of that if it makes you feel better and this is all the same thing now you have your Constructor and so everything that we write inside of our class here for the most part is going to be have the self a keyword in front of it and this means that it is this this value is unique to this object so we're going to do a couple things here so first we're going to initialize Pi games so we're going to do pygame.net which initializes Pi game then we want to create our screen and for this we're going to store it in a variable because we're going to be using the screen a lot so we're going to do self.screen equals pygame dot display.set mode and this takes in a tuple argument with the width and the height I'm going to do 1280x720 um just in case you haven't worked with our I'm just going to programming like I said the self means that it is unique to the object of this game class so if we make three objects of this game class um we can have three different screen variables for each one um each one has one of them so with that being said let's go ahead and get the basic structure for our game down um so for this we're going to do a a new method this is going to be the run method and we're going to pass it for now meaning we'll get back to it later and then we're going to do an update method give itself pass it for later um and then a draw method also give itself and pass it for later and then give a close method and pass it for later and this is just the basic structure of most games so now we're going to create a couple things here so the first thing we're going to do is we're going to make a Boolean value and this Boolean value is going to be running so self dot running and when we create the game object we automatically want it to be true so we're going to set it equal to true and then in the run method this is where our actual game Loop is going to be we can do what we're going to do is we're going to do while self.r running then we can do update software update and self.draw so this is basically saying that while running is true we're just going to update and draw this every single frame and so now what we can do is we can do a couple more things so um don't go ahead and do not run your program yet because if you do you're going to have a couple issues one we're not updating the display and so let's go ahead and update our display so for that we're going to do pygame dot display dot update and it automatically knows to point to this screen because we set the mode to this screen and now we want to do one more thing because right now Pi game does not know it doesn't actually know if you want to close the screen or not so if you ran it right now if we had an entry point properly set up you would see that you can't close your display and so the problem is that pygame doesn't know that you want to close it so you have to explicitly check if you have a close event happening and then if you do then actually close the display so for this we're going to pull the events and so that's just a simple for Loop so we do four events in pygame Dot event.gets if event DOT type equals python dot quits then what we're going to do is we're going to set running equal to false equal to false and this is going to break us out of this while loop here and then we are going to go to this close and so here we're simply going to put self.close at the bottom of our run method here and then in the close what we're going to do is we're going to do pygame.quitz and we're going to do system.exit this just makes sure that we are fully out of our PI game window here so the last thing to do real quick for this simple setup is to just check if underscore underscore name underscore underscore equals um underscore underscore Main then we can do game equals game which we're creating a game object and we do game dot run and this should create a very simple display there you go we have a cool little display and you might be wondering like why did we do all this extra work here we could have done it in like a few lines of code this is because it's good to have good architectural practices when you first start out because the more you build up on this the harder it's going to be if you you know write a ton of sloppy sloppy code so I thought it would be a you know a good idea to jump start you with the right practices so now a couple more things let's uh put a color on our screen so we're going to do self touchscreen dot fill light blue now we have a cool light blue color which is all good um and you'll notice that I wrote that in the draw method it's a good practice while it is not required it is a good practice to separate your updating logic from your drawing logic of course we could accomplish all of this in one single method but I do it's just not a good practice it's it's a better practice to separate this out and so with that being said let's do a couple more optimizations or a couple more organizational things so the first thing is we have these hard-coded variables here 1280 720. um and this is not um what we want to do because maybe we want to change the size of this screen here or something like that we want to actually have this very as a variable that we can change easily so for this we're going to create a new file and I'm going to call it globals.pi and this is going to take in this is going to have the screen with equal to 1280 and the screen height equal to 720. notice how I'm writing them in all caps this means that they are constants um so while this might not be the case we might make it so you can adjust the screen size um it is it we're just defining them as constants for now because and what the convention for this is all caps of course they aren't actually constants you could change them but when you see an all caps variable like this it means don't touch it so now what we can do is we can say from globals import all and then instead of these we can do screen width and we can do screen height and then there's one last thing that we can do here so while we can't see it um the FPS whenever we run this is going all over the place and so what we want to do is regulate the FPS on our game this is for a lot of different reasons for like calculations and for physics and stuff it's just better to have a consistent frame rate so for this we're going to create a clock using pi games built-in clock so we're going to do self.clock equals pygame.time.clock and this is just creating a clock object now all we do is in the update we're going to do self.clock dot tick and then this takes in the frame rate and instead of just writing in like 60 here I'm going to actually go to my globals and I'm going to define a uh an FPS constant of 60 and change this to FPS here so there you go that is the proper professional setup for your pygame window so that is the end of tutorial one the next one we're going to get into uh basic drawing and Sprite group stuff so yeah thank you for watching see ya
Info
Channel: Coding with Sphere
Views: 4,964
Rating: undefined out of 5
Keywords: coding, terraria, python, pygame, minecraft, game dev, dev, software engineer, computer science, tutorial, gaming, game development, terraria lets play, terraria tutorial, indie game dev, game engine, game series, coding tutorial, pygame tutorial, pygame game, pygame game example, indie games, best game engine, unity, godot, how to make games, how to code games
Id: Iz6alOMjNkk
Channel Id: undefined
Length: 9min 18sec (558 seconds)
Published: Wed Jun 07 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.