Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. /*Luke Burgess 08016497
  2. Assessment
  3. Version 2.0 05/12/2016 */
  4.  
  5. int randomNo; //Variable used to generate random number.
  6. int guess; //Variable to convert string to int data type.
  7. int dataStore;
  8. int chances = 20; //This is the lives/chances the player has to begin with.
  9. String inputText = ""; //This is the variable for the data entered by the player.
  10. String message = ""; //Variable to display hints/result.
  11. String messageEnd = ""; //Variable to display how many attempts the game took.
  12.  
  13.  
  14. void setup(){
  15. size(1000, 500); //Sets the size of the window.
  16. smooth();
  17.  
  18. randomNo = int(random(1,100)); //generates a random number.
  19.  
  20. }
  21.  
  22.  
  23.  
  24. void draw(){
  25. background(0); // Makes background colour black.
  26.  
  27. textAlign(CENTER);
  28. textSize(40);
  29. fill(70, 175, 10); //Dark green colour for text.
  30. text("Guess a number between 1-100", 500, 50); //Sets up instructions for game.
  31. textSize(30);
  32. text("Type a number and then press the ENTER key", 500, 200); //Further instructions
  33.  
  34. textSize(15);
  35. text("Chances remaining: " + chances, 800, 400); //Displays chances remaining in bottom right corner of window.
  36.  
  37. fill(255); //White colour fill to make hints/comments stand out.
  38. textSize(50);
  39. text(inputText, 500, 400);
  40. text(message, 500, 125); //Makes hints/results draw into program.
  41. text(messageEnd, 300, 450); //Makes total attempts needed draw on screen.
  42.  
  43. //text(randomNo, 200, 400); //Shows randomNo generated - used for testing purposes and commented out when not required.
  44.  
  45. guess = int(inputText); //Converts string data type into int to compare to random number generated.
  46. dataStore = int(inputText);
  47.  
  48.  
  49. if(chances <= 0) { //If statement asking declaring what to do once the number of chances runs out.
  50. endScreen(); //Runs function "endScreen" to end the game once chances have run out.
  51. }
  52.  
  53.  
  54.  
  55.  
  56.  
  57. }
  58.  
  59.  
  60. void keyPressed(){
  61.  
  62. if (keyCode == BACKSPACE) { //Deletes last character if there is one.
  63. if (inputText.length() > 0) {
  64. inputText = inputText.substring(0, inputText.length()-1); //reduces length by 1 if backspace pressed and length is greater than 0.
  65. }
  66. }
  67. else if (keyCode == DELETE) { //Deletes all characters typed.
  68. inputText = "";
  69. }
  70. else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT) { //Allows use of modifier keys.
  71. inputText = inputText + key;
  72. }
  73.  
  74. if(keyCode == ENTER) { //Defines actions on pressing ENTER.
  75. chances = chances - 1;
  76. keyCode = ' ';
  77. inputText = "";
  78.  
  79. if (guess == randomNo) { //Defines actions if the number entered is equal to the random number generated.
  80. message = "Well done, you guessed the number!";
  81. messageEnd = "Attempts needed: " + (20 - chances);
  82. }
  83. else if(guess > randomNo) { //Defines actions if the number entered is greater than the random number generated.
  84. message = "Too high, try again!";
  85. }
  86. else if(guess < randomNo) { //Defines actions if the number entered is less than the random number generated.
  87. message = "Too low, try again!";
  88. }
  89.  
  90.  
  91.  
  92. }
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement