Newbie 2D Game Development in C with Raylib

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi there recently I started to learn the C programming language and also recently I found out about the ray lib graphical library for C and I started to make a game with it and it was surprisingly simple so pretty quickly I was able to create this little beginning of a game where I can jump with this ball and I can jump over these platforms here so I can go over here I can jump on this and I can jump over there now this has some bugs but it kind of works as you can see it's bouncing around very weirdly but still I was able to do this very quickly so I thought let me document this learning process maybe it will be beneficial for someone or educational or entertaining or all of the above so let's try to make a game in C with railing so I'll close this game and I will start over from scratch so the first thing we have to do is we have to actually install railib so how we do that is we go to arraylip.com website and we have the code here somewhere we click here and we get to the GitHub repository of relief now at first I thought that it would be just as simple as just downloading this file this relief.h and then including that in my main.c but it didn't really work that way I had to do some other extra steps but let's now actually see if it works now because I already installed all the dependencies so let me CP from downloads the railib.h here and let's make a new file main.c and here we will include rayleap.h and then we will say int Main and we will return zero and then we have to add some boilerplate here now I don't remember anymore how it worked something like set FPS now Rayleigh is cool in that we can actually open this relief.h and then we can search from here so we just search for set FPS FPS set Target FPS so this is what we have to call so let's call that set Target FPS now I wonder why I do not have any order completion here maybe I have to save this file first yes now I think I have it so let's put here 60 FPS then we had to do something else we had to make a while loop when not window should close then we do something and then we have to say something like begin drawing and then we have to say end drawing and between these we can put our code that will draw stuff on the screen now I think there was something else I think there's like a set window let's go to reddit.h and say window init window that's it so let's go back here and say init window and we have to say the width let's do 800 by 600 and we will set a title my game and perhaps that's all then we can draw like a ball in the middle or first we have to clear the screen so that it will fill it with a white background so we can call clear clear background and we get the color and Ray lip has nicely or not so nicely used things like red for a red color it is defined somewhere defined red so we have all these colors here defined and we are going to use white and there's a ray white as well that they're using examples and apparently it's not completely white so we can just use y so let's say white so this will make a white background and then we can draw a ball here so draw Circle 3D not 3D just circle and we pick a center for the circle so we can in fact do something like this let's set here in window width or just say with 800 and int height is 600 and then we can pass these into this one so this will be width and this will be height and then we can do something here if we want to put the circle in the middle of the screen we can say that the position is width divided by 2 and height divided by two and then we will have the radius so let's actually say ball size and let's have a color here let's call this red and let's define the ball size here so let's say int no little float float ball size is let's say 40 pixels and maybe we have to do like point zero not quite sure about that so for some reason I have some squigglies here float ball size okay vs code is just being slow so now everything should work now that might be all so let's actually actually try and compile this and see what will happen but we have to use some special Flags when we compile this so let's in fact do code build.sh and let's put our build script here so we will say that gcco game main dot C and it was something like El Rey lip that's a flag we have to pass something like this let's see what will happen so I will say first I have to mod plus X this build.sh and then I can do build dot sh and actually we didn't get any errors that is amazing so let's see if it actually works I don't think it will work it actually works so you can just use it like this so you just include the relief.h but I had to do some installation before it worked like this or was it just the L Ray lib because before when I tried this I like cloned the whole repository and then I had to do like make and make install and all kinds of stuff well now it works just like this so if you want to try this yourself and you have problems with it then you can go to the rally website and you can go to the wiki and here you will have different kind of instructions for different operating systems so if you are on Linux you can go to a new Linux and yes I had to do exactly so I installed C make and then I installed all these libraries and then I use this build gray Loop using May and I cloned the repository and with all of this and I had to use this shared one because the others didn't work for me for some reason so this is the place you have to go if you want to figure out how to install this if it doesn't work directly by including the arraylip.h in your project so I'll actually copy this and I will save it I will say code description dot text and I will save here Ray lib installation so that I remember to put it in the description of this YouTube video okay so now we have a game already well it's just this game and we have a red circle in the middle of the game so now we should add some kind of gravity so that the ball moves down now I'm a simple man I will just do it like this I will add an integer of ball velocity and I will set it to four let's see and let's actually move this width divided by two and height divided by two into their own variables so this will be ball X and ball Y and I will set them here as well end ball X is this an in ball Y is I divided by two and then here in the loop I will just say ball y plus equals ball velocity so this should increase the ball y position which will make it move down with every single frame so if we do that and then we build and we run it then we should have a ball that's moving down that's great now of course it just moved off screen so we have to do something about that and it was actually moving down in a constant velocity so I would like to add to the velocity every time so it speeds up so I can say something like ball velocity plus equals one now maybe I should use a float here so that I don't have to increase it by one with every frame but let's see what will happen now okay it moves very quickly so now we should detect somehow when the ball hits the bottom of the screen so how do we detect that well we could do something like if so let's check if the bottom of the ball is at the bottom of the screen so how do we get the bottom of the ball well let's say int ball bottom is ball y plus ball size because it is round and this is the radius so the ball Y is the middle of the ball and then we add the radius so that will be the bottom of the ball so we can say if ball bottom is greater than the height now I might use window height here window height sorry width and window height so that makes a bit more sense so if the ball bottom is greater than the window height well greater than or equal to then we should stop the ball from moving so we can just say ball velocity is zero and I think that we should also move the ball to the bottle because if the velocity is such that it will jump over the bottom then we want to move it back so that it doesn't get stuck like too low so we can say Vault y equals window height minus ball size all size so we get the bottom of the window minus the radius of the ball because if we put the ball in the bottom of the window then it will be from the middle of the ball so we have the minus ball size so let's try and run this and it stopped there it was very fast so now we should make the ball be able to jump let's do that so in order to do that we have to say if key pressed is key pressed and we provide the key and again we can use the down sorry space key space if we press the space then we should make the ball velocity negative so it will move up and then because we are changing the velocity we are adding to the velocity every time then it should automatically start to slow down so if we do something like ball velocity is 30 then it will start moving 30 pixels every time but with every frame it will slow down and finally it will start moving down so if we do that and we build and run this game again then we can jump or not it did something but not quite sure what is happening there so sorry I have to put minus 30 of course so build it and now we can jump and that's a very big jump okay that's great so now we can jump so in order to increase the gravity we can add to this number so we could actually call this gravity now I wonder if there is some gravity thing in raily built-in int gravity is Network 3 maybe that is better so that's too much we might have to use like floats here because this is moving too fast now we can jump okay that's pretty nice so now we can jump with this ball but we cannot move left and right so let's do that next so we will just add here if is key pressed left it is probably key left yes if we are pressing left then we should say that the ball x minus equals let's say four and then we will do the same thing with key right key right and the ball X plus equals four so now if we run this then now we can actually move but I have to press like I can't press and hold I think there's another thing we can use it's his key his key his key down yes the Press only does it once but if we say is key down then it will repeat this all the time that it it's down so let's run this and then now we can move left and we can move right and we can jump and we can jump and move and now it looks pretty nice so now we can jump now the problem here is that we can jump many times so it's like Flappy Bird now we can jump with this ball and we could actually make Flappy Bird but I'm not going to do that so we should detect if we are on the ground then we can jump so could we create some sort of function that checks if the ball is on the ground so let's do int ball on ground and we should pass in the ball why and the window height and then what we should do is that we should say if ball Y is greater than or equal to window height minus ball height ball size then we return one otherwise we return zero so then I think that was the same thing that I did somewhere up here I said if ball bottom is okay so I can just replace this with if ball on ground and I give it the ball Y and the window height so I can remove that and then I can say here if ball on ground and key space is pressed so actually I might just move this up here if I know how to use this IDE I could do that so if the ball is on the ground we make sure it's on the ground and then if we are pressing space then we jump so if we run this again we get an error because we have to pass in the ball size here ball size so let's pass it in here ball size and of course this is C so we have to say what these are this is an INT that is an INT and this is an in as well so let's try this again so now we can jump we can move left we can move right we can jump but we cannot jump unless we are on the bottom so if I press space many times it won't jump it only jumps once that's great so what should we do next well I have those platforms here so I would like to do that so we could jump on top of platforms so let's do that how could I make a platform well a platform is just a rectangle so I will say draw rectangle and this gets the X position and the Y position which is the upper left corner of the rectangle so I can say something like can we use like percentages so if we use like window width times 0.1 and this has to be an integer so maybe we have to use some like ABS to make it an absolute number or can we just cast in I guess we can do that too int but which which one is casted in we have to do this and then the Y Position will be similar maybe I should just use the apps this is kind of weird but let's do that and Y would be a window height times let's say 70 percent and then the width would be I won't use abs and this will be window width times 0.2 and then the height will be ABS window height times 0.05 5 okay and I will change these two apps and perhaps I could put them on their own lines since we are short on Space because I'm zoomed in so much okay so now we are drawing a rectangle and we have to add the color so the color will be black and let's run this and see if it will work um it will not work we got some kind of warnings ah we have to include STD lip so that we can use apps so let's do that and let's try one more time okay now we have a platform there but of course we cannot jump on this platform so how do we do that we should detect where is the floor at any given moment so how would we do that let's do this we can actually change our ball on ground function so that it will detect wherever we are are we on the ground and the ground can differ between a platform and the actual bottom of the screen now maybe we could add a platform to the bottom of the screen so we don't have to check two different things so let's actually do that but I think to make this easier we should use some struct so now I have ball y ball size and ball X but what if I create a struct if I say struct type Def struct and I call this ball and the ball will have an INT X and an INT Y and a float sorry it's an INT hint ball int size no it is a float actually so let's call it float and these should be semicolons and then when I use this ball on ground function I will actually pass in the ball ball so the ball struck so I don't have to pass two different things then I just say ball dot Y and mole dot size and then here I can say that ball ball equals and I think there's some special thing that I can do like this and x equals window width divided by two and Y equals window height divided by two and size equals 40.0 so then I can remove the ball size and I can remove these and perhaps I could move these on their own lines and I would move the velocity there as well so I can say velocity is what did I use for can I end in a comma in C let's see so I will add a IMT velocity okay so then I have to change all of these ball wise to ball dot Y and ball size actually here I just give it the ball and I remove this and ball velocity will be ball dot velocity and ball size will be ball dot size and ball X will be ball dot X so let's see if this is still compiles and runs yes we can still jump around and everything works okay now we might want to do the same thing with the platforms since we're going to have multiple platforms then maybe we could Define a type called platform so type Def struct and it is a platform and the platform will also have an INT X and an INT y so basically it is a rectangle so we have to pass in everything that we used here when we draw the rectangle so X Y with height and color but let's not use the color let's just make it black for now so X Y end with aimed height okay and then we can do something here we should actually have an array of these platforms so then we can go through each of them and first of all draw them and then check the collision between the platform and the ball so let's do that so to the ball on ground we won't pass the Windows size the window height anymore we will pass an array of platforms so maybe we should call ball on platform and we will pass in platform platform maybe it works like this so this will be an array of platforms so then we will say for INT I is 0 I less than what how do we Loop through this do we have to pass in the amount of platforms or can we get the number from this I think we could use some kind of math here like size of platform divided by size of platform is that how you do it like if we say int count equals size of platform so this is the whole size of all the structs in this array and then we divide it by the size of one platform then that should be the count of the Platforms in this array then we can pass any amount of Platforms in here is that true I don't know let's see while I is less than count I plus plus and then we do this and this should be in fact platforms because it's multiple platforms so how do we check this let's first of all say that the platform platform is platforms index I and then we should check that if the ball Y is what if the ball Y is greater than or equal to the platform dot the bottom of the the top of the platform so the Y position platform dot Y and in fact ball ball y plus ball dot size let's do it this way so this is the bottom of the ball let's say paint ball bottom is that so if ball bottom is greater than or equal to platform Y and we can remove this but this is the only thing it also has to be it also has to be within the X position now in relief there is actually a collision Collision check Collision circle of wreck which we can use to detect the Collision but there was some problem with this but let's actually do that so it has to collide first of all so if check Collision Circle wreck so we give it the center Vector Center so this is the X and Y so I'm not quite sure how to do this should I do Vector 2 ball position is X ball dot X Y ball dot y or maybe I should say the vector 2 in the ball instead of X and Y that might be good but I guess we can do that too so this is the position of the ball okay and then the radius so we'll say ball position and ball dot size and the rectangle so let's call it platform Rec and let's say rectangle platform Rec will be will be what it will be dot X is platform dot x dot Y is platform dot y can I just cast it into a rectangle because we have the same things in there not quite sure width equals platform dot with and height equals platform dot height and let's do that and that and put them all on their own lines correct so we check the collision between the ball and the rectangle and then we return one now this is not very beneficial to return one from here ball on platform and we should Loop through all of the platforms here as well no sorry we should pass in the platforms so ball on platform platforms and we can't set the ball wide this so let's comment this out at first and figure out where we should actually position the ball and then we have to make an array of platforms so let's create it here let's say platform lat forms now I'm not quite good at C yet so I'm not quite sure what is the best way to create an array of platforms but let's put three platforms because in C you have to always set how many of things are in arrays so we will say that platforms will be an array of three different platforms and then we will say platforms 0 dot x equals zero so this will be the ground and platforms 0.y equals window dot window height window height minus let's say 10 so with the floor it's going to be 10 pixels and what else do we have on the platform we have width and height so platforms zero dot width will be window width because it's the length of the whole thing and platforms zero dot height is 10 pixels okay and then let's add two extra platforms so this will be one and this will be two and where should we position these let's put this at apps Windows window width times 0.1 and window width times I mean window height times 0.7 and make this apps again and width will be apps window with times 0.3 and height is going to be ABS window height times 0.05 something like this and actually I will copy this here as well and there should be two and I will put this on the other side so I will put like 60 of the window with and let's do 50 let's actually put like 30. and width is going to be the same and height is going to be the same okay so now we have some platforms that we can then draw on the screen and we actually have to draw them on the screen so let's see if our Loop actually works so let's do the same thing down here so for each of these platforms we will draw them so count is size of platforms and divide by size of one type and then this will be platform dot with sorry platform.x platform dot Y and platform with and platform dot height and let's see if this thing actually compiles probably not no size of on array function parameter platforms will return size of platform star okay Undeclared platform ah because I have to say actually I can just say platform index I maybe that is better to do the same thing up here to platforms index I am here as well platforms index and let's compile it again and now it actually compiled and it opened and we have two platforms here and there is in fact a ground platform as well that's 10 pixels now it seems that really doesn't really support this fractional scaling in Ubuntu because I have like zoomed my screen but this is definitely not 800 by 600. I mean I guess it is but it is not presumed like everything else but our ball has disappeared now and that means that our Collision detection doesn't work now we get a warning but I'm not sure if that matters or does it matter size of an array function parameter platforms will return size of platform pointer so does that not actually work then inside the function so if I set the count to three then will it do something else Yes actually now we stopped there and we can jump but it's a bit weird and we can move and then Here We Fall Down okay it kind of works but not that great because it gets stuck in weird positions and we get stuck in the bottom of the platform as well so we can jump and I can jump over here and then I get stuck here okay so I will do this I will pass in the int cult and then I can do platforms and the other one works so I can do size of platforms sorry I have to say platform count and I will set this here in platform count is size of platforms divided by size of a platform and then I can do the same thing down here platform count now I still have to manually put it here so I might as well like Define this somewhere but let's see if this now works and we don't get any warnings and we can jump and it still works great and I can go here but it's very weird so now we have to fix this so first of all when we jump and we land on the platform it goes in a weird place so how do we fix that I guess we should return the platform from this function but then what do we return if we don't find a platform can we return null then how do you do that c can I return no and this will be platform index I return value does not match the function type how do you define a function that might return null or what if I return the I and I say int and I return null from an INT I can so yeah well I could use this maybe not the best idea but I'm not sure how to do it the proper way so then we can say here if ball on platform is not null and we have to actually move this in its own thing we will say current platform and also int current platform is that ball on platform and then we can set the ball y to the position of the current platform so we can say that ball y equals platforms index current platform dot y minus ball dot size now what's going to happen here is that when we jump from below and we hit the bottom of a platform it will jump on top so let's let's see we got some warnings returning void star from a function with return type int next integer from pointer without a cast so I have to figure out how to do that for now we're actually jumping on top of this platform and if we jump here then we also go there now it does something weird there when it like jumps maybe I have to check the Collision before I draw the actual ball on the platform so if I jump here and I jump over here then now I Disappeared actually for some reason so what does this mean comparison between pointer and integer okay well I can return minus one isn't that the convention in this kind of things minus one so then if it's not minus one then we do that let's see if that helped anything at least we got no errors so now if we jump we are still able to jump we can jump on here and how about we jump there okay that fixed that so probably that was the problem but now if I jump here we will go there and if I jump here we will go there so how do we fix that one we have to say that the ball has to be moving downwards in order for us to stop so let's do that how do we check if we are moving downwards well if the velocity is greater than zero so we can say here where we are checking or on platform we will say that actually we will not change this function we will only change this part so if we have this lower and we have to say if ball dot velocity is greater than zero then we move the ball and in fact we do this as well so if we are currently on a platform and we are moving downwards then we move ourselves to the top of the platform and we stop moving I think that makes sense so now I'm jumping here if I jump there I go there still flashes finally and if I jump over here and I jump to this one then now we are going through it which might be acceptable in some games but then we stop all the top so that's great that's pretty good I kind of like that but what is going on here I think what's going on here is that because we are checking the Collision between a rectangle and a circle then in the edge we are not actually colliding because the circle is a circle but then once we Collide then we jump up and then we are not colliding anymore so then we go down again so that's an interesting problem so perhaps I should treat them both as rectangles and then just have some kind of threshold where it falls off so I would say check Collision wreck Rex okay check Collision Rex and it takes two rectangles so let's actually say rectangle ball position and let's do X and Y now I actually want to have like a like an image instead of a ball like my own drawing or something so it might be a rectangle in the end so maybe I will just change the whole thing to a rectangle but I guess we can imagine now that it is a rectangle so the X will be ball x minus ball dot size and ball y minus ball dot size so this will be the upper left corner of the ball and then the width will be ball dot size tie two and the height will be ball dot size times two as well and I only pass in the platform Rec and let's call this ball rack ball rack and platform rack and let's not take the ball bottom we don't need that anymore but now what will happen is that it will like overhang and actually something weird is going on here too but it will like overhang here we are floating now there so we should do something and something weird is going on here because we are moving all the time so first of all let's imagine that the ball X will be a bit further so let's add like 30 pixels and then let's make the width less 30 pixels and if I now run this and now we should might fall this way so that's a bit better now there's something weird going on here it's probably that this height is not going to be the same as the rectangle height so what if I put punch one minus six that's just worse so let's put plus one okay now it works okay great uh it's not that great wait a minute here it's not working it should fall off already like it should fall off here but on this side it will actually fall in the right place so what does that mean so the ball rectangle the x is the ball x minus the ball size okay plus 30. sorry has to be minus 30 this one and the width minus 30 as well right maybe let's see what happens now so now if I move here it falls off very quickly and here it doesn't work anymore what so let's take a look at this so ball X is going to be in the middle and we subtract from that the radius so we will be here so then we should add to that a little bit so that we draw a smaller rectangle yeah so let's put just 20 pixels and we add to this right yes and the Y is just going to be that it's going to be the same and the width is going to be the ball size times two but we have to minus the 20 pixels so that we are in the end of the ball and we have to minus another 20 pixels so that we are in the correct place so that is going to work so now if I go here then I will fall down and if I jump here and I go here then I will fall down okay that looks pretty good it's good for a newbie so I can jump here and I can jump here and everything works and I fall down but that is kind of weird why are we drawing the ball inside of the platform draw circle ball X ball y ball size because we are adding to it after work so we in fact have to do this in the beginning and now if we run it and now if we jump here yeah now it doesn't go through that platform or like inside the platform now that's a bit weird yeah so when we jump from here we are gonna jump so that we touch this but we don't go over it but we start to go down while we are touching it so then our test to see that if we are going down will be true and then we will jump over that one so how do we check for that we could modify the check here so Collision wrecks so what if we imagine that the ball is only the bottom half or bottom 25 of the ball so we would say that the ball y will be ball y plus ball size divided by two and height will be ball size divided by two plus one and then if we run this now whoa foreign minus ball size divided by two sorry I was gonna add and refresh okay so now we can jump here and we go here and we can jump over here and if we jump here then it works perfectly so we can jump from that to this one but we cannot jump from here to that one because it has to be the bottom fourth of the ball has to be inside the platform then they are considered touching each other so that looks pretty good great it just took me one hour and 20 minutes to do this I think that is a very good time frame for creating a bouncing ball in C so now we should actually make this like a game perhaps I should commit these changes already to GitHub because this is pretty cool already okay so let's continue what should we do next let's run this game again and see what we could do here so how should this game work we could like add more of these platforms and then we could either move up or we could move right how should we move so we could like move the image to the left as we are moving to the right so we'll like pan through the world we are in so how would we do something like that is there some kind of like function that would like pan that would like offset everything like translate no uh pan no we have some Sounds here too we can we can create sounds that's pretty cool what could it be called Point Point Circle Point pulley point line hmm offset image resize canvas update mesh buffer glyph info camera 2D wait a minute camera 2D begin mode 2D okay camera what does this mean get world to screen 2D okay so I think there is something in railing that actually can like move the camera around okay so it's called a camera device position orientation in 2D space that's great displacement from Target Target rotation Zoom okay let's try and use this camera 2D begin mode 2D get camera metric get World discrete begin mode how do I move the camera camera mode I don't really understand how this works because they're all just get there's no set like how do I set update camera here it is okay update camera camera mode okay let's try and do this but I guess we have to say here begin camera to the what was it update camera begin camera begin mode 2D okay begin mode 2D and we have to pass in the camera camera and let's set the camera here let's say camera to the camera equals what is camera 2D camera 2D struct offset Target rotation Zoom okay so this has to be a vector too so let's call this dot offset equals camera offset and Dot Target equals camera Target and rotation equals zero and zoom equals this has to be zero or one maybe one makes more sense and these are going to be Vector twos so let's say Vector 2 camera offset is I guess this has just an X and Y yes x equals let's start with zero and Y equals zero as well and then we will have the same with the target camera Target I think that can be zero as well so I think what we can do with this is that if we use the rotation or the zoom then the target defines where we are going to rotate from or Zoom from and in fact this will be a command of the semicolon and here as well okay so then we can say begin mode 2D camera do we have to set it in the while loop perhaps not I think it's up here begin mode to the camera and then we have to call end maybe end mode to and let's see if that actually now still compiles it does and it looks just the same but can we then move the camera around so when we begin drawing we can move the camera or should be before begin drawing or after update camera and we are going to get the camera and the mode from a 2d mode to the um what is the mode update camera mode that's all update camera mode for selected mode 2D you have to go to the C literal so hopefully we have some sort of color mode here no we'll only have colors with the c literal mode set mode mode mode is there a mode with capital letters full screen mode matrix model that's it update camera well thanks for not telling me what it is let's try zero what does it do wait a minute that is a camera but this camera 2D is there another one camera update camera 2D update camera 2D update camera okay well what is camera camera struct camera okay we don't have just camera uh camera 2D get screen to World 2D get world to screen 2D begin mode get there's no set set camera camera set camera let's ask chat gbd maybe that is a good position to ask chat gbd so let's say how do I move the 2D camera around in Rayleigh in C okay so camera 2D camera is zero why do we patent zero Target offset rotation Zoom okay basically what I did oh you can do this way to like cast it as a okay camera dot toilet get mouse position so this is gonna follow the mouse begin mode 2D with the camera okay that's what I did so where does it actually oh I don't have to update anything I just this updates it so I have to have it inside this so when I have the begin mode to the inside the loop and I pass it the camera then it moves the world around so I guess I have to do this as well end mode here as well begin drawing within beginning mode okay so other way this way and that way and then if I set here camera dot x minus equals one so now we should move to the left with every frame nope sorry it wasn't called X offset ah offset X camera dot offset dot x minus eight and let's run this look at this we are actually moving now that's great that's amazing so now what we can do is we can set the camera offset depending on the X position of the ball so we can say that camera offset x equals ball dot X that means it would be moved by the X position of the ball so we have to minus the window width divided by two because I want the ball to be always in the middle and we have to make this negative so can we do this minus that so if I do this then I move then we're moving now that's great for now we're actually following the ball that's great now maybe we should only move it once we get to the edge so in the middle it won't move so how should we do that we should say if ball dot X is greater than window width times 0.8 then we set the camera offset X to minus ball x minus this right is that correct and else if it is smaller than window width times 0.2 and we do that something like this so now if we go here and we start the move okay that works perfectly and when we go here we start to move here that's great I am now a professional game developer that's pretty cool now we just have to create more world so how do we do that so first of all we should create let's add a comment um create floor and we will set the width of the floor to window with times 10 and this should be floor and we should add one more thing let's do this let's say camera offset um can we just do this if if camera offset X is smaller than zero then camera object X is zero so we don't actually go like past sorry if it's if it's greater than whatever it it always has to be negative if it's greater than zero then it's zero right so if I build this now then it's not going to move this way and I actually still fell off the thing so it's not going to move this way but if I go here it is going to move okay and now we have lots of World here great so let me just add platform could we add this like random platforms let's do that so how could we create random platforms let's do something like int world with is going to be 800 let's say a window with times 10 and then let's say int platforms is going to be world with divided by what let's say platform with let's say int platform with is going to be 300 pixels let's just hard code this for or should we use like a window with times 0.3 so that's the platform with and it's going to be apps and the amount of platforms we are going to a platform count platform count is going to be World width divided by the platform with time 0.9 no times 1.1 so we add 10 percent of the platform with or in fact we add plus 0.1 times window with window with or 0.2 times so what I'm doing here is I will say that we will divide the world with by the platform with plus 20 of the window width so that there will be 20 of the width of the whole world available like empty no platform in that position okay and this will be platforms platform count plus one for floor and then what we will do is we will say four in I is 0 I less than platform count I plus plus we will draw a platform and let's say in platform X is going to be like window width times 0.1 and let's do apps again apps and then we will set the x to the platform X and then we will say platform X plus equals amps window width times 0.10 uh zero point uh 2 divided by three so there will be um three platforms per window with something like this I don't know what kind of math I'm doing here but this should work somehow and the Y will be some random number so y will be window height times Rand um how does that work Grand I think I have to like do something like include time dot h and then I can do here something like is Rand which is like seed Rand and I get time zero is it something like this so we actually get a random number and is Rand going to be from between zero and Rand Max so Rand divided by Rand Max so this will be from zero to one and times window height minus window height times 0.3 so what am I doing here so the Y position of the platform will be a random position on the y-axis but the maximum will be this so this is Max y platform Max y so in platform Max Y is that window height minus 30 so why don't I just do window height times 70 percent and this should be ABS I'm not sure if I even need apps but I will use it anyway so this is the maximum position that the platform can be at so that we don't put it too close to that floor and then we should add to this a little bit so that we are not too close to the top of the screen so let's do plus platform mean Y and that will be like 10 of the window height I mean why so it is going to be a random number somewhere in the y-axis and width is going to be 0.3 let's just have it there for now and this should be platform X and then we add to their platform so that the next platform will be actually the current platforms with plus that and they should be not one but I so platforms I so we move the next platform to the right by the width of this platform plus a third of twenty percent of the screen because we are going to have three platforms per screen around maybe my math works out maybe it doesn't and we don't have to count the platforms anymore because we have a set count here so let's see if this now works now it's probably not the best idea to create the whole world at once but I think it's gonna be okay uh why are they all at the same position and why don't I have a floor um because I started from zero I had to set this to one and less than or equal to that so now I have a bottom here but all of the platforms are up there so I should in fact have more space so I will put 30 percent and the random is not working why is not working how does brand work in C is ran time null random number is Rand okay random modular 100 okay so let's do time null and let's run it it's still the same is it because I used in tier will it convert this to end already and then do the operation no no way let's do that all right after a long time of debugging why Rand gives me zero every time I created this monstrosity that actually produces a number so let me create a function for this so let's create here float Rand float and return this so This returns a random float from zero to one and then I can go here and I can say brand float times platform Max Y and I can in fact move this inside here and now this should work after a long time okay now we have these platforms on random positions so I can move here and I can jump and I can do that and I can jump here and here and I can't jump there now I should change this a little bit so that the camera would move a little bit earlier so here I will set this to 60 and 40 and this is the 60 and 40. so now I have my game here now all of the platforms are a bit too high right now so I should probably change something about that but now I can jump through these things so let's make them a little bit closer to each other which we can do by changing this number so I will call this a platform closeness and I will set it here in platform closeness is three but I will set it to one actually that's not a good idea I will just do this platform spacing and this is going to be platform spacing will be 0.01 percent and it has to be a flow and I will change the maximum so platform mean y will be um 20 and Max y will be 80 and maybe I should move these up here because these are kind of like settings so these are the platform settings in this world with platform with platform count okay so let's run this again so here we have our game now can I jump over there yes I can jump there and here and now they are really close together I can jump there apparently and I can oh now I fell down how can I get back they're too high now okay up all right the spacing is not that great right now but at least I can move and my calculations are not correct because I ran out of platforms already so platform count is actually world with divided by platform with plus window with times flat form space that should be correct and let's run this again okay now platforms are very close to the bottom sometimes but it is working pretty nicely now for some reason I fell off the world I have no idea why but it happened now I have been coding for two hours and I'm not sure if anyone is interested in this video so I will end today's video here unfortunately if you watched all the way to this then I hope you're not too disappointed now if you do want to see more then please leave me a comment and if enough people want me to continue on this then I will continue but right now I'm getting tired already and I'm not sure how interesting this is so let me know in the comments if you like this kind of videos and then I will continue this project if you want me to do so so hit that subscribe button and leave me a comment and then wait for me to make another video and I will see you in the next one
Info
Channel: Unconventional Coding
Views: 5,284
Rating: undefined out of 5
Keywords:
Id: TFlfENy3iDY
Channel Id: undefined
Length: 66min 37sec (3997 seconds)
Published: Sat Jun 24 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.