1 00:00:00,000 --> 00:00:10,810 2 00:00:10,810 --> 00:00:11,330 Allison:Hey guys! 3 00:00:11,330 --> 00:00:13,360 I was just testing out the game I made. 4 00:00:13,360 --> 00:00:15,530 It's super easy to make simple games like this. 5 00:00:15,530 --> 00:00:18,170 And, in fact, I'll show you how I made it right now. 6 00:00:18,170 --> 00:00:24,415 Let's head on over to www.scratch.mit.edu. 7 00:00:24,415 --> 00:00:27,630 We want to create a new project, so lets click on the Create tab. 8 00:00:27,630 --> 00:00:30,540 I've already got a product opened, so we're going to switch over here. 9 00:00:30,540 --> 00:00:32,810 >> Let's start with a quick tour of the scratch environment. 10 00:00:32,810 --> 00:00:35,340 On the bottom left here, we have all the sprites that will 11 00:00:35,340 --> 00:00:36,520 be used in our game. 12 00:00:36,520 --> 00:00:40,100 For example, we have a monkey, banana, and broccoli. 13 00:00:40,100 --> 00:00:43,120 When we click on each sprite you'll notice that the right column will 14 00:00:43,120 --> 00:00:46,340 change, to show us that we're working with that specific sprite. 15 00:00:46,340 --> 00:00:49,480 Anything we put in this column will control the sprite that's highlighted. 16 00:00:49,480 --> 00:00:53,030 Here in the middle, we have our column containing our pallet a puzzle pieces, 17 00:00:53,030 --> 00:00:54,690 which we'll use to create our game. 18 00:00:54,690 --> 00:00:58,830 At the top, we have the category of puzzle pieces we have to work with. 19 00:00:58,830 --> 00:01:02,290 For example, the blue pieces, under the Motion tab, will allow us to 20 00:01:02,290 --> 00:01:04,069 control how our sprites move. 21 00:01:04,069 --> 00:01:07,370 We'll explore the other categories as we create our game. 22 00:01:07,370 --> 00:01:10,260 Finally, on the top left, we have our stage, where we'll 23 00:01:10,260 --> 00:01:11,310 actually play our game. 24 00:01:11,310 --> 00:01:14,210 Where we can see the effects of our scripts on the sprites. 25 00:01:14,210 --> 00:01:15,450 >> So let's get started. 26 00:01:15,450 --> 00:01:18,270 In our game, the monkey will try to eat the banana while 27 00:01:18,270 --> 00:01:19,670 avoiding the broccoli. 28 00:01:19,670 --> 00:01:22,580 The monkey will follow our mouse, and the broccoli and banana will float 29 00:01:22,580 --> 00:01:24,430 around the stage randomly. 30 00:01:24,430 --> 00:01:27,350 When the monkey gets close to the banana, the banana will actively try 31 00:01:27,350 --> 00:01:31,380 to avoid the monkey, just to make things a bit more exciting. 32 00:01:31,380 --> 00:01:34,760 >> Let's start by scripting the behavior of our main character, the monkey. 33 00:01:34,760 --> 00:01:37,850 To start our script, we'll use the When Green Flag clicked piece from the 34 00:01:37,850 --> 00:01:39,020 events category. 35 00:01:39,020 --> 00:01:40,590 Here. 36 00:01:40,590 --> 00:01:43,870 This will cause all puzzle pieces connected to the button to run when we 37 00:01:43,870 --> 00:01:47,420 click the Green Flag, here, at the top right of the stage. 38 00:01:47,420 --> 00:01:49,090 We want the monkey to follow the mouse. 39 00:01:49,090 --> 00:01:53,613 So we choose the Go To button and select Mouse Pointer, like so. 40 00:01:53,613 --> 00:01:55,730 So we're going to try it here. 41 00:01:55,730 --> 00:01:56,980 Let's test is and see what happens. 42 00:01:56,980 --> 00:01:59,390 43 00:01:59,390 --> 00:02:02,916 >> So it seems like the monkey really only followed the first mouse click. 44 00:02:02,916 --> 00:02:07,210 This is because, by default, the Go To block only executes once. 45 00:02:07,210 --> 00:02:10,259 To get the monkey to follow the mouse throughout the entire game, we need 46 00:02:10,259 --> 00:02:13,570 this block to execute over and over again. 47 00:02:13,570 --> 00:02:16,420 Sounds like we need some kind of looping construct. 48 00:02:16,420 --> 00:02:18,160 So lets move into Control. 49 00:02:18,160 --> 00:02:19,880 Look, let's try this Forever loop. 50 00:02:19,880 --> 00:02:24,470 51 00:02:24,470 --> 00:02:25,920 Seems like it works. 52 00:02:25,920 --> 00:02:28,300 >> So, next we should think about how the monkey will 53 00:02:28,300 --> 00:02:30,220 interact with other sprites. 54 00:02:30,220 --> 00:02:35,040 Basically, we need to add logic to our game, such that, the game will end if 55 00:02:35,040 --> 00:02:37,580 the monkey touches the banana or the broccoli. 56 00:02:37,580 --> 00:02:40,050 And actually use the word "if" when describing the behavior 57 00:02:40,050 --> 00:02:41,225 we're trying to create. 58 00:02:41,225 --> 00:02:44,770 And conveniently enough, there's a control blocked called If that we use 59 00:02:44,770 --> 00:02:46,630 for exactly this purpose. 60 00:02:46,630 --> 00:02:50,700 So the diamond shape, right here, on the block, is where we put the 61 00:02:50,700 --> 00:02:52,320 condition that we want to check. 62 00:02:52,320 --> 00:02:54,830 In this case, we'll check whether the monkey is touching the banana. 63 00:02:54,830 --> 00:02:57,260 >> Se we grab the Touching piece from the Sensing category. 64 00:02:57,260 --> 00:03:01,970 65 00:03:01,970 --> 00:03:05,590 And choose Banana from the drop down menu. 66 00:03:05,590 --> 00:03:08,110 Now we have to tell our sprite what to do when it touches the banana. 67 00:03:08,110 --> 00:03:11,440 We want the game to end, and the monkey to say, you've won. 68 00:03:11,440 --> 00:03:13,710 So we add in a Say and Stop All piece. 69 00:03:13,710 --> 00:03:18,210 70 00:03:18,210 --> 00:03:19,870 We'll change, hello, to, you won. 71 00:03:19,870 --> 00:03:28,200 And the time to 0.5. 72 00:03:28,200 --> 00:03:31,500 Now, we'll apply the same logic when the monkey is touching the broccoli, 73 00:03:31,500 --> 00:03:34,494 except we'll have the monkey say, you lost, instead of, you won. 74 00:03:34,494 --> 00:03:46,610 75 00:03:46,610 --> 00:03:48,120 >> Let's test this out and see if it works. 76 00:03:48,120 --> 00:03:52,940 77 00:03:52,940 --> 00:03:53,610 Awesome. 78 00:03:53,610 --> 00:03:56,770 We've finished scripting the monkey's behavior, but we're not done yet. 79 00:03:56,770 --> 00:03:59,720 We still have to tell the broccoli and banana what to do. 80 00:03:59,720 --> 00:04:02,060 >> Let's start with the simpler of the two, the broccoli. 81 00:04:02,060 --> 00:04:04,280 Which just needs to move randomly. 82 00:04:04,280 --> 00:04:07,150 Again, we'll begin with the When Green Flag clicked piece. 83 00:04:07,150 --> 00:04:09,990 We need the sprite to be in constant motion, so let's drag 84 00:04:09,990 --> 00:04:11,830 out the Forever loop. 85 00:04:11,830 --> 00:04:14,730 We'll create a random motion using some motion pieces with a random 86 00:04:14,730 --> 00:04:15,980 number generator. 87 00:04:15,980 --> 00:04:27,920 88 00:04:27,920 --> 00:04:30,610 >> Alright, let's test this out. 89 00:04:30,610 --> 00:04:31,470 Awesome 90 00:04:31,470 --> 00:04:33,370 >> OK, now only the banana is left. 91 00:04:33,370 --> 00:04:35,700 Let's start off with the same set of pieces as before. 92 00:04:35,700 --> 00:04:38,360 93 00:04:38,360 --> 00:04:42,050 So if the monkey gets close, the banana should avoid it. 94 00:04:42,050 --> 00:04:44,500 Else, the banana should move randomly. 95 00:04:44,500 --> 00:04:46,780 Sounds like a job for this If Else block. 96 00:04:46,780 --> 00:04:55,930 In the condition, we will check the bananas distance from the monkey, and 97 00:04:55,930 --> 00:04:57,180 then have it avoid accordingly. 98 00:04:57,180 --> 00:05:04,340 99 00:05:04,340 --> 00:05:06,730 >> Now, when this condition doesn't apply, the 100 00:05:06,730 --> 00:05:08,340 banana should move randomly. 101 00:05:08,340 --> 00:05:12,190 So let's just steal our random motion pieces from the broccoli. 102 00:05:12,190 --> 00:05:15,200 All we have to do is drag the pieces we want to the banana sprite, and 103 00:05:15,200 --> 00:05:16,450 they'll be copied over. 104 00:05:16,450 --> 00:05:20,000 105 00:05:20,000 --> 00:05:20,510 >> All done. 106 00:05:20,510 --> 00:05:21,760 Let's test it out. 107 00:05:21,760 --> 00:05:28,550 108 00:05:28,550 --> 00:05:29,140 Awesome. 109 00:05:29,140 --> 00:05:30,610 Everything seems to be working. 110 00:05:30,610 --> 00:05:31,980 Now go and make your own games. 111 00:05:31,980 --> 00:05:34,340 >> If you need some inspiration check out past projects 112 00:05:34,340 --> 00:05:36,340 from CS50 on the website. 113 00:05:36,340 --> 00:05:40,250 >> My name is Allison, and this CS50. 114 00:05:40,250 --> 00:05:42,050 >> I need to get these set up super quick. 115 00:05:42,050 --> 00:05:44,214