1 00:00:00,427 --> 00:00:01,318 Hey, welcome back. 2 00:00:01,318 --> 00:00:03,090 How much did you get done on your own? 3 00:00:03,090 --> 00:00:04,330 I'm ready to finish this game with you. 4 00:00:04,330 --> 00:00:07,530 So let's get back into workspaces and see about setting up our game loop and 5 00:00:07,530 --> 00:00:08,250 making things work. 6 00:00:09,400 --> 00:00:12,247 So first, before we actually start on the game, I think 7 00:00:12,247 --> 00:00:16,079 I actually want to bring in the clear screen function that we used earlier. 8 00:00:16,079 --> 00:00:19,768 So I need to import os and 9 00:00:19,768 --> 00:00:26,040 then I need to make our def clear_screen 10 00:00:26,040 --> 00:00:31,943 function which will do os.system cls 11 00:00:31,943 --> 00:00:37,860 if os.name is equal to nt else clear. 12 00:00:39,370 --> 00:00:42,320 So cool, now we have a handy way of clearing the screen. 13 00:00:42,320 --> 00:00:45,190 Second, let's handle getting the random locations. 14 00:00:45,190 --> 00:00:49,282 Now if you just looked through the random docs, just kind of skimmed them over. 15 00:00:49,282 --> 00:00:51,290 You might have seen the choice function. 16 00:00:51,290 --> 00:00:53,320 No this is a great one to get a random choice and 17 00:00:53,320 --> 00:00:57,060 we could totally use it, but there's one that's a little bit better. 18 00:00:57,060 --> 00:01:00,360 We want to make sure that our random selections don't overlap. 19 00:01:00,360 --> 00:01:02,540 The sample function does exactly that, 20 00:01:02,540 --> 00:01:05,510 we don't want to select two cells out of the same. 21 00:01:05,510 --> 00:01:09,530 Sample makes sure that we don't do that by giving us a certain number of random 22 00:01:09,530 --> 00:01:12,190 unique elements from whatever iterable we give it. 23 00:01:12,190 --> 00:01:15,640 Well we have an iterable, cells, and want three of them. 24 00:01:15,640 --> 00:01:20,360 So we can actually take out all of this and 25 00:01:20,360 --> 00:01:24,850 just return random.sample cells three. 26 00:01:24,850 --> 00:01:26,690 So that'll return three cells. 27 00:01:26,690 --> 00:01:29,560 So that's great.That returns a list of three tuples. 28 00:01:29,560 --> 00:01:32,950 And we'll use unpacking on the other end to assign each one of those items 29 00:01:32,950 --> 00:01:34,960 into monster dorm player variables. 30 00:01:34,960 --> 00:01:39,330 Now did you guess that tuple unpacking works on lists too? 31 00:01:39,330 --> 00:01:41,240 Because it totally does. 32 00:01:41,240 --> 00:01:44,820 So we should get those locations at the beginning of the game. 33 00:01:44,820 --> 00:01:47,750 So let's do it here outside 34 00:01:47,750 --> 00:01:51,560 of the while loop because we don't want to reget locations each time. 35 00:01:52,670 --> 00:01:54,940 So you can do these in whatever order you want. 36 00:01:54,940 --> 00:01:56,800 I'm going to do them as monster door player. 37 00:01:58,320 --> 00:02:04,070 But if you want to do player, door monster, you can totally do that. 38 00:02:05,580 --> 00:02:10,300 So, we need to put a quote mark right there. 39 00:02:11,660 --> 00:02:13,295 So I forgot a quote here at the end of clear. 40 00:02:13,295 --> 00:02:16,445 Okay, everything looks good now. 41 00:02:16,445 --> 00:02:17,990 Okay so 42 00:02:17,990 --> 00:02:22,710 cool, now that we have the player position we can figure out the available moves. 43 00:02:22,710 --> 00:02:25,450 So let's look here at get_moves. 44 00:02:25,450 --> 00:02:27,310 This is a closest function to us. 45 00:02:27,310 --> 00:02:29,500 We can go ahead and solve this one. 46 00:02:29,500 --> 00:02:32,780 So first I'm gonna the player into x and y's. 47 00:02:32,780 --> 00:02:38,326 So we can do x,y = player we can unpack the player tuple into x and 48 00:02:38,326 --> 00:02:40,498 y coordinates, right? 49 00:02:40,498 --> 00:02:46,651 So then, let's say, if x = zero, then we'd said right here, right? 50 00:02:46,651 --> 00:02:48,900 If x is 0, then they can't move left. 51 00:02:48,900 --> 00:02:52,229 So we want to do moves.remove left. 52 00:02:53,801 --> 00:02:55,022 Okay? 53 00:02:55,022 --> 00:03:01,890 If x = = 4, then we want to do moves.remove ("RIGHT"). 54 00:03:01,890 --> 00:03:05,520 So we can take out these two things right there, because we've done those now. 55 00:03:06,850 --> 00:03:07,720 Close that parenthesis. 56 00:03:08,890 --> 00:03:16,071 If y = zero, then moves.remove UP, right? 57 00:03:16,071 --> 00:03:19,290 Because if the players y is zero, then they can't move up. 58 00:03:19,290 --> 00:03:23,898 And lastly, if y = 4 moves.remove down 59 00:03:23,898 --> 00:03:28,910 because they can't move down at that point. 60 00:03:30,320 --> 00:03:35,770 Okay, so that's cool, so we've been able to find out all of the stuff. 61 00:03:35,770 --> 00:03:37,190 Now let's try this real quick, 62 00:03:37,190 --> 00:03:40,320 because we're at the point now where we can actually try some things. 63 00:03:40,320 --> 00:03:43,700 We just need to update these prints that are down here. 64 00:03:43,700 --> 00:03:47,022 So here we need to do .format(player), and 65 00:03:47,022 --> 00:03:52,230 now that we've filled that with the player position we can remove that. 66 00:03:52,230 --> 00:03:56,675 And here, we can put this with .format, 67 00:03:56,675 --> 00:04:03,040 let's do a ,.join, of get_moves(player). 68 00:04:03,040 --> 00:04:09,270 Okay, so let's save that and let's go ahead and play this. 69 00:04:09,270 --> 00:04:16,762 So, down here Python dungeon game, line 58. 70 00:04:16,762 --> 00:04:21,145 I need one more, right there, okay. 71 00:04:22,236 --> 00:04:25,010 Sometimes it's really hard to count those parentheses. 72 00:04:25,010 --> 00:04:27,906 So I can see here that was put into room one two and 73 00:04:27,906 --> 00:04:30,510 I can move left right up and down. 74 00:04:30,510 --> 00:04:33,780 So let's actually run this again, see if we can get a corner. 75 00:04:33,780 --> 00:04:38,150 Cool, so we've got 0 and 3 and I can't move left because I'm in 0. 76 00:04:38,150 --> 00:04:41,060 So that's nice, we got the things that we wanted. 77 00:04:41,060 --> 00:04:43,801 Let's make sure that QUIT works too, it does, nice. 78 00:04:46,339 --> 00:04:49,750 Okay, so those moves look right and we're good there. 79 00:04:49,750 --> 00:04:51,920 So, I think it's time to let the player move. 80 00:04:51,920 --> 00:04:58,160 So, let's move this this get moves call here, out into a variable. 81 00:04:58,160 --> 00:05:02,680 So, we can actually do this right here inside the True. 82 00:05:02,680 --> 00:05:07,160 So let's say, valid_moves = get_moves for the player. 83 00:05:08,970 --> 00:05:17,020 And then here we'll join, valid_moves, all right. 84 00:05:17,020 --> 00:05:18,540 So we have the moves and 85 00:05:18,540 --> 00:05:21,430 now when a player enters a move, we can make sure that it's valid. 86 00:05:22,560 --> 00:05:26,130 So we don't care about that. 87 00:05:26,130 --> 00:05:30,976 So here below that, if move in valid_moves, 88 00:05:30,976 --> 00:05:37,191 then player = move_player and whatever move they put in, 89 00:05:37,191 --> 00:05:42,411 else, let's print out a little bit of a message, 90 00:05:42,411 --> 00:05:48,290 and we'll say, walls are hard, don't run into them. 91 00:05:49,390 --> 00:05:50,150 Which really, 92 00:05:50,150 --> 00:05:54,370 I would assume, people know already, but it's always good to tell them. 93 00:05:54,370 --> 00:05:58,310 And then we'll just do a continue there, because we wanna just loop on around. 94 00:05:58,310 --> 00:06:01,160 So, if the moves are in the moves variable, we'll move the player. 95 00:06:01,160 --> 00:06:03,060 Otherwise, we'll print out a message. 96 00:06:03,060 --> 00:06:07,770 And let's take the clear screen here at the beginning of the loop too, so 97 00:06:07,770 --> 00:06:12,530 we'll do clear screen, right there. 98 00:06:12,530 --> 00:06:16,220 It doesn't really matter exactly where we do it, but we'll do it right there. 99 00:06:16,220 --> 00:06:19,702 Okay, so now we need to create this move_player function, 100 00:06:19,702 --> 00:06:23,409 cuz we've called it, we need to make it actually do something. 101 00:06:23,409 --> 00:06:29,045 And once again I think I want to split the player out into x and y. 102 00:06:29,045 --> 00:06:32,845 Because that's, that's really handy just being able to look at x and y. 103 00:06:33,935 --> 00:06:38,075 So if move is equal to left, 104 00:06:38,075 --> 00:06:42,045 then I want x to minus equals one, right. 105 00:06:43,175 --> 00:06:48,815 And if move == "RIGHT", then I want x += 1. 106 00:06:48,815 --> 00:06:50,025 I want to increment the x. 107 00:06:51,410 --> 00:07:00,150 If move is equal to up then I want y to minus equals one I decrement that by one. 108 00:07:00,150 --> 00:07:05,290 Otherwise if move is equal to down, I want y equal to plus equal one. 109 00:07:06,770 --> 00:07:10,270 And then at the end, no matter what it is, I want to return x and y. 110 00:07:12,090 --> 00:07:15,060 So let's, I don't wanna return player, I wanna return x and y. 111 00:07:15,060 --> 00:07:18,390 I wanna return whatever these new values are, right? 112 00:07:18,390 --> 00:07:22,230 So we're changing x and y and the end we're gonna return x and 113 00:07:22,230 --> 00:07:26,190 y which is the same tuple as it was when it came in, well with the new values. 114 00:07:26,190 --> 00:07:27,430 So everything should be fine. 115 00:07:27,430 --> 00:07:34,300 So let's try this game out in this state, dungeon game. 116 00:07:34,300 --> 00:07:36,100 Okay, I'm at 4,3. 117 00:07:36,100 --> 00:07:38,795 So if I go left, I should end up at 3,3. 118 00:07:40,420 --> 00:07:44,603 Cool, I'm on 3,3 and if I go down I should end up at 3,4. 119 00:07:44,603 --> 00:07:49,330 And I can't move down anymore because the 4 is down. 120 00:07:49,330 --> 00:07:50,870 So that's cool, that's great. 121 00:07:50,870 --> 00:07:53,219 What if I do down, again? 122 00:07:53,219 --> 00:07:56,370 Well, I can't see my message about running into a wall. 123 00:07:56,370 --> 00:07:58,450 But that's okay, we can fix that later. 124 00:07:58,450 --> 00:08:01,070 But that's pretty cool, the game is working, right? 125 00:08:01,070 --> 00:08:03,210 Available moves are changing, the coordinates are changing, 126 00:08:03,210 --> 00:08:04,630 everything is working. 127 00:08:04,630 --> 00:08:07,970 All right, that was a pretty good chunk of code there for movement. 128 00:08:07,970 --> 00:08:11,250 We still need to handle when the player finds the door or the monster though. 129 00:08:11,250 --> 00:08:14,370 I think we should probably move some of the game loop into a function too. 130 00:08:14,370 --> 00:08:16,140 And of course we need to draw the map. 131 00:08:16,140 --> 00:08:18,110 Once again try to finish this out on your own. 132 00:08:18,110 --> 00:08:18,610 I believe in you.