 How's it going everybody? Welcome back to another Pi Game and Python programming tutorial. In this video, we're going to get into something super duper cool that makes your game and your code and your program kind of look a little bit more like a platformer because we're finally going to be introducing the scrolling platformer where it kind of looks like you're moving around a world that will follow you as you progress further and further through the level. So the way that we do that is pretty simple but first you have to understand this concept that this doesn't really happen with the player like the player will keep moving obviously but the player doesn't move further and further through the screen because the screen is a static defined like width and height you know at the very at the very start of our program when we have our main function we're setting the window width and the window height to 640 and 480. Obviously that can't change so well if you have that rationale how does this happen the player can't move further than the dimensions that we kind of allocate for it so how does it look like we're moving all around this world that is apparently bigger than what we can see? Well here's a fascinating thing and you might kind of resonate with it because often people will tell you like hey John the world doesn't revolve around you shut up stop your whining but the thing is in two-dimensional games and in pi game and the way that we're actually building this that's kind of the way that it works the world does revolve around you the world does revolve around me the world does revolve around the player now when I say that that means that as the player gets closer and closer to one edge of the screen if we move further and further along everything in the world will move according to how the player is moving and there's a way that that actually all happens so it's not in our player object it's not obviously in the other blocks or the way that we do things in the other platforms and things in the game it is in the level it is in the world it's in the way that we're actually inherited and inside the map so in the constructor of our level let's set up a few new variables I'm going to call mine self dot world shift and I'm actually going to call mine shelf dot world shift x because let's work with the horizontal direction first right now that's going to be zero and this variable this world shift is going to equal the change or how far we've really moved according to the original position in our world right now this is zero now scrolling we actually kind of have a view and that's that box or the the square that you see the world through that's following the player now if we have boundaries like if one player if the player moves too far to the right side of the screen if it crosses that right boundary it'll start to move so we this of course applies for the for left and for up and down and that sort of thing but the point that I'm trying to get across to you is that there are boundaries there is a view box that we're actually kind of setting up here now I'm going to set mine kind of relative to the size of the screen so let's set this up in at least in the horizontal direction let's say self left view box is going to equal to like I said I'm going to be using the these things relative to the screen window width divided by two let's have half of that divided by window width divided by eight so half of the screen subtract a little bit I'm going to use eight not 18 sorry subtract a little bit more of whatever the window width actually is now when I use right self dot right that's going to be window width divided by two plus window width and then I'll actually use 18 no in fact I'll use 10 because 18 is kind of very very tiny this now allows for less change in the right direction and more change in the left direction because the game kind of moves to the right at least initially that's the way that I kind of at least envision a game in a platform where you always seem to seem to have the player moving in the right direction as they progress further and further through the level so that's all the variables that we need so far we need a world shift that'll keep track of how far we've really changed things and this left view box and this right view box that are going to act as our boundaries okay let's continue our update function will work just fine and our draw function will work just fine but what if we gave this another function what if we had a function that was like shift world I would call this change world but then it'd be like yeah my my program changed the world look guys I made a funny joke we have a we have a shift function we have a shift world function and right now we're going to have that shift it's going to take a variable called shift x once we continue we are going to have it have shift y but we're also going to have a world shift y along with that so for now we're just going to use shift x I will actually show you how to use shift y in in this same video it's it's very very simple you're practically just duplicating the code but world shift x we're going to work with our self dot world shift x we're going to add whatever we're shifting shift x the variable that we just created shelf dot world shift x we're going to keep track of that variable in this function we're actually going to add whatever we passed to the function now this is where we actually have to keep control of all the other objects and platforms in the level so we do that by looping through all of the objects I'm going to say each object so it doesn't look like a type for each object in this level in the self in this level's object list we're going to say each object its actual rectangular position its rect x is going to be added to by of course shift x nice right and that's all that we actually have to do for now but that's what's shifting everything in the world we have to keep track of whether or not the player has moved through the boundaries or not that's going to be another new function I'll call mine run view box you can call yours whatever you want maybe it can be like scroll or run camera it's whatever it's just going to take one argument obviously self because it's it's an object and this is where we actually test the player's position now because we have access to the player in this level we also have a we have a self dot player object that's what we're going to use self dot player object it's x position if it's less than or equal to self dot left view box well guys what do you think we should do I know you can guess it we're going to have a view difference and that view difference is the change it's going to be the offset the difference between our view box and the player's x position so I'm going to copy this right up here because I don't really want to type all that okay I guess I'm not going to copy that fml let's try one more time copy that code paste it right there this is the difference this is the change between how far we have we are able to go to the left and how far that we have gone to the left now this is what's going to make up for the world difference we're actually going to run our self dot shift world with this view difference but how do we stop the player from moving that far to the left well we set up that view boundary didn't we we have the left view box and I'm sure you guys know how this works let's actually stop the player in his movement let's say player object dot wreck dot x can equal the left view box he stops at that boundary nice now what about the right direction well it's the same exact code except it's the other way around if it's greater than the right view box well now we're going to be using the right view box over and over and over again rather than left it's going to be right but we're still changing with the world difference cool now that all that has happened we don't have any more code to run in the level object or the level zero one object but we do however need to incorporate this view box in our game running loop so remember we added this section or this kind of category for the logic and the things that actually happen well let's do current level run view box let's see what happens run the code hopefully no errors hopefully hopefully hopefully no errors yeah all right cool so now we've got my little character here I can move left and right and oh the whole world moves around him it almost looks like there's a camera following him and I jump down whoa I'm over here now well when I jumped when I jumped down we didn't see any change vertically why is that I can still jump off the screen whoa why is that why isn't the camera following me keep in mind we didn't actually add support for the vertical direction yet but this is super duper easy right we just we just wrote all the code for this all we have to do is add a few new variables and a few new tests self dot shift y all right yeah that can equal the same thing initially that's going to be zero let's add some new variables let's say okay we've got we've got left and right what if we had up and down that's of course going to be using the world height the window height and I'm not I don't think I'm going to be using um up up is going to be four I'm not going to subtract me from from that and let's say down can equal white dot two plus window height dot 12 divided by 12 okay I think those are some good numbers you can play around with it and test for the camera that you really want but for now that works just fine for me and in our shift world let's add that new variable let's go shift dot y shift underscore y and do the same sort of manipulation that we have here do the same thing in the y direction in the run view box we have new tests to make test for left uh now that we've tested for left and right let's test for up and down if it's less than up you know what we do we use up y y up and keep in mind since we're using a shift world function now that we just added a new variable for that we're not going to do anything in the y direction sorry in the x direction we are going to do things in the y direction we have to account for this in the other function calls that we've done this is all in the x direction not in the y direction so these are going to be zero x direction not y in this case y not x let's make the change for the other one self dot down view box super cool super duper cool all right now let's run this code see what happens boom okay cool now it looks like my screen is kind of blank it looks like oh i can jump in everything cool it looks like i'm only on one platform but we know we can move around and the camera will follow us we know because we have that other one down there if we jump down wow i overshot it i jump past it i'm dead now you saw the the platform just fly by us because the camera is happening and we're of course subject to gravity let's try one more time hopefully i can make the jump this time here i am in my in my world i can jump down we and i can land on this block over here i'm actually going to maybe tighten the down difference or up difference i don't know what's when i want to work with but what if we did like when i jump i want it to move back down i want it to like we did eight if i come into that whole section i wonder if i just went halfway cool that works kind of well i think this down to three i can jump up and jump back down so it looks like i'm moving higher it all depends on the kind of game feel you want your game to have maybe that's too much for you maybe that's not enough maybe i can go five and go back go back 10 the way that we had it before or 12 it's it's entirely up to you and that's the best part about programming your own thing is that you can make this however you want it and i can of course jump down to other positions and we can add as many blocks as we'd like to the world because of the setup that we added and how we organized the code beforehand and now we have a scrolling window scrolling platformer cool thanks for watching guys hope you enjoyed this i hope you understand everything that we covered in this video and uh we're going to move into some really cool stuff next so thanks see you again