My First Game - GML - Score, Lives & Effects - Space Rocks (Part 4)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to the fourth tutorial for making space rocks in Gmail so today I'm going to be adding the score and lies and also the different rooms in the game so let's get started let's create a new object and I'm gonna call this obj game and this object is going to be more of a system object it's not ever going to be visible to the player but it's going to be controlling things behind the scenes so let's come into its create event and in here we're going to use the built-in variables score and we'll just start that off as zero and also lives and let's just go with three so these two variables are special variables that are built into game maker and they're actually global variables and that means that unlike instance variables which are only accessible to a particular instance or local variables which are only available during one script global variables will persist for the whole game and any instance can use them so now that we've set those two up let's go and draw them to the screen so let's go into the draw bed and this is where we can draw different elements to the screen so you can draw things like text shapes and spots so we're going to be drawing text and the function that we can use for that is just draw text as you can see there are a few different ones so if you want to draw it with a certain color or if you want to draw it transformed you can use those ones but for now we'll just use this so if the XY location of the text I might draw it just at the top left would bumped over a little bit so I'm just gonna go 2020 and now it's asking for a string so remember a string unlike something like this which is a number a string is a bunch of letters that we put like this so if I want to draw the score to the screen I have to convert it from a number into a string and there is actually a function for that so we can just go string and that will convert it to a string and since that is just going to be drawing a number just so that we know what that number corresponds to I'm also going to write score and I'm gonna add that together and that's gonna do probably exactly what you think it will so it's just gonna say school and then it will add whatever number this is as a string to the other string and it will draw those combined and let's do the same thing for the lives except I'll draw it down a little bit and we'll do lives and lives and now all we have to do is add the game to our room and for objects like this the convention is to just put them at zero sir but really you can put them wherever you want their position isn't going to matter alright and let's just run the game so there we go now at the moment there is none actually doing anything so let's have the score increment when we shoot an asteroid and the lives decrease when we hit an asteroid so at the moment it's actually just drawing with the default font so let's create a new font call it FN t text and again you can pick whatever font you want I'll just go with Canon SOLAS and I might turn off anti-aliasing just to give it a bit more of a retro look so that we don't have the text pixels smoothing it's going to be more blocky so now we just have to set that font value and since we only need to set it once and we're going to use the same font for the entire game we can just set it in the create event for the game object so we just go draw set font FN t text and now it will use that font to draw this alright let's actually play with those values for the lives in the score so in the bullet that's what we had the collision with the asteroid and now no matter what asteroid is being hit I'm also going to add to the score so I had skull plus equals 10 this is a global variable so we can access it from the bullet alright now let's come over to the ship occlusion with the asteroid and let's get rid of one life lives minus equals 1 so now if we run the game and we shoot an asteroid you can see that the score is going up and our lives go down if we hit an asteroid all right now let's create the other rooms for the game so I want my start room to be essentially the same as the same dimensions and everything but just to have some text appearing in the middle so I'm gonna duplicate this one RM start and I'm gonna drag it to the top here because whatever room is at the top is the room that you're gonna start in when the game runs all right and I'm just going to get rid of the ease and now we do want to create the game object in this room and then I want it to persist for the whole game right so I don't actually want it to be getting created in the game room I want it to be created right here at the start and then as we transition between the different rooms I want that object to persist and keep score everything so I'm going to just take this persistent alright and now I'm just going to duplicate the start room again I'm gonna call that RM win and again I'm just gonna get rid of that game object and one more time for game over all right and now depending on what room were in I actually want the game object to be drawing different things so I only want it to draw this stuff when we're in the game room but if it's in the other rooms I want it to draw different text so I could just do if the room that we're in is equal to RM game and then I could do a bunch of other else if statements to check for the rest of them but let's do something different this time I'm actually going to use a switch statement so I'm gonna go switch room and now a switch statement has essentially the same logic as an if else if chain right so I'm basically saying switch the room so whatever room is equal to in the case that it's equal to the RM game then it will do this stuff and then at the end of that we just put a break so that's one case but we can set up another case like the start and we will do the same for all of them and the reason that we put these breaks in is because it kind of signals the end of the case if you don't put the break in then the switch statement doesn't know when to finish and it'll actually just run the code for the next one so make sure that you do put a break here alright so now for the star I want to do a few things so let's start off with the title let's go draw text and I'm actually gonna use one of these ones here so I'm gonna use transformed color because I want to draw this a little bit bigger and I want to add a color to it and I think this is gonna get quite big so I might do this over the multiple lines so you can see down here there's a few more arguments that we have to give it so the X Y I want to draw this in the middle of the room so I'm gonna go room width / 2 and I'll do it still at the top but a little bit down right so remember our room is 500 by 500 so that will be a fifth of the way down the string itself is just going to be space rocks now for the X scale and Y scale so normally this is 1 by default but I want this to be a lot bigger than the other text so I'm gonna say 3 for birth I don't want to tilt this text at any angle so I'm just gonna say 0 and now so these are asking for 4 different colors and that's just because you can actually have the corners of all of the text being kind of different colors I'm gonna set them to all be the same so what I'm gonna do is right here I'm gonna set a temporary variable C equal to C underscore and these are constants for some different colors that we can use you can actually use hex values and stuff but this is easier if you just want some stock colors so I'm going to use sealer and you set for all of those and for the Alpha I want this to be not transparent at all so I'm gonna say 1 next I'm going to draw kind of the instructions to the player so again I'm gonna have this be centered in the middle of the room so room with over 2 and I'll just go down a little bit more and now for the string so I'm gonna firstly say on the first line score 1000 points to win but I actually want a few different lines of text and what we can actually do is open this up with an ad and this will continue the string over multiple lines so now I'm just going to tell the player what the controls are up is move so left right is change direction and space is shoot and then I'll just say press ENTER to start and we can end the string that all right so let's just give that a test so remember we should be starting off in the room start so this text should be popping up all right so we can see it's kind of working but the text is left justified isn't it so everything is being knocked over to the right so if we want to get it Center justified we can use draw set H a line so the horizontal alignment and then we use FA Center to get in the center and then after all of this we have to reset it back to normal because we don't want anything else being drawn incorrectly so we change it back to be to the left all right and just run it again and then we go everything is popping up right okay now for the rest so I'm gonna do quite a similar thing to here so might just copy this paste it here so instead of using the yellow for the large text I'm going to use lime because with one so I'm gonna say u1 I might put it down a little bit and now for the text I'll put this at 300 and just say press ENTER to restart all right and I'll copy this for the game over this time I'm going to use red I'm gonna say game over and I'll put this at 150 and then I just want to tell the player what the final score that they got was so I'll do this at 250 and we'll get final score Plus string school and then also we'll do another line saying press ENTER to restart so a lot of this is pretty boring it's just playing around with the numbers to see what looks okay and now for us to actually check this we should get the game workings that were transitioning to the correct rooms so first off because we start in this start room and we've said press ENTER to start we have to set up that key press so in this game object I'm going to add the step event so I'm gonna have if keyboard check pressed the enter key and now depending on the room I want to do different things so if we're in the start room that I want to go to the game room but if we're on the wind screen then I want to restart the game and the same for the game over screen so we could use another switch statement and say depending on the room in the case that we are in the start room then we are going to change the room so let's use room go to and this function will transition us to a different room so let's go to the game room but if we are in the wind room then we are going to game brief stops we're gonna restart the whole game and actually I want it to do the same thing if we're in the game of a room so what I can actually do is say case room game over and because we haven't put a break statement after this it will run this code in either case right it will just keep going until it hits a break statement alright so that takes care of the enter presses but what about the score and the lives so I'm gonna have this object keep track of the scope sir if the score is greater than or equal to 1000 because that's what we said would be the win condition then let's room cursor the winner and also we can say if lives is less than or equal to zero then we can also go to the game over but one other thing that we have to do is make sure that we're only checking this in the game room because if we check this in every room it's also going to be doing this in the wind room so it's going to be stuck in a loop while we keep moving to the winner room which is not what we want so first we have to check if the room is equal to the gamer all right and I'll just have this horrible all right and just speed up our testing I'm just gonna set the school to nine ninety and lives down to one and let's give that a run so let's just press ENTER and let's go and choose something there you go so we won let's restart great so we've restarted the game press ENTER again and let's go into it great and game over all right so that will do us for now in the next tutorial that will be our final tutorial we're just going to add some flourishes to the game so we're going to have the game object actually spawning in more and more asteroids as we go through the game and we'll be adding some sound effects and music so I hope you enjoyed the tutorial and I will see you in the next one
Info
Channel: GameMaker
Views: 98,116
Rating: undefined out of 5
Keywords: YoYo, Games, YoYo Games, GameMaker, Game Maker Studio, GameMaker Studio, game, maker
Id: 4y3I5jL7ERo
Channel Id: undefined
Length: 12min 38sec (758 seconds)
Published: Tue Oct 23 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.