hello everyone and welcome to the first episode in the pie game rpg tutorial series and in this series this is what we're going to be making as you can see uh we've got this little character that we can move around uh we also have collisions and we have enemies and we can kill the enemies and we also have a camera that follows us around and when we touch the enemies we die so that's what we're going to be making in this series and it's going to be made with pie game and python so let's get right into it first things first we're going to want to install pygame and you can just do that by going into command prompt and once we've got command prompt open we can just type in pip install pi game and then it's going to say for me requirement already satisfied because i've already installed it but once we've got pie game installed we want to set up our project directory so i've just made a folder here called pygamerpg and this is where all our code is going to be stored as well as any images that we have we're going to want to create three files we're going to want to have main. py then we also have config. py this is going to be like all the settings for our app so any variables that we might have and then finally we'll have sprites dot py and sprites dot p y is going to be where we can store the classes for our sprites we also should create a folder called img and that's where our images are going to be stored um and now let's open up these in a text editor i'm going to be using sublime text you can use whatever text editor you want but i'm just going to be using sublime okay so in our main.
py file here firstly we're going to want to import pi game so import pi game and then we're going to also want to import everything from sprites. py and config. py obviously these are empty but we still need to import them when we write in them later so we can just do from sprites import star which means all and then from config import import all and then we're also going to want to import sys which is system okay now that we've got that set up we're going to start building up our game class and this is going to start literally everything that our game has it's going to bring all our code together it's going to be like the main class basically so we can have a class and call it game and we obviously have our init method so define init and it's going to need self once we've done that we need to do pygame dot init and that is a function that is part of pygame basically it just initializes pi games so we can start using it you need to run pygame.
net whenever you create a pi game program then we need to create our screen so we need to do self. screen and we'll set it to pygame. display dot set underscore mode and here we're going to have a tuple and we'll have it to win width and win height we don't have win width or win height defined yet so we can do it in config.
py and we'll do win underscore width equals uh let's do 640 and win height is 480. because we've imported everything from config we can just use these variables right here straight out of config so basically pygame. display.
set mode it creates the game window for us and with the width is uh 640 and the height of it is 480. then we'll do self. clock equals pie game.
time. clock and basically what what the pie game clock is it allows us to set the frame rate of our game if you don't know what frame rate is it's how many times the game updates per second and we can also do self. font equals pygame.
font. font and i'm going to set it to ariel or ariel and we'll have 32. we're not actually going to be using this self.
font yet i'll explain what it does in a later video but we'll just put it there for now because we're going to need it later and then finally we'll do self. running equals true and now let's create a method called new and this is going to be called whenever we run our game this is basically just going to set apart all our variables for our game we'll have define new and itself we'll just comment it here saying a new game starts self. playing equals true these two variables here will be useful when we want to see if the players have if a player has died or not or if the player has decided to quit the games i'm just going to write out a few empty functions and i'll explain what they're going to do once i've done it so we can have define update self then we can also have define draw self we can have define main self we'll also have define game over self and define intro screen self okay so these are all our functions that we're going to be using in our main game class so let's just go back to our new function here we want to set up some sprite groups in our new method um basically what this is it's a it's a group of sprites that we can control easily it might not make any sense of a moment but hopefully in a bit it will make some sense so we do self dot all sprites equals pie game dot sprite dot layered updates and what this is it's a object that's going to contain all our sprites in the game so it's going to contain our character it's going to create it's going to contain all the walls that we have in our game it's also going to contain all the enemies and by having one group of sprites it's basically going to allow us to update them all at once then we can have self dot blocks equals pie game i'm just going to copy and paste this self.
blocks and self. enemies and self. attacks so self.
blocks is going to be where all our walls are stored um self. enemies is going to contain all the enemies that we have in our game and self. attacks is going to be all the attack sprites that we have so you know like the attack animation when we attack an enemy basically uh so these are just all empty groups at the moment that we can add our sprites into later and then we'll do self.
player this is going to be our player object and is going to be a player uh we don't actually have a player class defined yet so we should go into sprites dot py and start coding out the player uh so we're going to need to import pi game of course uh and also from config import all so we're going to be importing everything that's in here and then we also need import math and import random uh these will need these modules later they come pre-installed with python so you don't need to worry about that uh and then let's make our player class and it inherits from pygame. sprite. sprite basically this pie game.
sprite. sprite it's a um it's a class in the pie game module that basically makes it a lot easier to make um sprites basically our player is now a sprite so we need an init method we have self we're also going to need game so basically by passing in game here we'll be able to access all the variables here uh we'll also need to have an x coordinate and a y coordinate to set whether play is going to appear on the screen uh we'll have self. game equals game self dot underscore layer equals player underscore layer so this self.
underscore layer here um notice back here in our sprite groups we have this pygame. sprite. layoutday by setting the layer we can tell pygame in what layer of the screen we want the sprite to appear we might say that the grass of the game is going to be at the bottom then the rocks and then the player over top so the player will be um drawn above everything this player layer we haven't defined it yet so let's define it in config.
py player layer equals let's just set it for one to the moment because we haven't got any we haven't got any other layers but for example say we have like ground layer which is where our grass would go uh we might want that at one and two so the ground layer gets drawn first and then the player layer gets drawn on top of that so i'm just going to keep player layer at one for the moment um self. groups equals self. game.
all sprites okay self. groups by setting for self. groups we're basically adding in the player to the all sprites group and we're able to access the all sprites group because we're going to pass in the game as an object and then we'll do pygame.
sprite. sprite dot underscore and score in it and we need to pass himself as well as self dot groups and this is just going to call the init method for the um inherited class and by passing in the groups we're adding the player to the all sprites group now we can do self. x equals x and then we'll multiply that by tile size as you saw earlier it's a tile based game so the tile size of every tile in this game is going to be 32 by 32 so this player right here is 32 pixels by 32 pixels and that enemy is 32 pixels by 32 pixels so we're going to need to define tile size in config.
py and i'll just do it here tile size equals 32. we're setting self. x to the x that we've passed in here times the tile size solved.
y equals y times tile size and self. width equals tile size so the width is going to be uh 32 and the height is also going to be 32. now we need to set the image of the um player class and for the moment it's just going to be a rectangle so we can do equals pie game dot surface and then we pass in a list here and we'll have self dot width and self dots height so by creating a surface here we're basically creating a rectangle which is 32 pixels long and 32 pixels tall and then we're setting that rectangle as the sprite image so basically our sprite is going to be a rectangle which is 32 pixels wide and 32 pixels tall and then we're going to fill the image with a color with the color red so now we'll get a red rectangle as our sprite image and we can just set the red to two five five zero zero if you don't understand what this means this is rgb i'll summarize it quickly but basically this is how many um what amount of red we're going to have in the color this is how much green we're going to have and this is going to be how much blue we're going to have and it's on a scale of 0 to 255 so we're going to have the maximum amount of red we can have we're going to have no green and we're going to have no blue and then we're going to set the self.
rex and basically every sprite in pygame has an image and it also has a rect so the image is what it looks like but the rect is like where it's positioned and how big it is basically this is what it looks like and this is the rectangle like like the hitbox basically so we can do self. image. getrex by doing self.
image. direct we're basically just setting the self. rect to the same size as the image so the hitbox and the image of the player are the same and then self.
rect. x equals self. x and self.
x dot y equals self. y we're going to tell pygame the coordinates of our rectangle and also every pie game sprite also has to have a method called update it's going to need self and we'll just pass so that's it for the player class we can leave this now and we can return back here what parameters did the player class have it had self we can just ignore that it had the game object and add some coordinates so to pass the game object we can literally just type in self because we're passing in this class of game which is self we can have it as one and two its coordinates is going to be one on the x axis and two on the y axis but because we're multiplying it by tile size it's going to be positioned 32 pixels on the x axis and 64 pixels on the y-axis so that's our player object created there and we also need a events method i forgot to add this in earlier so now how about we go into our main method which is going to be running constantly while we're playing our game so in the main method we'll just comment this game loop so every game needs a game loop so we can have while while self. playing so while self.
playing equals true we want to call the method self. events we want to call self. update and we also want to call self.
draw and then if self. playing is ever false if we ever stop playing then we're also going to set self. running to false as well so our game is going to have three parts to the main method it's going to have the events method it's going to have an update method and it's going to have a draw method the events method is going to contain everything like our keypress events so when we um type on the keyboard or whatever update basically going to update the game to make sure that it isn't just a static image and then draw is going to display all the sprites onto our screen so going up to events so i'll just comment this game loop events so we can do for event in pygame.
event. get so it's a for loop and pie game. event.
get is going to get every single event that happens in pie game and we're going to iterate over that list and then if event dot type equals pygame. quit self. playing is going to be false and self.
running is also going to be false so basically pie game dot quit the pie game quit method is when we press the close button so it's constantly checking to see if we press this button or not so if if the event type was a quit event once we press that we stop playing and we stop running okay so that's it for the events method now for the update method uh game loop updates self. all sprites dot update so self. all sprites is that sprite group that we had here this layered updates and this layered update object it contains a method called update and basically by calling self to all sprites to update it's going to find the update method in every single sprite that's in that group and it's going to run this method so self.
all sprite start date going to look in this group and go through every single sprite in this group and it's going to call the update method in that sprite which is this down here and now on to the draw method so firstly we're going to just want to fill the screen with black so self. screen uh self. screen is our screen that we've defined up here with win width and win height uh self.
screen. fill and we can fill it with black so it'll just be a black window at the moment but then we can do self. all sprites dot draw self.
screen and what this does is we're referring to this all sprites sprite group again and it's going to call the draw method and what this draw method does it walks through every single sprite in the all sprites group and in our case it's just this one player it finds the image and it finds the rectangle and it draws that onto our window so once we've drawn all our sprites um we're going to want to do self dot clock dot tick self dot clock dot tick and if you remember earlier i said self dot clock is going to allow us to set our frame rate so how many times the screen updates and then here we can just pass in fps and here i'll set a variable called fps and set it to 60 that's 70. 60. so we're going to update the screen 60 times per second with this clock.
tick method and then finally pygame. display. update and this pygame.
display. update it will actually just update the screen and then game over we can deal with this later in a different video so that's it for our game class uh now we actually need to convert this class into an object so we can say g equals game and it doesn't have any parameters and then we can do g dot intro screen so as soon as we run this file it's going to create the game object and it's going to run the intro screen method but because the intro screen doesn't have anything in it it's just going to skip over it and then it's going to run the g dot new method so g dot nu if you remember it's going to set self. playing to true is going to create all the sprite groups and it's going to create our player object so this is going to be called whenever we start our game and then while g dot running while this self.
running is true what we want to do is we want to do g dot main and g dot main it's got its own loop inside of that and while while while we're playing the game we want to run the events method the update method the draw method and once this loop stops we'll set self. running for false then we'll break out of this loop and then we can call the method pygame. quit so we'll quit out of our game and we can type in syst.