 Okay, welcome to rock paper scissors gooey version using TK inter by Tokyo Ed Tech That is me quick shout out to my channel members Kevin and Paul tonight I'm gonna show you or today is wherever it is wherever you're at I'm gonna show you how to make a quick rock paper scissors gooey version So let's take a look at what I'm planning to do now this program I'm sharing with you is actually done by one of my students and I thought she did a really great job with this So I want to show you what she did and I'm gonna walk you through the steps to recreate that So I'm gonna go ahead and run that and you can see that this is a gooey version of rock paper scissors And you see here it says junk in that's because here in Japan we call rock paper scissors junk in Or don't know why this is their word for it. So I'm gonna go ahead and click rock So you can see here the computer chose rock. I chose rock and the result up here is a tie So I'm gonna show you how to do this. So a couple things the way gooey's work We've got some different gooey elements here. We've got one two three four buttons We've got a label and we've got a second label and we actually have a third label here So what I'm gonna do is I'm gonna go ahead and recreate that as closely as I can and you can see There's a bunch of code here and we'll walk through how to do that. So you can see These are the various You know labels Buttons etc etc. Now if you don't know how to do a gooey I'm assuming you know a little bit about TK inter for this particular tutorial I have a couple other tutorials that actually walk through it a little bit more step by step But let's give it a shot here. Okay, so first thing I need to do for my gooey is to create The actual window. So I'm gonna be using the TK inter Module and I'm gonna make a root window equals TK inter dot Shit, I don't remember the code. So I'm gonna go cheat over here. It's TK dot TK. That's right TK That actually creates your your root window And then at the very bottom the very last line of my code should be root. It's root dot main loop Okay, so I'm gonna go ahead and save that and run it and just test it and see what happens So you can see here. I've got my little window now Okay, so if you remember we had a title and that title Was junk in for the Japanese version. We'll call it rock paper scissors And put by Tokyo Ed Tech a little shout out to myself there So again, if you're not sure I strongly recommend just testing things and making sure that they work You know as you're going along and if you recall She already measured it out. So thank you to my student. I'm not using your name because she's Not she's under, you know, 18 so root dot geometry and I think she had it at 400 x 500 And we'll go ahead and stick with that and for some weird reason. This has to be in quotation marks It's it's not weird reason, but that's just the way it is. So let's go ahead and test that and there we go. So This is our basic root window. So you can see how easy that is to do in TK inter So what I tell my students is to design their GUI first So what we did was we designed this on paper and then we create our widgets So if you recall we had a widget for the result a widget for the player's choice a widget for the Computer choice and some buttons for doing things. So I'm just gonna go over to here and Take a look at that. So we got some labels Okay, so I like yeah, I thought she did a great job here with naming everything. I was really really happy to see this Again, you don't have to be this, you know precise I guess with your naming, but it just makes life super duper easy, especially for beginners So I'm gonna just get started with that. So label Result equals TK inter dot result And it's not a result that is a label. Sorry so TK inter label and it's gonna go into my root window and When I start it my text is going to be You know choose something like that and If I run this now again, if you haven't seen my other GUI tutorials, you might not be familiar with this So if I run this now You don't see anything. Okay, so you also have to In this case, we're gonna use pack. It's one of the built-in geometry managers There's like there's a few different ones, but pack is the simplest one and Label is not defined. So this is label underscore result not label dot result Not bad. And now you can see the label is there and the text choose is also there so Next we had the button for rock button for paper and a button for scissors. So I'm gonna say so button rock equals TK inter dot button It's also going in the root window its text is going to be rock and For buttons, we need to connect buttons to a function So in this case, I want to connect it to the function called rock and Same thing button rock dot pack Now if I run this I'm gonna get an error. Let's take a look at that Okay, so you can see down here Something beginners really got to get used to doing is reading these error messages because they tell you basically what you need to Know for the most part and you can see here line 16. So this line here It says button rock it tells you the actual code that you typed in it says name error rock is not defined So what's telling me is it doesn't know what this rock is and of course it doesn't know because we haven't created the function yet So what I would do at this point is type create functions And I'm gonna go ahead and create my function. So rock. I've got rock and Since I don't know what codes in there yet I'm just gonna type pass passes a placeholder and we'll delete that later And I'm also gonna have paper and I'm gonna have scissors. I think there was a reset function. So resets and pass So let's go ahead and do that Okay, so now let's run it and see if we get an error. Okay, great. So rock Okay, he doesn't do anything yet because the function only passes but let's go ahead and just do the rest of our gooey first So we're a button for rock paper and scissors So I usually do this a lot you'll see I get something working and then just copy and paste if I'm just making a few changes Yeah, this is something I do recommend to beginners, especially and Is to be careful with your name so button scissors calls has a label scissors and It calls a function called scissors So it's just really really Consistent and kind of easy to understand. Let's go ahead and test that make sure everything looks like it should okay So rock paper scissors and Then we also have the labels So I think the way it was done. We had label computer choice We'll start that out blank with nothing And again, we got a packet and I'm gonna go ahead and copy that and make a label User choice So this is where we'll display the rock paper scissors and then finally we needed a reset button So I'm gonna go ahead and make that real quick as well reset text and command Again, I can't emphasize enough just consistency and naming It's just really gonna make your life super duper easy So let's go ahead and test that one last time. So basically I've got my GUI setup Yes, I've got my result label here. I got my buttons here I've got my reset button here and notice there's nothing here because we haven't put anything into it It's just blank text No, so the next part is Really to deal with the logic of the game So let's take a look real quick at the the outcome matrix for this particular game So you can see here We've got three choices For the player three choices for the computer rock paper scissors in both cases So if the player chooses rock your chooses rock We got a tie so you can see our R is a tie PP is a tie and SS is a tie Now if the player chooses paper and the computer chooses rock The player is the winner because paper covers rock However, if the player chooses scissors and the computer chooses rock then the computer wins So you everybody's I think almost everybody in the world is familiar with this game. So This kind of shows how it works. So we need to code that into our game So a couple things we got to think about is how do we get the computer to make a choice? The user is making a choice Based on which button he or she presses but the computer choice needs to be random so What I can do here is I can use the random Module I'm gonna do is I'm gonna say you know random number equals random That ran in and we've got three choices so one two or three and Then what I'm gonna do is I'm gonna convert that so if random Number equals one computer choice equals rock L if random number equals two then computer choice equals paper and I Could do an else here, but let's do an L if just in case There's something wrong with our code and we don't recognize to and say computes computer choice equals scissors all right, so The computers made a choice at the start of the game notice This is out here in the global space and normally we don't really recommend using global variables But we'll just go ahead and do it this way this time Just because it's probably a little bit easier for beginners It's something as you as you learn a little bit more you learn ways to get around that but for now Let's just keep it simple. So Let's just go ahead and deal with the rock So again back to this outcome matrix So the computer so the player has chosen rock so the computer can choose rock paper or scissors So if it's rock and rock we have a tie if it's rock and paper the computer wins And if it's rock and scissors the player wins So because I'm inside the rock function. I know that I can Assume I've chosen rock. So what I can do here is I can say You know deal with the you know choices So if just like my student had she did a really good job here if player choice actually I'm not gonna say that because we know the player choice here is rock So I'm gonna say so if the computer choice Equals rock right now, I'm just gonna say print you know tie L if Computer choice equals Paper say prints so paper beats rocks a print computer wins And then otherwise our only remaining choice is that the player wins I'm gonna code it straight out just in case computer choice equals scissors Print user wins or player wins will say okay, so let's go ahead and just test this out Okay, so when I hit a hit rock And you can see down here it says player wins now We don't actually know what the the computer choice was but we're assuming That in this case the player won then the computer choice was Scissors okay, so what we need to do is we need to display that information in the GUI okay, so We've got three labels. So we got label result Label computer choice and label user choice. Let's just go ahead and do that then So if it's rock, we know the following so we know label user choice and I'm gonna do this method text equals Say I'm gonna put rock for now and I'm gonna say label while we don't know rock so in the case of Tie so we know that the label computer choice And this is how you change the text in a label equals rock and what I can do there now is copy this and And the same thing here Texts scissors and instead of printing I want to say label results Text Equals and here this is gonna be The computer wins and this one's gonna be player wins. Okay, so let's go ahead and test it. Okay, so I'm gonna go ahead and hit rock Okay, scissors rock player wins Okay, now Nothing changes here. So the reset doesn't work Let's go ahead and implement reset real quick So reset so when we reset the game the only thing we really well a couple things we need to do is we need to do this again So I'm gonna go ahead and just copy and paste that here and indent that all But the thing you have to remember is that these are global variables again We're it's not particularly a good programming practice to use but for now. We'll stick with those. So we've got Random number and computer choice Now I don't really need to worry about random number at this point But I do need to worry about the computer choice. So I'm gonna say global computer choice and the reason is That computer choice is accessed outside this function So I need it to be able to access this global computer choice variable and That's why I use the global keyword Okay, this random number. I don't only need to use it in this section. So I don't have to worry about that Okay, now I could just basically I could simplify this take this out and make just do a function call But I'll just leave this for now because it's a little bit easier to understand and I think it's yeah, it works So let's just leave that there for now again This is a you know tutorial for relative beginners, you know my student whose code I showed you earlier She did such a great job Yeah, she's only been coding for like three and a half months And so I was really happy that she was able to convert this program To work with a gooey it was her choice to do this. This was kind of an open-ended project. So great job for her Let's see. Okay, so let's go ahead and test that So I'm gonna hit rock. Okay, so scissors and rock player wins. I'm gonna hit reset Click this again Okay, so you see paper computer wins reset Okay, scissors player wins. So it looks like it's working Okay, now we got a tie. So we've seen all three different examples So the next step is to kind of make it look a little bit better. Well, actually, maybe the other thing is with reset We might want to reset the labels. So label Computer choice You know text Equals, you know blank label user choice Text blank and I guess result as well. So label Result label not label label result Text Or maybe just I think I had it as choose initially something like that No parentheses. Let's test that real quick. So rock player wins is rock reset choose rock Okay, I think I'm pretty happy with that now. I want to get in there those really cool ASCII Designs that my student was using so You can see here We have a rock shape. We have a Paper shape and we have a scissors shape so One kind of way I'm going to make this code just a little bit more efficient is I'm going to take these images and Put them into a variable So what I'm gonna do is I'm gonna copy this Again, this will be linked to my my github. I'll put a link down below so I'm gonna say You know ASCII art So I'm gonna say rock image equals and Notice I'm using three quotes. So I'm gonna use paper image Equals and I'm gonna do scissors image And so again, I'm just gonna copy this from my students And but you know, you'll be able to copy it from my github So let's see here three And the scissors image is going to get that oops I do paper. Let's make sure you screw that up. That's scissors. That's scissors. That's not paper do Copy that. Okay, so All right, I think it's good. So what I'll do here is if the computer choice equals rock I'm gonna go ahead and put rock image And then here I'll put You know paper image Here I'll put scissors image And then up here I will put rock image So that way I don't have to keep having these images, you know all the way through the code Okay, so we hit rock Okay, so there's mine and there is the Computer now you can see how it's a little bit messed up. Okay, and that was that was to be expected Okay, so I hit reset and test it again Okay, so far so good and the reason that this is messed up There's actually there's two reasons is that the label is automatically centered and So that's kind of throwing things off and the other thing is that the font is Proportional font so the width of the characters is different Depending on the character and we don't want a proportional font. We want a non-proportional font so What I'm gonna do is I'm gonna go down to my labels And I'm gonna take a look at the code my student has just to make sure I don't mess this up so In the label, you know widget created creation We need to choose the font And this you should have courier on your computer Hopefully if not, you may have to choose a different font that is mono spaced And we justify it left and So you say justify equals tk enter left note. This is in capital letters And the font is courier. So now if I save that and run it And I hit rock Now you can see how it looks just like it is supposed to so the computer wins because the computer chose paper I'm gonna hit reset hit rock Player wins because rock beats scissors and reset And rock I'm gonna tie because we have two rocks here. It's pretty cool. Um, it's working just exactly as expected and Yeah, that's that's pretty cool. The only thing I might change on this program is I might put the result down below. I'm gonna put that Down here before the reset button But after the labels, I think that for me that works a little bit better So I'm gonna go ahead and run that and test it So I'm gonna hit rock Okay, so we got scissors versus rock player wins. I kind of like that just that arrangement a little better Um, yeah, I think we can go with that So now basically all I need to do Is to do the same thing But for paper and scissors Okay, so you can see how compact this code is And just kind of makes it a little bit easier to code it. So I know this is working as it should So I'm gonna copy that and paste it into here And so in this case the user is choosing paper So if it's paper, it's a tie If the computer So it should be a paper image Because that's what the tie so paper and paper Um, what else right here? So the computer choice equals Rock So paper beats rock So actually we'll make the scissors since the computer wins And that's going to be scissors image And down here this should be rock Because rock No way paper. Yeah rock. So player So the computer has rock Player has paper player wins And the computer choice is rock image Okay, so You got p here paper image here. You got s here scissors image here You've got r here rock image here. So let's go ahead and test that and play around with that. Just make sure it's working Okay, so we hit paper Okay, so we got scissors And paper computer wins. Let's reset paper um Computer wins again. So we're going to reset make sure it's resetting Okay rock play player wins because the player chose paper I'm gonna hit reset. I'm gonna hit rock and just make sure that's still working computer wins Because the computer chose paper Okay, so I think it's working pretty well and then we got to do the same thing for the Scissors choice by the player Okay, so in this case it's going to be scissors oops and so if the computer choice is scissors, we've got a tie And this should be the scissors image and if the computer choice equals What does what beats scissors should be rock? Because rock beats scissors And we would use rock image here And then if the computer choice is paper player wins and paper image Okay, so let's go ahead and test that last one So I'm gonna hit scissors So scissors and scissors tie so reset Scissors so scissors and rock rock beats scissors computer wins reset Okay, scissors beats paper player wins And then again, it's good to always go back and just test to make sure everything else is working as you expect it And we've got a lot of ties here Tie I'm hitting reset Time hit reset Okay, so I found that with decanter and not decanter, but with the random module I do find you get a lot in a row of the same. It's it's always disconcerting Um, so I'm gonna hit reset there and test out rock Can you get a tie? Okay, and rock paper beats rock computer wins So yeah, that is that is it that is the gooey version Of rock paper scissors and again a lot of thanks Many many thanks to my students who again did such a great job with this And I wanted to share that because I I love to share my student's success and uh Yeah, so again, she did such a really good job converting our text based version of this program to use a gooey again after only three and a half months of class so, you know give her a round of applause And uh, you know, again, I'll put this code A link down below. So if you haven't subscribed, please do if you're thinking about it Please become a channel member and support the channel more directly and uh, yeah, keep on coding. Take care. Have a good one