1 00:00:00,000 --> 00:00:02,822 . In this video, we're going to talk about 2 00:00:02,822 --> 00:00:06,460 the programming model that we're going to use for this class. 3 00:00:06,460 --> 00:00:09,659 We're going to use the event driven programming model. 4 00:00:09,659 --> 00:00:14,676 And this is what's going to allow us to build the interactive applications that we 5 00:00:14,676 --> 00:00:18,879 wanna build for this class. Coincidentally, event driven programming 6 00:00:18,879 --> 00:00:23,771 is also my favorite programming model. [laugh] Alright, there is no coincidence 7 00:00:23,771 --> 00:00:26,656 here. I am one of the professors teaching this 8 00:00:26,656 --> 00:00:29,353 class. Obviously, I'm going to teach you one of 9 00:00:29,353 --> 00:00:33,180 the models that I like best. So let's start learning about it. 10 00:00:33,800 --> 00:00:38,691 Alright, so far the programs that we have written have sort of executed in this 11 00:00:38,691 --> 00:00:41,044 linear fashion. What do I mean by that? 12 00:00:41,044 --> 00:00:46,880 Well, the program starts. And then we run, you know blocks of code. 13 00:00:46,880 --> 00:00:53,187 There's a, then b, then c, then d. And we keep going, and eventually your 14 00:00:53,187 --> 00:00:56,512 program ends, okay? And each letter here, you know, like A, 15 00:00:56,512 --> 00:01:01,289 this corresponds to some block of code. This might be the body of a function, it 16 00:01:01,289 --> 00:01:06,126 might be part of an if statement, or the block of code after an if or an else, or 17 00:01:06,126 --> 00:01:09,451 whatever, okay? But after that executes you know exactly 18 00:01:09,451 --> 00:01:13,623 what's going to execute next. The only thing that can change the flow of 19 00:01:13,623 --> 00:01:16,162 control of your program is the data, right? 20 00:01:16,162 --> 00:01:20,576 So it doesn't mean that the programs execute exactly the same every time, 21 00:01:20,576 --> 00:01:23,418 right? The different input data, maybe I do an A, 22 00:01:23,418 --> 00:01:26,237 C, G. H, you know, Z, whatever, and eventually I 23 00:01:26,237 --> 00:01:28,881 end. Okay, so this doesn't mean that your 24 00:01:28,881 --> 00:01:34,499 program is exactly the same every time, it means that the flow of control is dictated 25 00:01:34,499 --> 00:01:38,002 by how you wrote the program and what the inputs are. 26 00:01:38,002 --> 00:01:43,422 So that when you get to the conditionals in the program, you know which way you're 27 00:01:43,422 --> 00:01:46,793 going to go. And I can write very complicated and very 28 00:01:46,793 --> 00:01:52,014 powerful programs using this model, okay. Now, in this, in this class instead 29 00:01:52,014 --> 00:01:58,407 though, we're going to look at the Event-driven programming model, alright? 30 00:01:58,407 --> 00:02:04,900 And what's different, okay? It does not look like this above here 31 00:02:04,900 --> 00:02:11,799 instead, my program starts. I initialize every thing so I need to set 32 00:02:11,799 --> 00:02:14,780 my program up. And then. 33 00:02:14,780 --> 00:02:20,247 I wait, and this is the key difference. My program effectively just ends and 34 00:02:20,247 --> 00:02:22,649 starts waiting, doing nothing else, Okay? 35 00:02:22,649 --> 00:02:27,700 And nothing now is going to happen in my program until some event occurs, and that 36 00:02:27,700 --> 00:02:32,442 doesn't mean that you can't wait up here. So imagine, you know, block H here is 37 00:02:32,442 --> 00:02:37,075 looking for some sort of file input. So maybe it's waiting for you to type the 38 00:02:37,075 --> 00:02:42,009 name of a file in, so it can read the name of the file in the next Q block C and move 39 00:02:42,009 --> 00:02:45,492 on from there, right? The difference is, is block H is waiting 40 00:02:45,492 --> 00:02:49,032 for a specific input. I'm waiting for you to type a file name. 41 00:02:49,032 --> 00:02:52,108 Down here, I'm not waiting for anything in particular. 42 00:02:52,108 --> 00:02:53,850 I'm just waiting. So any event. 43 00:02:53,850 --> 00:02:58,777 Can occur, and I will respond to it. So some time in the future, some event 44 00:02:58,777 --> 00:03:02,775 occurs We'll talk about what events are in a second, all right? 45 00:03:02,775 --> 00:03:07,454 And when that event occurs, I run some function which is called a handler. 46 00:03:07,454 --> 00:03:12,453 That handler executes, when it completes I go back to waiting, some time in the 47 00:03:12,453 --> 00:03:17,116 future, another event occurs, right? I execute a, another handler and this 48 00:03:17,116 --> 00:03:21,489 handler is probably different because that event was probably different. 49 00:03:21,489 --> 00:03:26,408 When that's done, I go back to waiting. So now most of the time my program is just 50 00:03:26,408 --> 00:03:30,113 sitting there waiting. Its not doing anything but whenever an 51 00:03:30,113 --> 00:03:35,093 event occurs it immediately then goes off, runs a handler and executes the code in 52 00:03:35,093 --> 00:03:36,144 response. Alright? 53 00:03:36,144 --> 00:03:41,058 Now eventually, to end my program, I get some sort of event that corresponds to 54 00:03:41,058 --> 00:03:41,751 quit. Right? 55 00:03:41,751 --> 00:03:44,082 Basically says, you're done. All right? 56 00:03:44,082 --> 00:03:46,917 And then the program ends at that point. Okay? 57 00:03:46,917 --> 00:03:52,273 So the primary difference here is that on, on the top my program just keeps executing 58 00:03:52,273 --> 00:03:55,738 code forever. On the bottom here, I keep waiting forever 59 00:03:55,738 --> 00:03:59,518 until events occur. This is a fundamentally different way of 60 00:03:59,518 --> 00:04:01,830 thinking about programming. Okay. 61 00:04:01,830 --> 00:04:06,920 To better on your stand the event running programming model we have to talk about 62 00:04:06,920 --> 00:04:11,141 events, what are these events? Okay and there is a bunch of different 63 00:04:11,141 --> 00:04:16,169 kinds of events and it really depends on what type of system you're running, what 64 00:04:16,169 --> 00:04:20,018 the events of interests are. So we're going to focus today on the 65 00:04:20,018 --> 00:04:24,177 events that you're going to see in this class that we're going to use. 66 00:04:24,177 --> 00:04:29,205 All right and I'm going to break them down to four categories and see some input 67 00:04:29,205 --> 00:04:32,180 events. Keyboard. 68 00:04:32,720 --> 00:04:40,750 Events, mouse events and timer events. And in these categories there's actually 69 00:04:40,750 --> 00:04:46,309 some subcategories. The types of input events might be 70 00:04:46,309 --> 00:04:51,330 something like a button. Or a textbox input. 71 00:04:51,330 --> 00:04:58,200 Right type of keyboard events you might see are key down. 72 00:04:58,200 --> 00:05:02,974 Events, or key, up events, when you release the key. 73 00:05:03,266 --> 00:05:11,257 On the mouse, you might see a click event or a drag event, and a timer is something 74 00:05:11,257 --> 00:05:15,623 that just happens periodically. Right? No matter what. 75 00:05:15,623 --> 00:05:18,030 Okay. So these are the different kinds of events 76 00:05:18,030 --> 00:05:20,789 that you are going to see in our system as we build games. 77 00:05:20,789 --> 00:05:24,901 You might push buttons to set up control, you might type things into a text box to 78 00:05:24,901 --> 00:05:28,763 give information to the program. As you're controlling your game, if you're 79 00:05:28,763 --> 00:05:32,776 moving something around in the game, you might use the key pad to control at the 80 00:05:32,776 --> 00:05:36,688 arrow keys, and when you push them down you do one thing, when you release them 81 00:05:36,688 --> 00:05:39,597 you do something else. On the mouse, you might click on the 82 00:05:39,597 --> 00:05:42,907 elements in the screen and you might move them around some really. 83 00:05:42,907 --> 00:05:46,920 So these are the kind of events that we're going to see in our system, and for each 84 00:05:46,920 --> 00:05:51,768 event that you care about, so for instance, let's say, a button event. 85 00:05:51,768 --> 00:05:55,170 You're going to then write some sort of a handler. 86 00:05:55,170 --> 00:05:59,136 And you're not going to write one handler for all button events. 87 00:05:59,136 --> 00:06:03,969 When you create a button in your system you write a handler that handles that 88 00:06:03,969 --> 00:06:05,395 particular button. Okay? 89 00:06:05,395 --> 00:06:10,228 So most of the time your system, as we said, will be sitting around waiting and 90 00:06:10,228 --> 00:06:15,434 when something happens, like a button gets pushed, then you go off and you'll run the 91 00:06:15,434 --> 00:06:17,987 handler. So, let's take a look at our first event 92 00:06:17,987 --> 00:06:20,867 driven program. Here it is, you can see it's pretty short. 93 00:06:20,867 --> 00:06:23,954 It's only fourteen lines including blank lines and comments. 94 00:06:23,954 --> 00:06:27,451 So, we are going to be able to walk through the entire program and it's 95 00:06:27,451 --> 00:06:30,692 hopefully, we will get a good understanding of what's happening. 96 00:06:30,692 --> 00:06:34,910 Now, we don't know how to build the user interface yet, and most of the events that 97 00:06:34,910 --> 00:06:39,076 I have talked about have to deal, do with the user interface, clicking, buttons and 98 00:06:39,076 --> 00:06:42,420 moving the things like that. So, we are just going to look at a timer 99 00:06:42,420 --> 00:06:44,683 event. The timer events are critical for any 100 00:06:44,683 --> 00:06:48,798 event driven program, as we've said most of the time the event driven program is 101 00:06:48,798 --> 00:06:52,312 waiting, doing nothing Thing. So unless you have a timer event that 102 00:06:52,312 --> 00:06:56,320 fires periodically, your program can't do anything unless the user does it. 103 00:06:56,320 --> 00:06:59,446 Does some action. So instead of waiting for the user to 104 00:06:59,446 --> 00:07:02,801 perform an action, we are just going to have the timer fire. 105 00:07:02,801 --> 00:07:05,302 And then we'll do something on our own. Okay? 106 00:07:05,302 --> 00:07:06,780 Now. Let's look at the code. 107 00:07:06,780 --> 00:07:10,962 Alright, so if I get to the first line that's not a comment, you can see on line 108 00:07:10,962 --> 00:07:14,109 four that we are importing simple GUI What's simple GUI? 109 00:07:14,109 --> 00:07:18,227 Well, Simple GUI is a Python module that we've written for you to allow you to 110 00:07:18,227 --> 00:07:22,499 build interactive applications in Python. And in a future lesson, we'll cover Simple 111 00:07:22,499 --> 00:07:25,536 GUI in much more depth. For now, I don't wanna get into it. 112 00:07:25,536 --> 00:07:29,190 We're just going to mostly ignore it, and I'll just give you the pieces of 113 00:07:29,190 --> 00:07:32,484 information that you need, okay? Next, we're going to define an event 114 00:07:32,484 --> 00:07:34,852 handler. And this event handler, I'm going to call 115 00:07:34,852 --> 00:07:37,014 it Tick. You can call it anything you want. 116 00:07:37,014 --> 00:07:40,822 But it takes, it's a function that takes no argument, arguments, and it does 117 00:07:40,822 --> 00:07:43,808 something silly. All it does is print the word tick to the 118 00:07:43,808 --> 00:07:44,550 console. Okay? 119 00:07:44,550 --> 00:07:48,517 Next, on line eleven, I'm going to actually register the handler. 120 00:07:48,517 --> 00:07:51,558 So this is where simple GUI comes into play. 121 00:07:51,558 --> 00:07:56,650 I'm going to call a function in the simple GUI module called create timer and. 122 00:07:56,650 --> 00:07:59,440 As the name implies, this going to create a timer. 123 00:07:59,440 --> 00:08:03,540 The first argument is the number of milliseconds that you want to occur 124 00:08:03,540 --> 00:08:07,697 between each time the timer fires. Alright, so I put 1,000 here, that means 125 00:08:07,697 --> 00:08:10,488 the timer event is going to occur once every second. 126 00:08:10,488 --> 00:08:15,271 The second argument is the function that you would like to call or the handler that 127 00:08:15,271 --> 00:08:19,258 you would like for this timer event. And I give it tick, so this is the 128 00:08:19,258 --> 00:08:21,991 function tick that we're going to execute, right. 129 00:08:21,991 --> 00:08:25,004 Then finally on line fourteen. We call timer.start. 130 00:08:25,004 --> 00:08:27,286 This starts the timer running. Okay. 131 00:08:27,286 --> 00:08:30,220 So let's run this, and let's see what happens. 132 00:08:31,900 --> 00:08:35,105 'Kay, tick. So hopefully you can see that it's 133 00:08:35,105 --> 00:08:40,934 printing tick about once every second. And this is exactly what I want to happen, 134 00:08:40,934 --> 00:08:44,505 right? I told it that every 1000 milliseconds you 135 00:08:44,505 --> 00:08:50,334 should fire an event, that event occurs, the tick function then runs, it prints to 136 00:08:50,334 --> 00:08:51,500 the console. Now. 137 00:08:51,500 --> 00:08:54,767 You're going to notice something here, I can't stop this program. 138 00:08:54,767 --> 00:08:58,891 There's no [laugh] kind of stuff stop event here, so it's going to keep printing 139 00:08:58,891 --> 00:09:01,837 tick forever. All right, the way that I can stop this is 140 00:09:01,837 --> 00:09:04,409 actually hit the reset button and code sculptor. 141 00:09:04,409 --> 00:09:07,462 Now my program is done. All right, I want to point out one 142 00:09:07,462 --> 00:09:09,872 additional thing about what's happening here. 143 00:09:09,872 --> 00:09:13,140 You'll notice that after I call timer.start on line fourteen. 144 00:09:13,140 --> 00:09:14,278 The program ends. Okay? 145 00:09:14,278 --> 00:09:16,658 There's no more code here. The program is done. 146 00:09:16,658 --> 00:09:19,089 Alright? This is a key to event driven programs, 147 00:09:19,089 --> 00:09:22,607 where they just seem to end. That's when you go into that wait state. 148 00:09:22,607 --> 00:09:25,142 Alright? And hopefully, I've already convinced you 149 00:09:25,142 --> 00:09:28,660 that the program works, because you saw that, that tick was happening. 150 00:09:28,660 --> 00:09:31,506 But after the program ends, you go into that wait state. 151 00:09:31,506 --> 00:09:33,938 And then you start waiting for events to occur. 152 00:09:33,938 --> 00:09:36,783 And in this program. Those timer events are going to occur. 153 00:09:36,783 --> 00:09:40,250 And then we run the tick handler. And it'll keep doing that forever. 154 00:09:40,250 --> 00:09:44,397 So there's one final concept that we need to understand in order to completely 155 00:09:44,397 --> 00:09:47,757 understand event driven programming and that is the event queue. 156 00:09:47,757 --> 00:09:52,010 Alright, now there's nothing that you can do to stop two events from happening at 157 00:09:52,010 --> 00:09:55,317 exactly the same time. For instance imagine a timer event fires 158 00:09:55,317 --> 00:09:58,100 at exactly the same time that a user pushes a button. 159 00:09:58,100 --> 00:10:01,670 Alright, so how do we handle that? Well the system handles it for you. 160 00:10:01,670 --> 00:10:05,975 So internally there is something called an event queue and you have, you never see 161 00:10:05,975 --> 00:10:10,122 this and you have no control over it. But it is basically taking events as they 162 00:10:10,122 --> 00:10:14,836 happen and the system puts. These events in this cue so the click 163 00:10:14,836 --> 00:10:19,520 event happens, the timer event happens, okay, a key down. 164 00:10:19,520 --> 00:10:23,411 Event happens and so on. So as events happen in the system, the 165 00:10:23,411 --> 00:10:26,988 system puts it into this event queue. Which you never see. 166 00:10:26,988 --> 00:10:31,381 This all goes on behind our back. Now, remember your program is sitting 167 00:10:31,381 --> 00:10:33,417 there waiting. For an event to happen. 168 00:10:33,417 --> 00:10:37,545 So now what the system does is when your program is waiting, it looks in the event 169 00:10:37,545 --> 00:10:39,910 queue. If it sees an event, so right now there's 170 00:10:39,910 --> 00:10:43,937 a click event, it'll says, oh let's take this click event, take it out of here, and 171 00:10:43,937 --> 00:10:47,699 let's figure out which handler to run. Now behind your back, automatically, it 172 00:10:47,699 --> 00:10:50,328 then figures out the handler, then it runs the handler. 173 00:10:50,328 --> 00:10:53,930 Now your code is actually running. Okay, when your code is done, you go back 174 00:10:53,930 --> 00:10:56,588 to wait. The system then looks in the event queue. 175 00:10:56,588 --> 00:10:59,464 It sees hey there's a timer here. I'll take that out. 176 00:10:59,464 --> 00:11:02,563 Figure out what him or I should run now. And so on right. 177 00:11:02,563 --> 00:11:06,490 And it'll keep doing this untill there are no events left in the queue. 178 00:11:06,490 --> 00:11:10,363 At that point right the system will then say oh, there's nothing to do. 179 00:11:10,363 --> 00:11:13,461 You will wait forever'till another event occurs, alright. 180 00:11:13,461 --> 00:11:17,831 And you, you can't control the order this happens but it shouldn't matter right. 181 00:11:17,831 --> 00:11:22,534 Your program should be written to respond to events and do what needs to happen when 182 00:11:22,534 --> 00:11:25,521 those events occur. No matter what order they occur in 183 00:11:25,521 --> 00:11:27,790 alright. And I wanna make one final point. 184 00:11:27,790 --> 00:11:30,546 When this handler is running, nothing else can happen. 185 00:11:30,546 --> 00:11:33,980 If more events happen, they just go into this internal event queue. 186 00:11:33,980 --> 00:11:36,320 Alright? So while your handler is running, you 187 00:11:36,320 --> 00:11:40,222 can't run and process any more events. So what this means is if you build a 188 00:11:40,222 --> 00:11:43,811 handler that is really long or maybe has an infinite loop in it, even. 189 00:11:43,811 --> 00:11:46,880 Then you're stopping all other events from being processed. 190 00:11:46,880 --> 00:11:50,417 This is going to make your, your program or your game very unresponsive. 191 00:11:50,417 --> 00:11:53,539 So just keep that in mind. Now you've seen the event driven 192 00:11:53,539 --> 00:11:56,435 programming model. Iin this model you first write your 193 00:11:56,435 --> 00:12:00,833 handler functions and then you register those functions so they will get executed 194 00:12:00,833 --> 00:12:03,782 in response to some events. Your program then just ends. 195 00:12:03,782 --> 00:12:07,697 At that point the system takes over. And while the system is running your 196 00:12:07,697 --> 00:12:11,934 program is waiting most of the time. Whenever an event occurs it first gets put 197 00:12:11,934 --> 00:12:16,439 in the event queue then the system pulls these events out of the event queue one at 198 00:12:16,439 --> 00:12:18,798 a time and executes the appropriate handler. 199 00:12:18,798 --> 00:12:22,660 This is why this is sometimes called the Hollywood Model of programming. 200 00:12:22,660 --> 00:12:24,734 Don't call us, we'll call you. Right? 201 00:12:24,734 --> 00:12:29,003 You don't get to decide when your functions get called and sent, instead, 202 00:12:29,003 --> 00:12:33,449 the system calls your handler functions whenever the events actually occur. 203 00:12:33,449 --> 00:12:38,310 Just like a Hollywood actor who goes to an audition is told not to call them back, 204 00:12:38,310 --> 00:12:40,799 right? If they decide to choose him, or her, 205 00:12:40,799 --> 00:12:45,660 they'll give them a call at that point. So you don't have any guarantee what order 206 00:12:45,660 --> 00:12:50,403 your handler functions will run, you just know that if and when the events occur 207 00:12:50,403 --> 00:12:55,264 your handler functions will in fact run. And this is how graphical user interfaces 208 00:12:55,264 --> 00:12:56,450 are built, okay? When. 209 00:12:56,450 --> 00:13:00,032 You pull down a menu and select an item or you push a button. 210 00:13:00,032 --> 00:13:02,617 These are events that invoke event handlers, 211 00:13:02,617 --> 00:13:05,260 Okay? And this is also how we are going to write 212 00:13:05,260 --> 00:13:09,548 the interesting interactive games that we are going to write throughout the 213 00:13:09,548 --> 00:13:12,367 semester. Now, I hope you have come to enjoy this 214 00:13:12,367 --> 00:13:13,660 model as much as I do.