Created
February 5, 2014 05:06
-
-
Save radiatoryang/8817704 to your computer and use it in GitHub Desktop.
This is a class assignment for my Unity classes about code. It's full of errors and typos.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
// THIS CODE IS FULL OF ERRORS! DEBUG IT! | |
// 0) first, copy and paste this into a file in a Unity project | |
// 1) read the comments and variable names to figure out the coder's intention | |
// 2) go to the "Console" tab in Unity, look for stop sign icons -- these are code compile errors. | |
// the error will tell you the filename, then the line number and column, e.g. "GuessingGame.cs (8,100)" | |
// 3) double-click on an error in the Console to go to it in MonoDevelop | |
// 4) look at the line it specified, look around it, look at the lines before it -- it's just a hint | |
// 5) guess what the computer science-y words mean! | |
// 6) you may need to do some scene setup / configuration to fix this! | |
public class GuessingGameAmazing : MonoBehaviour { | |
int guess = 0; // this is the number the player is guessing | |
int secretNumber = Random.Range( 0, 21 ); // this is the number we have to guess | |
string instructions = "PRESS SPACEBAR TO GUESS\nPRESS ARROW KEYS TO EDIT GUESS:\n"; | |
// Use this for initialization | |
void Start () { | |
// at the beginning of the game, generate a random number from 0 to 20 | |
secretNumber = Random.Range< 0, 21 >; | |
} | |
} | |
// Update is called once per frame | |
void Update () { | |
// TODO: use GetComponent for TextMesh? | |
// push left arrow to decrease guess | |
if ( Input.GetKeyDown( KeyCode.Left ) ) { | |
guess = guess + 1; | |
TextMesh.text = instructions + guess.ToString(); // change Text Mesh text | |
} | |
// push right arrow to increase guess | |
if ( Input.GetKeyUp( KeyCode.Right ) ) { | |
guess -= 1 | |
TextMesh.text = instructions + guess.ToString(); // change Text Mesh text | |
} | |
// if player presses spacebar, then evaluate the guess | |
if ( Input.GetKeyDown( spacebar ) ) { | |
if ( guess < secretNumber ) { | |
TextMesh.text = "your guess was too high"; // change Text Mesh text | |
} else if ( guess > secretNumber ) { | |
TextMesh.text = "your guess was too low"; // change Text Mesh text | |
} else { | |
TextMesh.text = "YOU WIN, YOU ARE THE BEST"; // change Text Mesh text | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment