 Hi everyone, it's Elise. I'm making a video just to demonstrate some things you can do with conditions a bit further. Matt did a video or lecture on this on stream and I'm just going to take you into further detail and talk about my thoughts when it comes to making decisions about where you might need conditions. So I'm starting with a program that already has some stuff in it because this is just some stuff you would have learned early in the semester. I want to make this video more about the conditions. So I'm just starting with a basic player. His name is Box Carrier and a ground and a background and some variables to help me build the Box Carrier player and also the ground location and everything else is fairly straightforward. So the first thing I want to do in this program, if I just bring up my program again, is I want to be able to have something that follows the mouse like this, which I haven't done yet. So if I press play, this is an older version I was just showing you. Okay, so currently we don't have that thing following the mouse. What we want is a paint drop that's going to be stuck to the mouse position. So if we start with a paint drop, let's just make it, let's give it some X and Y values. So int, I'm just going to call it paint for now, maybe drop, drop is better. So let's give it a drop X location and a drop Y location just so we can keep track of where it is on the screen. And then what we're going to do is we're going to first give it what it's X and Y should be. So currently the X and Y of that drop should be where the mouse is, right? So drop X equals mouse X, drop Y equals mouse Y. And then we're going to draw the, an ellipse at that location. So we got the variables drop X, drop Y, and let's give it a radius or diameter of 30 each direction. Okay, so when we press play, we should just have something that follows the mouse now. So it's using the drop X and drop Y variables that we made, but you can directly use mouse X and mouse Y and change your code to make it a bit differently up to you. But this is how I'm going to do it here. So now we're going, we're coming to a decision. So we, what do we want to do in this program? When I click, I want this drop of paint to fall and no longer follow the mouse. So in order to do that, something has to branch follow a different path. If I was to click, it's going to do one thing in my code. And if, if I don't click, I want to follow the other path. So if I just bring up Photoshop, my little whiteboard here is our code following down. When I click, I want the paint to fall, paint falls. And when I don't click, I want it to just do a little X here to show not clicking. When I don't click, I want the paint to follow the mouse, follow mouse. And then no matter what, afterwards, they should continue the code the same way. So this is when we come to something like this, where we need to split off to two different paths, we're going to need a condition. So we're going to need some kind of if here. So I might just build it at the bottom here. So we've got some, some more space. Okay, so if something, we don't know what that something is yet, I want one branch to follow the mouse. Actually, instead of following the mouse, let's say this branch here is drop the paint. And else, in the other case, I want it to follow the mouse. So this is where we'd use our, our condition. Okay, so what is that something where well, we don't actually have an in belt variable that we can really use for this because we want to keep track of something that we click once, and then it's going to continue to keep that, that thing true. So I'm making a variable up here, a bullying and I'm just going to call it falling, I can make it is falling to make it clearer. So something is falling. That can be true or false. Is it falling true? Is it falling false, etc. So I'm going to start that off at false at the start of the program. And therefore, I can make this here falling. Okay. Now we need some way to trigger if falling is going to be true or false. And when do we want to make falling true? Well, we want to make it fall when we click the mouse, right? So we have to make a mouse event void mouse pressed. All I want to happen when the mouse is pressed is that the paint can now fall falling equals true. Okay, so if it's falling, we just want to increase the y drop y plus plus. That's good enough for now. So the drop y is just going to increase incrementally over the draw loop. And otherwise, we want to follow the mouse. So in fact, I can just put in this code here, where the mouse x mouse y variables are copied into the drop x drop y variables. And then afterwards, we can draw our lips. And I'll put that down here. Because no matter what the lips gets drawn, so we can, we know we can put it here. That's no problem. Okay, it's complaining about falling because I've changed your name up here. So I'll just change this back to falling. Press play. So currently we have something that follows the mouse. And if I click, it falls, which is fantastic. So nothing else is happening when I'm clicking, and it's going to fall in forever. So currently we don't have any way of making it false. So we want it to happen when we hit the ground. Ideally, we set it back to false, so it's no longer falling. And it will go back to the mouse. So I'm going to say when it's falling here, drop y plus plus. If that drop y is more than ground y, and this is why I had that ground variable, ground y is just tells me where is that rectangle on the ground. If drop y is more than ground y, falling equals false. That's all I'm going to do for now. So this is a branch here where it doesn't necessarily have the two parts like this one here. Instead, we have, it does something and if something here happens, so we've got this if branch, if it hits the ground, you've got to do something over here and otherwise just keep going. And eventually this one will meet up with the main path here. So unlike this scenario here, my mouse, my pen lagged out, unlike this scenario here where there's two different ways you could go, and you need to pick one or the other, this one here simply just has the continue doing things as normal, unless you meet the conditions of this if, and then you have to go down this path and you'll reconnect later. Okay, so let's test this one out here. I've just added in that drop y. If the drop is more than the ground, which is this rectangle here, it gets set to false and it snaps back to the mouse. I don't know if you saw that. Yeah, snaps back to the mouse, which is excellent. That's exactly what we want to do. Okay. So now that we have the mouse part working, let's add in some things about this character. I'd like to move box carrier left and right. And for that, we're going to need some events with the keys. So let's add in void key pressed. And in here, I'm going to use a branch. So because if I put anything in here, so if key pressed, or sorry, when the key is pressed, I'm going to change the background to actually that's about example. I'm going to change the fill color of everything to actually it's not going to work either. I'm going to change the stroke to 50 something really noticeable. Right. So anytime I press a key, so I'm just pressing the, I'm just pressing B, B, nothing's happening. Okay, we see some stroke here. Oh, stroke, stroke, wait, sorry, I was trying to make something super noticeable. All right. I'm picking a different key this time. I picked, I picked T. It's running. Things are changing. Again, nothing's changing because nothing's, of course, your demonstration doesn't work while you're recording at least. No worries. Basically, anytime that I press key pressed, something's going to happen on the screen. Oh, it's because I was maybe I had something else highlighted. Anytime that I do a press a key, anything in here is going to be triggered. But I don't want it to always be triggered. I want it to have only happen for certain events, like a path. So just like we had in this example, I want everything to happen as normal, unless there's an if like we hit that condition. So if key, now key is a variable that stores the last key pressed on your keyboard. So if the last key pressed, I pressed is equivalent to character a, I want box carrier to travel to the left. So I'm just using WASD as the keys right now, so mainly A and D. So box carrier to travel left, I've got player X minus, oh play X minus equals 10 equals X play X minus 10. Just so I can just show you guys. Okay, so the character moves 10 pixels every time I press the key A. Now to make it a bit smoother, I'm going to just gonna make it look a bit nicer. I'm going to add in a player speed. So int, I'm just going to call it speed, maybe p speed because we might have a drop speed later. Okay, in player speed equals zero currently. And then when I press a p speed should equal three. Okay, now it should be nice and smooth. Oh, we have to increase and draw. So where am I drawing my character? Here's the character player X equal plus equals p speed. This is just like every frame I'm incrementing the player X position by the speed. Okay, so now I'm going to press a letter that's not A. Nothing's happening. Now I'm going to press a letter that is A. Excellent. It increments to the right because I'm an idiot and didn't put negative three. Okay, there we go. So now I press A, it moves to the left, moving very fast. Okay, now I want to have something that moves to the right. If key is equivalent to D, then I want p speed to equal three. And we'll just test that one out. So I press this way. Yep, I can press this way. Now it's not stopping when I don't press either of them, which is might be a bit of a problem for us, but I'll get to that in a minute. And if I press both at the same time, it doesn't really work there either. It might pick one over the other. Okay, then that's all we need to do there. Now you can also say else if here. So there's times where you want it to pick one or the other or nothing. So I might just do this example in Photoshop 2. So if I have an if else, oh, sorry, an else if I should say, it could do this thing, which is an if, or it could do this thing, which is the if else, it could do another if else, it could have as many if else as you want. And otherwise, it will do the else. So it's kind of just like an if, but we're kind of combining them. You can have lots of different types. And I should say that they all will meet up with each other at the end and continue to execute the rest of the code together. Okay, I'm actually not going to do that this time. But as an extra exercise, if you would like to maybe make put in some mechanisms that stop the players from being able to press both keys at the same time, that's something you can try and do. So currently, we can press a I can press left and right in our WASD keys, and we can move back and forth. But I want it to stop eventually. So when I'm not pressing anything, or when I release a key, I might just add in that now. So void key released, speed equals zero. I'm not really doing anything fancy at the moment, because I don't really want want to go into doing speed and player controls when we're about conditions. But that's something you can do. So currently, it doesn't doesn't keep moving. Okay, moving on. So currently, we have paint dropping, we have a player being able to move. And I want something to happen when the player collides with this paint drop. So what am I going to do? Now, I'm going to do something where the paint drop is a certain color, and if the color hits the player, the player is also colored that color. So in order to do that, I'm just going to add in some extra variables. So instead of coloring the player black, I might color them. I might make some variables first. So int player r int player g and int player b, so rgb, but for the player. And I'll probably just make them equal, like bright green to start with just so it's super noticeable. Oh, sorry, not the player, the player should start as black. So I'm just going to make those that and the paint drop should be different ones. So int d r for drop. So maybe I'll make little comments. Player color. And this is the drop color. d r equals zero int dg equals just make this 255. So it's gonna be bright green and int d b is equal to zero. Okay, so I should, when I'm coloring this drop, when I draw this drop, I should give it a fill color. And it's going to be the drop r drop g and the drop b. And when I'm coloring the player, I should color it similarly where I have player r, player g and player b. So when I just press play, let's test it out. Yep, so we currently got the player as a dark color, got black, and we got the paint drop as green. And that should stay when I drop the paint drop there. When it teleports, yeah, exactly the same. Okay, so now I want it to have that when I drop my paint drop and collide with the player here, then he colors himself green. Now there's things you can do with like more hitbox kind of things for those of you who know a bit more about games. And you can get into the specifics with putting in like the drop radius and making it, you know, all pretty, but I don't know if I'm going to get into that in this video. Okay, so where was I? Okay, so when it's dropping, so let's look at the if falling. So at this point, the drop is dropping down, this is dropping down basically. If it's hit the ground, it's gone too far hasn't hit the player. So I'm going to have to put something here. If, and I don't know what I'm going to put here, the player is hit, question mark. If the player is hit, I want the player to change color, I want the player to become the player, the color of the drop. So player R is going to become drop R. Player green is going to become drop green. And player blue is going to become drop blue. That's also going to happen. Okay, so again, we have this path where normally nothing would happen. We'd skip this. But if we have the player is hit, we have to follow a different path and do something else here. So how do we tell if the player is here, we're going to have to have a bully in here. Now conveniently, I set up these player X and player Y variables beforehand. So if player, or sorry, if drop Y is more than player Y. So that's the white wooden if it's, if it's hit the player's head somewhere, then it's hit and drop Y, or sorry, drop X is more than player X. So it's currently hit the left side of the body. It's kind of to the right of the very left of the body, which is hard to explain. And drop X is less than player X. Now how wide is my player? Just trying to think about this. I think I think it's about 60 pixels. So I might just say plus 60. Okay, if all these conditions are satisfied and they might not be correct, I can just adjust them later. The player has been hit. So let's test that out. Okay, the player has been hit. Obviously, these hitboxes aren't fantastic. We haven't adjusted them too well. Instead of plus 60, you might make it plus 80 and make it a bit more generous. So the pain is falling. I'm moving around. If I get hit, I turn green. And if you notice, there was a bit of a gap where the circle overlapped a little bit, and that's because it's measuring from the center of the circle and also the very top corner of this, of my box carrier character. So if you want to make it more specific, you could do something like if drop Y is more than player Y minus drop width over 2, and then you'd have like some kind of drop width variable. In our case, our drop width is 30, so we can say minus 15. And we get perfectly in that hitbox. So something to note, you could do that with the X's as well. So now we've got the player changing color. I also want the drop to change color. So when do I want the drop to change color? When it hits the ground, let's just make it random. So here we got the drop has hit the ground. So as well as falling equaling false, let's also change the color. So drop R should equal something, drop G should equal something and drop B should equal something. And I'm just going to make this random 255. So it's just going to be a random color from 0 to 254. So I guess I should make it 256. Okay. And the other thing I should note is that this here will evaluate to a float, but dr, which is our drop red is actually of type int. So it's going to complain. So I'm going to just use an inbuilt function called floor. And all this does is turn something into a and integer from a float. So I'm like this. And I'm just going to copy and paste this into all of them. Okay. So now when my drop hits the ground, it changes to a random color, which is really nice. Now there's some like visual bugs. You can see where the drop is visible in front of the ground. If you don't want that, simply just draw the ground on top. So currently ground is being drawn up here. Let's copy and paste that and move it down here. Let's just move it right to the end of our code. There you go. Now the drop goes behind. Also, it's kind of, even though now it's not visible down here, it only you can visibly see it change color here and doesn't fully disappear. Again, that's a radius thing. So if I wanted it to drop completely below, then when I check if the drop is hit the ground, let's make it, it's hit the ground plus half of its radius. So half 15 pixels. If it's radius is 30, sorry, not radius, width and height. So half of its width and height, aka the radius is 15. So now when I drop it, it goes completely off the screen and then changes color, which is nice. So now we can do fun things with that character where he changes color, goes off the screen and we get to do it again. There you have it. That's the game. So there's some other things that you can do here. I would highly encourage you to try this yourself or try something similar, but there's lots of things that you can add on. So you can add on an eye to the left or right side of his face and maybe when you press A or D, he changes directions visually. So you can see, oh, he's facing left or he's facing right. You could also make the drop speed up faster. You could have it affected by gravity. So maybe accelerates as it falls. You can add in more complicated controls. You can add in a point system that displays the console just by using print. Yeah, there's lots of things you can do. You can try and make the drop look more like a drop with a triangle on top, like a raindrop shape, like a pear, something like that. But yeah, hopefully you guys enjoyed watching this and learned a bit more about conditions and variables. Thanks.