Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 7: Wizard Battle App
Lecture: Building the game loop
0:00 Over here in PyCharm, let's knock out the first pass on our initial game loop. Now, it's probably not going to end this way,
0:08 or probably refine it and add some enhancements, but let's just get started in kind of the same way we have with other apps.
0:15 So, again, we are going to add our program.py file and we are going to have our main() method, we'll have our print_header(), as always
0:27 and in this one we are going to have what we are going to call a game_loop(). And then in our main let's just go and say print_header()
0:33 and then we'll just run the game_loop(). And finally, let's use our PyCharm live template to call the main()
0:42 only if it's actually being executed rather than imported. So header is pretty standard as always so let's just do this,
0:56 ok, standard header now let's focus in on this game_loop() we have here. So the concept of our game_loop() is we just want to go around and around
1:04 getting input from the user until the game ends so let's just start really simple with the concept of going around and around,
1:12 and so we'll just say while true and we are going to do some work here. First thing we are going to do is get some input from the user,
1:19 so we'll call this cmd for command and this is pretty standard we'll say you attack, run away or look around.
1:29 Like so, and let's give the users hints on which they can do so we'll say attack, run away or look. And then we have four basic outcomes here,
1:39 one could be they actually said they want to attack and let's just print out some things we can kind of test a little loop here,
1:46 then we'll rewrite the behaviors. So if they say a we'll say attack, elif if it's run away we'll just print('run away'),
2:00 if they are going to look around we are going to print('look around'), and finally,
2:05 if they hit something else and we don't know what it is we'll just say all right, you must want to exit the game,
2:10 like if we just hit enter with no command that means you are gone, so we'll just say something like this, ok, exiting game. Bye.
2:18 So, let's go ahead and run our app just to make sure everything is hanging together. Now, no run configuration, so we kow how to get that going.
2:27 All right, so down here we can attack, we can look around, we can run away, or we can say enter and we should exit.
2:34 Now, oops, we didn't really exit we just printed exit, right, that doesn't mean anything to Python just that we printed exit.
2:41 So we can exit out of this infinite loop by just saying break and then we'll come down here and basically
2:47 run the next line which is empty and we're done. So look around, enter, exit, perfect.
2:52 So our little loop is running, it's time to build up the data structures with classes and objects it's going to be great.