- Home /
Paper, Rock, Scissor
I am attempting to recreate "Paper, Rock, Scissors" as a modest attempt to learn scripting. I appear to be adding my math function every frame. Can some one tell me what I need to change so that it adds 1 to my score every time I click on the toolbar array. I have a lot to add, but I need to make sure I am concocting my equations correctly. Thanks...
var toolbarInt = 0; var toolbarStrings : String[] = ["Paper", "Rock", "Scissors"]; var myScore = 0; var computerScore = 0; var computerChoice =1;
function OnGUI () { toolbarInt = GUI.Toolbar (Rect (25, 25, 250, 30), toolbarInt, toolbarStrings); if (toolbarInt)myScore ++; GUI.Label (Rect (25, 60, 220, 140), "The Computer has chosen " + toolbarStrings[toolbarInt]); GUI.Label (Rect (25, 120, 200, 100), "The Computer's score is " + computerScore); GUI.Label (Rect (25, 140, 200, 100), "Your score is " + myScore);
}
Answer by Bunny83 · Jan 27, 2011 at 10:12 AM
The GUI.Toolbar is just a selector. Your toolbarInt will be 0, 1 or 2
OnGUI is called at least once EACH frame. You need another button to tell your game you finished your selection. Within the if block, which is executed only once when you click on the button, you can do all your stuff.
function OnGUI () {
toolbarInt = GUI.Toolbar (Rect (25, 25, 250, 30), toolbarInt, toolbarStrings);
if (GUI.Button(Rect(XX,YY,WW,HH),"Done"))
{
if (toolbarInt)myScore ++;
// TODO: do your calculations here
}
GUI.Label (Rect (25, 60, 220, 140), "The Computer has chosen " + toolbarStrings[toolbarInt]);
GUI.Label (Rect (25, 120, 200, 100), "The Computer's score is " + computerScore);
GUI.Label (Rect (25, 140, 200, 100), "Your score is " + myScore);
}
You may also have a look at GUILayout. With it you can do all the stuff as with GUI but all Elements got positioned automatically.
Answer by Jesse Anders · Jan 27, 2011 at 10:06 AM
I see a couple of potential problems with your code.
First, in this line:
GUI.Label (Rect (25, 60, 220, 140), "The Computer has chosen " +
toolbarStrings[toolbarInt]);
Shouldn't that be toolbarStrings[computerChoice]? That is, don't you want to be displaying the computer's choice there, not the player's choice?
Also, I'm not sure what this line:
if (toolbarInt)myScore ++;
Is supposed to do. I don't use UnityScript so I'm not sure how 'if (toolbarInt)' is interpreted, but however it's interpreted, that's not going to give you a point every time you win a round. (If, for example, 0 is evaluated as false and all other integers as true, that'll simply add points continuously as long as the last selection was not 'paper'.)
What you probably want is something like this (untested):
var newtoolbarint = GUI.Toolbar(
Rect(25, 25, 250, 30),
toolbarInt,
toolbarStrings
);
if (newtoolbarint != toolbarint) {
toolbarint = newtoolbarint;
// Since the computer's choice is always 'rock', only
// paper can win. Note that if you make the computer's
// choice variable, you can then compute whether the
// player won or lost using the modulus operator and
// a little arithmetic.
if (toolbarint == 0) {
++myScore;
}
}
Note that toolbarint will need to be initialized to (e.g.) -1, and reset to that value before the beginning of each round. (I think that will work correctly, but I haven't tested it.)