Game Over

28 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€67.17
List Price:  €95.97
You save:  €28.79
£55.84
List Price:  £79.78
You save:  £23.93
CA$100.80
List Price:  CA$144.01
You save:  CA$43.20
A$112.68
List Price:  A$160.98
You save:  A$48.30
S$95.16
List Price:  S$135.95
You save:  S$40.79
HK$543.23
List Price:  HK$776.08
You save:  HK$232.84
CHF 63.03
List Price:  CHF 90.05
You save:  CHF 27.01
NOK kr794.99
List Price:  NOK kr1,135.75
You save:  NOK kr340.76
DKK kr501.02
List Price:  DKK kr715.78
You save:  DKK kr214.75
NZ$124.35
List Price:  NZ$177.65
You save:  NZ$53.30
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,350.53
List Price:  ৳11,929.84
You save:  ৳3,579.31
₹5,987.45
List Price:  ₹8,553.87
You save:  ₹2,566.42
RM312.96
List Price:  RM447.10
You save:  RM134.14
₦108,274.53
List Price:  ₦154,684.53
You save:  ₦46,410
₨19,454.09
List Price:  ₨27,792.76
You save:  ₨8,338.66
฿2,378.12
List Price:  ฿3,397.46
You save:  ฿1,019.34
₺2,466.48
List Price:  ₺3,523.69
You save:  ₺1,057.21
B$432.51
List Price:  B$617.89
You save:  B$185.38
R1,311.95
List Price:  R1,874.30
You save:  R562.34
Лв131.43
List Price:  Лв187.76
You save:  Лв56.33
₩103,223.92
List Price:  ₩147,469.07
You save:  ₩44,245.14
₪257.31
List Price:  ₪367.60
You save:  ₪110.29
₱4,072.64
List Price:  ₱5,818.31
You save:  ₱1,745.67
¥11,041.56
List Price:  ¥15,774.33
You save:  ¥4,732.77
MX$1,420.31
List Price:  MX$2,029.11
You save:  MX$608.79
QR254.14
List Price:  QR363.08
You save:  QR108.93
P970.50
List Price:  P1,386.50
You save:  P415.99
KSh9,063.70
List Price:  KSh12,948.70
You save:  KSh3,885
E£3,560.93
List Price:  E£5,087.27
You save:  E£1,526.33
ብር8,897.26
List Price:  ብር12,710.92
You save:  ብር3,813.65
Kz63,830.88
List Price:  Kz91,190.88
You save:  Kz27,360
CLP$69,315.99
List Price:  CLP$99,027.09
You save:  CLP$29,711.10
CN¥510.87
List Price:  CN¥729.84
You save:  CN¥218.97
RD$4,256.60
List Price:  RD$6,081.12
You save:  RD$1,824.52
DA9,489.42
List Price:  DA13,556.89
You save:  DA4,067.47
FJ$162.54
List Price:  FJ$232.21
You save:  FJ$69.67
Q538.26
List Price:  Q768.97
You save:  Q230.71
GY$14,619.81
List Price:  GY$20,886.35
You save:  GY$6,266.53
ISK kr9,747.50
List Price:  ISK kr13,925.60
You save:  ISK kr4,178.10
DH704.68
List Price:  DH1,006.73
You save:  DH302.05
L1,290.62
List Price:  L1,843.82
You save:  L553.20
ден4,134.98
List Price:  ден5,907.37
You save:  ден1,772.38
MOP$559.01
List Price:  MOP$798.63
You save:  MOP$239.61
N$1,299.34
List Price:  N$1,856.28
You save:  N$556.93
C$2,571.30
List Price:  C$3,673.45
You save:  C$1,102.14
रु9,517.06
List Price:  रु13,596.38
You save:  रु4,079.32
S/260.20
List Price:  S/371.74
You save:  S/111.53
K283.61
List Price:  K405.18
You save:  K121.56
SAR262.83
List Price:  SAR375.49
You save:  SAR112.65
ZK1,933.89
List Price:  ZK2,762.82
You save:  ZK828.92
L334.30
List Price:  L477.59
You save:  L143.29
Kč1,691.03
List Price:  Kč2,415.86
You save:  Kč724.83
Ft27,618.75
List Price:  Ft39,457.05
You save:  Ft11,838.30
SEK kr770.19
List Price:  SEK kr1,100.32
You save:  SEK kr330.12
ARS$72,002.55
List Price:  ARS$102,865.20
You save:  ARS$30,862.64
Bs482.86
List Price:  Bs689.84
You save:  Bs206.97
COP$308,627.53
List Price:  COP$440,915.37
You save:  COP$132,287.84
₡35,480.70
List Price:  ₡50,688.88
You save:  ₡15,208.18
L1,775.44
List Price:  L2,536.46
You save:  L761.01
₲544,980.94
List Price:  ₲778,577.57
You save:  ₲233,596.63
$U3,110.44
List Price:  $U4,443.67
You save:  $U1,333.23
zł287.20
List Price:  zł410.31
You save:  zł123.10
Already have an account? Log In

Transcript

Hello, in this video we are going to look at implementing a game over system. Because at the moment if we play our Tic Tac Toe game, the previous video implemented AI, play it plays a PCI places on the middle is one here, hey, I counted it, I'll be stupid and I'll post one here, and the AI wins. At the moment, nothing happened after this animation. And that's the same for wherever I win, lose, or draw. But what we want to do is display a game over state, which will have sort of a semi transparent background, which are sort of gray the objects and we'll have a menu bounce onto the screen. The menu will consist of two buttons, a home button and a retry button.

It's gonna be the same for any game over state. You can customize it very easily. The code will be very extendable, we'll be using a separate UI class. So let's get down to it. So first of all, go to your definition file. And there are a few definitions we need to create.

So the first one is going to be a game over background file path. So hash defined game underscore over on the school background. And this is gonna be rez four slash scan report background because we are going to be using the same background as deployment pause menu, which we have not implemented yet, and we will be implementing in the next video. But the purpose of creating a separate hash define is that we can easily modify our game without having to go through anywhere we've used this background image. So next, we're going to create a game of a window because if you just saw that semi transparent background Now this is going to be sort of the background of the window that's going to bounce onto the screen using a nice cool animation. And that's going to be game underscore over underscore window.

And this is gonna be rez for slash, again, pause window, you can change it to a different window image for your game over screen if you want to. And we need to add a few button definitions. So we need to add a Actually you know what, I will just copy and paste this to save some time. And I'll change this to retry. This is also going to be retry is going to be retried a pMj. As usual, we're going to use the same foil path for the retry pressed button.

But you can easily change it if you have a different image to appear when the button is pressed This is provides a very extendable way of creating our game. Just need one more, and this is going to be the home button. So home button, we're going to have home button pressed. foil path is home button dot png. And then again, it's home button dot png. If you're missing something, there it is, is meant to be retry button.

By bad all of these images were already provided when we set up our project earlier on in this series. And if we scroll down, we need to create a couple more hash defines just for the animation. So we're going to do hash define game on the screen. Go over on the score background from the score fade underscore in time. And this is going to be how long it's gonna take for the game of background to fade in. Gonna be half a second, I'm gonna create a define for the menu, bouncing duration.

If you don't want to bend affecting the fade effect or slide effect, you can easily change the animation. Just go to our sonar costs API guy which is coke cost of sonar learning Dakota, UK, there will be a link provided and that shows you with just some videos, the different animations and aspects of cocoa so that you can easily use this and put one and a half seconds. So that now what we need to do is actually create some new files. Like I said, we're gonna have a separate UI system and this is going to be a class So if I show the classes in folder, I just like to duplicate an existing file, rename this UI dot Hage UI dot cpp just drag this on to my editor using Visual Studio clips Android Studio, or whatever ID you're using. Id however you need to, and you're good to go.

So included that go to AI. It's going to get rid of all the contents. And in here, we're gonna put a hash pragma once in tude focus 2d dot h. And now the class of you are going to have a couple of public methods. The first being the constructor and this is gonna take cost today. Hold on layer asterik layer won't actually do anything in the constructor in this video. This is just setting me up so we can create it as a pointer and pass in variable in the next video for the pause system because that will be part of the UI system as well.

Next, we're going to have a method for showing the game over. Just want to say one thing, though in a lot of games, game over is usually considered the thorough losing state the bad state in this game, you The game is always going to end so always going to be game over at some point. Even when you win, the game is over. When you lose, the game is over. Or when you draw the game is also over. Hence why it's called game over and not like game win, but you could have separate methods very easily because we've got a state system in our game so you can easily detect which particular date has occurred, and then show me the color menu.

And this is going to take a cost to the layer and this would allow us to draw to the current layer we're going to have a couple of private methods and this is going to be void retry. This is going to be co cos to the wrath. So this is going to be the original sender. gonna call it P sender, this is just a naming convention of Cobras TX you can call it whatever you want to and another one go to Main Menu. So these will be called depending on which particular method is I mean, which particular button is pressed. So go to UI dot cpp get rid of all the contents.

So now in here we need to include a few files. First beam you all you got haitch neck We're going to include the sonar framework file from the sonar cocoa Salpa dot h. And this is just going to be allowing us to use the UI functionality for stuff like centering very easily hash include definitions, or Hey, include game scene dot h. Include Main Menu, scene dot h. So first of all let's create the constructor, which like I said, will be empty. So cost to the layer asterik layer will be implementing this in the next method. Now while you just be easier for copy and paste these instead of typing them out myself Copy and Paste UI colon colon and just put some nice curly braces. So now for the show gameover we are going to be creating a background and the menu system. First of all we want to get the screen size we need that for stuff like position.

This is going to be equal to direct colon colon gift instance. Yeah, get visible size. And what we want to do is sprite asterik background. This is going to be equal to sprite colon colon create. And this is really going to take file path to our game over background like so. They set the position to background position and this position will be Sona co cos help.

Oh Conan co long get screen center. So we're just centering it in the screen, difficultly that sort of semi transparent gray background that will just sort of hide some of the game objects. Now we're going to set the opacity set to pass it to zero. Lay Schild background. Finally, we just need to run an action which will fade it in so background run action and the One option is going to consist of a sequence So the first action in the sequence is going to be a delay time. And the delay time is created with a time of peace, underscore fading time times by 2.5.

And then fade in create, and the fade in is merely going to take the game over background fading time. So let me explain why we're using a sequence and not directly doing the fade in. And the reason for this is the computer or the actual phone, which is a computer really yet the computer doesn't know about the animation in terms of we need to display the background after the animations have occurred. The computer will know the game state as soon as the piece has been placed or it's decided to place the piece there soon as being placed in the array. But before the after image has actually appeared. Because we've got animation, it takes a bit of time.

But we want to allow that to appear, allow the actual essay went in or the losing pieces to change. And as a result, this will allow us to see why it's happening. Give us some time to process it as the user, then show the background and then show the menu as well, which we're going to be implementing in a second. You want this to last a lot longer, you just change this aka if you want the actual game over to come a little while after the actual game has finished. You just change this value to increase it. So now when I create a menu item image, we're going to create a menu system menu item image, which is going to be the background, which incentives that game have a window, then to menu item sprites, which will be the home button and the retry button.

So menu item image. And I'm going to call this overlay. overlay overlay. Window item equals to menu item image, create. And this is going to consist of a few different parameters. The first one is going to be game over window and Game Over window again, game over window again.

So this is four different states such as disabled state, regular state, we just want the same one for each individual state. We're not modifying the states. And in terms of any callback, we don't want this to call any method when it's pressed. So now we're going to do menu items right? Actually not colon colon. This is going to be asked to retry item equals menu item sprite create and for the normal sprite, we need a sprite, quote unquote on create for this it is going to be retry button we can just copy and paste this for the selected Sprite.

We also need a disable sprite so we can actually use the callback otherwise we won't be able to use the callback we need to specify the disabled sprite butter sprite as well. And for the callback instead of specify no one of the do cc underscore callback. One because remember, your retry method has one parameter The selector is UI colon colon retry. We're not passing any parameters in ourselves that handles itself. Target is this and that is it in terms of initializing the menu item sprite for the retry button. Now we can position it.

So retry. No, I'm actually that's a bit easier just to see retry a retry button, we try item. set position. For this is gonna consist of effects to first of all, you want to get the overlay window item, yet, content size and we're going to divide it by four. The reason we are doing this is because by default, it will will be centered in the actual item in the overlay window item. And what we want to do is just position it slightly to the right.

And we're just going to get a quarter. we've experimented with these values, so we know what values look good if you want to change the positioning, if you need absolute values, feel free. I don't recommend using absolute values or recommend using dynamic calculation like what we're doing here. But you can easily change this. And now we're going to set y position and this is going to be the retry term. Get position.

Y why when I'm modifying this, the center is a Okay, so it's still in like something so lucky if we have close everything off by the looks of it. What does he not like? No matching constructor for initialization of Coco's 2d Vic to ah the contents it should be dot width. My bed was trying to pass in a vector and which is obviously not good at all. Now we are going to create the main menu item. So if we copy and paste this to save some time, rename this to main menu item and a menu item.

We need the home button here. We could call it home item whatever you want, it's your choice. This is obviously going to call the go to Main Menu mefferd you're setting the main menu position. Again the position wide for main menu item. But for this we aren't actually adding for to eight all we are taking away so this will position it to the left left of the center of the overlay window item in the x axis. And now that we've got that, we can actually construct our menu.

So menu asterik menu equals menu panel colon create. And in here, we merely specify the items that we want to put in here. So overlay window term, retry term, and main menu item. This will display them in this particular order. So these two items will be on top of this one. So if we were to put this after retry attempt, this will be on top of that one.

So that's just something to bear in mind. We're just gonna know this parameter that it requires. And now we're going to set the position and for this we're going to be Menu set position. For this was undo vec To sonar focus help you i colon colon and get screen center. And for the x value, we want dot x we want it centered in the x axis. But for the y axis, we want to do something slightly different.

You want to get the screen center, but we're going to do dot y plus screen size. dot height was wondering if we're doing that only appear off screen because if that's the center and we're adding the height of the screen, they'll no longer be visible and that is what we want. Because we want an animation which will make the menu bounce onto the screen, your locks if you want to fade in animation, you can easily do that. Using the code that we've got from here, just adapt it to the menu but we are going to do a bounce animation. So now we're gonna do layer. Child.

This is going to be menu. The animation we're going to create is called eat bang. So there's a whole heap of them, feel free to check out our cocoa API guide at co cos dots on our learning Daugherty gay said earlier on in this video, there will be a link provided so feel free to check that out on the quarterly menu, action easing equals inventor, colon colon create and for this we just need to specify where it's going to move to so it's gonna be moved to hold on hold on create. First of all, that's for the two ration which is menu on the score mukbangs duration. And for the position, we just specify the center of the screen which is Coco's helper, UI, corner colon get spring center. And now we're going to create a sequence we do love our sequences.

This is gonna be called menu, show sequence equals sequence, colon, colon create. And the sequence again, we need to do a delay time so the actual piece animation can completely finish and the user can see the result before the game over status showed me create. And this is going to be piece of skill fading time time by 2.5. Then the menu, show sequence Surely, that should be over here. Finally, we just need to run the action. So menu, run Action Menu show sequence.

And that's it for showing the game over, just need to implement you retry and go to Main Menu methods. These are very simple. We've already done this before, so we're actually going to go and get the code. So if we go to Main Menu dot cpp and if we just copy and paste this for example, here, so game scene creating the outline because for the reach, right, we want to reload the current game scene. For the transition. We're using the same transition of fade.

You can use a different transition if you want to use a different transition time, more than welcome to We are replacing it not popping anything or pushing one on we want, we don't want the currency to exist anymore. And we're gonna do the same for here. The only difference is it's gonna be Main Menu scene. And that's it, we can now go into our game scene dot cpp and just set this up. So the first thing we need to do is actually go to games in that age. And just include the UI header.

Create an instance of UI. So that in our game scene dot cpp, we just need to construct it. So UI equals new UI, colon, colon UI, the path in the layer, which is this, and finally we just need to go to the check, win condition. So check, win, keep scrolling down, check when. So after we have literally done everything You can move this car out if you want. And it was just for testing, we want to check if this date is equal to draw, lose or win.

If it is, then we can actually show the game over. So state draw does it equal to the game state. If it doesn't, the game state equal win state. And if that doesn't play finally equal the lose state. And if so, in here, so this is going to occur after we've placed any piece we've done all our check in so every single time we do a piece place, we will check this so we're going to do you I shield game over the layer just passing this so to draw the objects to it. That's it if you wanted to different do different menu for the different free states drawing when and losing with different items can easily just extend this have different methods in there and just do different if statements for the different states or do a switch statement.

Easily done. So now if we run this hopefully it'll build and we haven't made any errors. Okay, no good so far. Okay, build is successful. Now it's just running it on our simulator. So now if I play the game, nice piece place one here.

Place on here the move the AI wins. Okay, it crushed so sequence it crushed on a sequence. So where did it crash for go to UI, it would have obviously messed up here. That's where it's messed up. It's literally have created and sort of an infant sequence here. We should be constructing it using the menu action pleasing.

So let's just rerun this, to know why I did that. That was just totally a stupid move of mine. So this should be a okay now we shouldn't get any sort of problems like we just did. So just let this run on our simulator. Play. Dice a PC placement here, plays one here, it wins.

As you can see, there's a gray sort of background that fades then you can change the intensity of The gray background or a different image there, we got a menu system for click home takes us back to the home screen. If I click play again or this, I will beat dir this time. As you can see, wait for the animation to appear. I can click the retry button and it retries and I can play again. Okay, so this is the drawing just, let's say the user see it for a bit, and then we can retry again at the moment. Why do we want to quit from here?

What if we want to retry without having to complete this instance of the game? We'll be implementing that in the next video using the pole system that we'll be adding to our UI system. So if you have any questions, feel free to post them on my education platform. So no learning curve okay? Or feel free to reach out via Twitter, Facebook, Google Plus the comment however you feel comfortable. If you want to check out the source code, there will be a GitHub link provided along with the Coco's API guide.

So feel free to check that out for all the different animations, transitions, all of that good stuff. And as usual, thanks for watching, and I hope you have a great day.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.