050 Enemy artificial intelligence in python| python tutorials by payminds | python for beginners
All right so we got a maybe. Two videos left, in this, specific. Project and. I just want to cover a few more things just to make it feel semi complete I do want to mention though and this is something I'm not gonna be doing but. If you watch the videos or if, you do watch the videos not really sure where they are the course but there are videos, dedicated to. Using Jason. Which. Is, basically. A, way, to save, data and then load data from a file what you could do and I, would be really interested in seeing this I would like to do it myself but I don't think we're gonna have time to to. Venture onto that but. If you guys want to do it and send me a link what I would, I think would be really cool is if you could at, the beginning of the script here tried, to load a savefile so, let's say you had a file, called save. Jason. And the, save would contain, a character's. Name, hitpoints. You know all these stats and then also, experience. Points you could create a leveling. System where, your characters, could actually level, up and you. Could create a ton of enemies and then, randomly. You, know, basically. What you would do is for each battle, the, players, would get experience, points and then after each battle you'd have the option to save back into that Jason file and then. Also continue, on to other battles what, you could do is make a list of let's say 50 different enemies, and then, in each battle determine. You know 2 or 3 enemies, to throw into that battle and also. Make, the enemies data, scale. To. These. Guys levels, so. That is a bit complicated and, it would be tedious to watch me do it because, it's, just gonna be covering a lot of the stuff that we've already learned and I, think this, specific. Project is already got too long so I just want to finish it up at this point, get, it done and then we can move on to some other fun stuff. So. What we're gonna be doing I believe, we're at the point right now where one, enemy attacks so we need to make all enemies, attack but we also need to code this into somewhat. Intelligent. Attacks. And give. Them a cure spell maybe some potions, and allow, them to, to. Heal if needed, and that's make the battle a lot more challenging. So. What we're going to do, is. Right here we're gonna say four enemy, in enemies. So, first, we. Need to, indent. This. Bit right here because that's. What. We're working with. So. We need to generate. An enemy, choice between zero, and three. So. Random. Dot rammed range. Between. Zero, and three. And. Then. What we need to do. Obviously. Is, zero, is gonna be attack two, is gonna be magic, and three. Is gonna be item so. What we can actually do. Is. Copy a bit of this so. Actually. This is unnecessary because we've already got that coated so if. Enemy. Choice. Equals. Zero. We're. Gonna print the enemy's name. So. Let's. Say. Enemy. Dot, name. Dot. Replace. I'm. Gonna replace all spaces. With. A blank character. If. They attacks for it then that'll be damaged. Actually. They attack player. So. We're. Gonna print out the players name as well some, players. Target. Name. And. We're gonna replace, all. Spaces. With. That. So. That's just the attack, itself, and what we need to do is we need to put this bit of code before. Our. Enemies so that if. We. Have no enemies left if we drew if we've just killed them obviously. They can't attack and that's the error we were getting in the last video. So. There we go first.
We Check. Check. If battle. Is over. So. Let's add a comment here, check. If. Player. One. Here. Check. If. Enemy. One. So. We're going to label that as enemy attack phase. So. Let's. Just, comment right here. Those. Attack. Elif. Enemy. Choice. Equals. One we, now need to generate a minute a random, number from. The enemy's length, of, magic. So. Let's. Go ahead and. Let's. Set. Magic, choice. Equal. To random. Dot, R and range. And. It's. Gonna be between zero and. Enemy. Magic. And, it's. Gonna be the links so, we need, to wrap that in the links, that's. In here. So. It's gonna generate a random number between 0, and the length of their magic, list so let's say they have 5 spell that's going to generate a number between 0 and 5, so, that's gonna be the magic spell itself so, what, we're gonna do is we're gonna come up here and copy the. Magic. And. We're. Gonna say. That's, gonna be spell and. This. Is, well let's just type this out on. Our own. So. We need to generate the damage and then get their FP so. Magic. Damage, is going to be equal to. Enemy. Dot. Generate. Actually. Magic. I. Believe. We have that in the magic itself let me just check up here what we did. With. The, player here. Yeah. All right so that's what we did we basically said. Spell. Equals. They're. Actually. Individual, spell and that spell generates, damage. So. We're doing that in the same way just in one line and then we need to get their magic points, so if. Enemy. Dot. MP. Is, less. Than. All. Right that's why we did that. So. Because. We're using spell. Multiple, times here, so. MP. Costs. It will actually let's just name that spell right, spell. Equals, enemy. Magic, magic choice so. Spell. Cost. We're. Gonna basically. Rerun. It. Let. Me think about this for a moment. We, could break that out into its own function. Or. Make. This recursive. Which. Would require putting, it in its own function. So. What we're gonna do here, let's. Go ahead and. So. I need to pass in the enemy. So. Let's copy this up here at the top. Let's. Can. We put that into. Into. Its own in, the person class let's, just check.
Yeah. We can all right. So. Let's drop this down. Choose. Any spell. So. I. Mean. It's gonna turn into self. Self. You. Know that should work. Alright. Else. Let's. Return, the. Spell. Itself. Or. The, magic, damage. So. You turn the spell that should, work. Actually. Let's return, spell. And magic damage. Alright. So. Back. Up here. Let's. Say. Enemy. Choose. Enemy, spell. Let's. Print out. Just, make sure we've got these correct, here we're, gonna have to play this a bit to make sure well. Actually to get. Alright, so empty range for red Ridge okay. So we, screwed up there let me just check. Self. Magic. Choice. So. All right because we have a past innate magic to the enemies so. Let's, just let's. Go ahead and give them a few spells as well. The, magic, is gonna be. Let's. Say. That. I mean spells, equals. Fire. Meteor. Here. I'm. Just gonna copy this. There. We go so, it looks like he did actually the enemy did choose magic. Let's, something went terribly wrong this was just attack. And. Enemy. Chose. Spell. So they did choose the spell correctly, which. Is awesome, now all we need to do is actually take that, spell, they chose they had enough magic. Points, to do it we need to reduce their MP, and, we. Need to then. You. Know determine this bit right here so. All. Right. So. Enemy. Reduce. MP. And, we're gonna reduce it by spell. Cost. Then. We're going to print, that right there so, if spell type equals, white we're. Gonna say enemy. Heal. For, magic, damage. So. I'm. Gonna say it heals enemy, dot name. Before. Next. Antigen we're gonna also. We're. Gonna have to turn. This into. Basically. Another if condition, that if their hit points are less than twenty percent or. So then choose a white magic spell otherwise or then randomize it otherwise. You know and just keep rolling until they. Hit of black magic. Spell. So. Let's. Say, their, target, was their target already chosen, no. All, right so let's just talk, like that right there. Target. So. Players. Target. Take. Damage we're gonna pass that magic damage. So. We're gonna do is we're gonna print out. Enemy. Name. Replace. All. Spaces. We're. Gonna go. So. It's gonna say. Imps. Fire. Deals. You know how much damage so. Players. Dot. Target. Get. HP. Print. Players. Target. Has. Died. Then. We're going to. Remove. That player just. For now. So. Let's comment out this and. Let's. See this, wrong. So. In tact. So, still one player is attack. It's, God. Let's. See. What happens now. So. This is on. Y. 202. And. The issue is. List. In dice it must be integers or slices, not personal. Where. Is. What. Oh. I. Didn't. Even see that bit right there. Alright. Let's go ahead and try again. So. M heels, here. Heels in. 634. That. Strange. Magus. Is not attacking. There. Magus it acts. So, it's actually working, for, the most part is. That. It. Keeps, healing, oh. Yeah. This is definitely gonna make it a lot more difficult. Nobody. Attacked that diamond oh. You. Know what so. Here's here's. The problem, is we're generating the. Wrong numbers. So. Enemy choice there because we don't have three right now we don't have item we just have wanted to that's why some of them weren't, attacking.
So. There we go so we've got three, people attacking. Correctly. And. All. Right good, stuff so, in potox Magus, is meteor deals, damage so, this is looking a lot better now what we need to do is just make sure they're not using white magic unless their, hit points are less than 50%, so. What we're going to do is. If. Spell. Type. Equals. White. That's. In the yeah okay. Or. Spell. Type. Equals. White, and. Self. HP. Wow. Is. Going to be equal to. Let's. Say. Self. HP. /. Self. Dot max HP. Times. So. And. Percent. Of. Their. Hit points. Is. Over. 50. Then. They're gonna choose a spell again. There. We go so, if if, they don't have enough MP or if it's white magic and they have more than 50% health they're gonna choose again they're gonna choose black magic, so, let's go ahead and save this make sure it works we should see enemies not healing. Unless. They, are. Under. 50% health. So. Let's go ahead and start this where again I'm just going to attack just for the safe of it. Alright, so we. See the imp is really, low, we. Have, a bit of an issue there, so. When, the enemies attack we need to skip one more line so, there. Is four. Enemy, and enemies right here so we'll go, print. And. Then above. Magic. We. Need to remove that one right there. There. We go. All. Right so. That's. Fine for now we should, we're. Gonna play, a little bit and see if we can see a heal. Actually. We're not going to get to that point because we are going to lose. All. Right so now we need to do a bit of healing let's just use one of our mega elixirs. All. Right. So. Magus. Attacks, Valis. There. We go we're still good let's go ahead and. Attack. And. We might see. A. Heal. Here soon. Let's. Give it another go see what happens. Here. Of his, cure is so, bad. Especially. For his. Let's. Give him a different cure spell so. Let's, go ahead and make yoga. Which, again is from. Final. Fantasy, and. It's. Gonna cost. 50. It's. Going to cure for. 6,000. It's. Going to be white this is going to be the only. White. Magic. That enemy's house, so, if, he does he'll its gonna do a lot of healing it's gonna heal 6,000 so that should make it more difficult so, I think we're good we're not gonna go through the items the items would be the same thing for an enemy but in. Games, enemies. Don't typically, use items. So we don't even need to worry about that so, I think we're, done with this this, was a lot, of fun actually I really enjoyed this this, code base is going to be updated, on the, github repository. So, go ahead and go, over to, and I'll put the link up there it is. Github.com. Slash. Nick. Germain. So. I'll put this link in the video's discussion, section, you, guys can go check it out if I haven't already I'm not sure I'll have to check the, repositories. In my list of repositories, there's also other repositories there, some. Of them also which do relate to this course as well so. Thank. You guys so, much for watching this section of the course I. Personally. I'd, love to spend like so much more time on this but I just don't think we have the time and we're, gonna be moving on to other more, awesome things as well and at some point we, are going to be covering a video game creation, library. For Python, what, I'd really like to do if we, have, enough time to do it would. Be to put a graphical. Front-end to. The logic. In this game I think, that would be super, awesome and we already have a lot of this created, so we would just need to plug it in to the, PI. Game that's what we're going to be using. So. I'll see you guys in the next video.