sorry for my bad english, I want to ask how to make games with the principles of rock, paper, scissors with javascript? or anyone has a link to a tutorial??
please help … thanks
sorry for my bad english, I want to ask how to make games with the principles of rock, paper, scissors with javascript? or anyone has a link to a tutorial??
please help … thanks
This is a basic coding problem. Your best bet is to read through some basic Unity tutorials (google for it) and then post back here with specific questions. A good place to start would be with the docs for the Random class.
edit: way better post below
Lets look at the basics.
3 buttons… Rock, paper and scissors
Your game starts off saying, The computer is ready, choose:
var msg="Make your choice!";
So you first need code that picks the computers response. We can do this by enumerating Rock… 0, Paper… 1 and Scissors… 2
Now, you need some code for the buttons
function OnGUI() {
var human=-1;
if (GUI.Button(Rect(10,10,50,30),"Rock"))
human=0;
if (GUI.Button(Rect(10,70,50,30),"Paper"))
human=1;
if (GUI.Button(Rect(10,130,50,30),"Scissors"))
human=2;
if(human>-1){
var computer : int=Random.value * 3;
}
}
OK, now some game logic.
rock > scissors
scissors > paper
paper > rock
var results=0;
// did we win?
if(human == 0 computer == 2) results=1;
if(human == 1 computer == 0) results=1;
if(human == 2 computer == 1) results=1;
// did we lose?
if(human == 0 computer == 1) results=-1;
if(human == 1 computer == 2) results=-1;
if(human == 2 computer == 0) results=-1;
Now, we know if we won, lost or tied… the results will be 1 if we won, 0 if we tied and -1 if we lost.
Now we simply change our message to include our results. We will also format the enumeration to make it easier on us.
var pick : String[]=new String[3];
pick[0]="Rock";
pick[1]="Paper";
pick[2]="Scissors";
msg="You picked: " + pick[human];
msg+="\nI picked: " + pick[computer];
if(results==-1)msg+="\nI win!";
if(results== 0)msg+="\nWe tied";
if(results== 1)msg+="\nYou won!";
msg=="\nPlay Again?";
Lastly, we want to display the message.
GUI.Label (Rect (40, 50, 130, 100), msg);
OK… the whole game now works on a single script that displays gui information in the window. Because the GUI is redrawn each frame we need to call it each frame. This is done in the OnGUI function. The only actual piece of information that is not generated in this function is the display message, since we want to keep it from frame to frame, until it changes.
So here is all the code combined.
var msg="Make your choice!";
function OnGUI() {
// check to see if the human has pressed a button
var human=-1;
// draw and check each button
if (GUI.Button(Rect(10,10,50,30),"Rock"))
human=0;
if (GUI.Button(Rect(10,70,50,30),"Paper"))
human=1;
if (GUI.Button(Rect(10,130,50,30),"Scissors"))
human=2;
// if the human has pressed something, do the game logic.
if(human>-1){
// choose a value for the computer
var computer : int=Random.value * 3;
// set the results to equal to start
var results=0;
// did we win?
if(human == 0 computer == 2) results=1;
if(human == 1 computer == 0) results=1;
if(human == 2 computer == 1) results=1;
// did we lose?
if(human == 0 computer == 1) results=-1;
if(human == 1 computer == 2) results=-1;
if(human == 2 computer == 0) results=-1;
// enumerate the picks.
var pick : String[]=new String[3];
pick[0]="Rock";
pick[1]="Paper";
pick[2]="Scissors";
// create the text we need for the new message
msg="You picked: " + pick[human];
msg+="\nI picked: " + pick[computer];
if(results==-1)msg+="\nI win!";
if(results== 0)msg+="\nWe tied";
if(results== 1)msg+="\nYou won!";
msg=="\nPlay Again?";
}
// display the label containing the message
GUI.Label (Rect (40, 50, 130, 100), msg);
}
Thank you for asking for a very simple example. I believe that at some point this will help other new people in creating simple stuff to learn unity.
Haha thats what I call a decent post
There is not a real Unity based tutorial on this stuff. We all look at it as… Oh its just a simple thing… anyone should be able to do it. i wanted an approach where it was very simple, and it explained how Unity looked at certain things on a base level. It really is a reach to go from press a button to do something and understanding that Unity will run this every frame.
Very helpful, thanks BigMisterB!
thanks for the help and guidance … I will try and learn more
I must say BigMisterB bravo that is an excelent way to explain game logic with a practical and simple code example. This is a feature that is missing in many tutorials. You often see a breakdown of the code segments with a description of how they work and what they are doing but rarely see an explanation of why the code is doing it, or where the progamer came to the solution provided. I think you should make a model for a tutorial based on this formula that covers the basics of logic within the code.
I personally found that when learning, I understood much of what was being done in the code that i came across. But very little of why it was this way, which slowed my learning by the fact that i had to work that out based on what the code did and my understanding of the various parts, almost reverse engineering the code. well done.
for BigMasterB, thank you very much for your help … really helpful and useful …
Thank you BIgMasterB, this is very helpful!
^ Should really use enums for good coding practice.
A true enum would have been better, but its a bit more coding to resolve and I wanted to keep it simple.
I am trying to learn unity, and I know some basics but this project is beyond what I can do at the moment. I actually tried to build a unity project with this but didn’t get it working, would it be possible to post an example Unity project. Thank you very much.
Ha ha never mind I got it working
Thx again
If I would do this with say a UI, how will this work?
This thread is TEN YEARS OLD!
Please start your own post. It’s free.
How to report your problem productively in the Unity3D forums:
How to understand compiler and other errors and even fix them yourself:
If you post a code snippet, ALWAYS USE CODE TAGS:
How to use code tags: https://discussions.unity.com/t/481379
Also, please consider this:
https://www.youtube.com/watch?v=1kUlD7OWpLM