forked from gametutorials/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
162 lines (148 loc) · 8.74 KB
/
Main.java
File metadata and controls
162 lines (148 loc) · 8.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//***********************************************************************//
// //
// - "Talk to me like I'm a 3 year old!" Programming Lessons - //
// //
// $Author: Ben Humphrey [email protected] //
// //
// $Program: Functions //
// //
// $Description: Creates a custom function and executes it //
// //
//***********************************************************************//
// What we do in this tutorial is create a menu for the user to select options
// from. You know how on a calculator there are things like square root, cosine,
// sine, etc...? Those are called functions. Those functions allow us to do
// things with just calling one function and not having to actually do the math
// for the square root, cosine, etc. Functions in programming are the same.
// We create a function that does something, and then we can call that function
// by just 1 line of code, not actually copying and pasting the code each time
// we want to perform that function. This makes code smaller, more readable and
// less frustrating :) In this tutorial we create a menu function that draws
// the menu options. Some functions take parameters and some return values.
// For instance, square root takes a number to square, then it returns the result
// of the square root right? : (double value = sqrt(49);) The "value" is the
// return value, and the 49 is the parameter that is passed into the function.
// This is an example of using a function, however, in this tutorial we won't
// use a input parameter.
//
// Below we have the code that will display a menu and ask the user for input.
// Depending on the menu item chosen, a message will be displayed, or if the
// user chooses menu item 6, then the program quit. Check it out:
// Import the Scanner class so we can use nextInt() to read in user input
import java.util.Scanner;
// Create our class for the Main program
public class Main
{
// If we want to create a new function we can do so inside of our class' scope.
// Below we create a function called "DrawMenu()". Notice that it's the
// same way we have been creating our main() function. We need to specify
// that the function is "static" and our return type will be "void" because
// we won't be returning data, just displaying the menu to the screen.
public static void DrawMenu()
{
// Once inside our new function's scope, we can start coding what we
// want to happen when this function is called. Below we just do a
// bunch of WriteLine's to display the menu all organized and clean.
// The "\t" characters in a string stand for "tab". They basically
// create 5 space in the string instead of having to type 5 spaces
// yourself.
System.out.println("\t\t ****************Game Menu****************");
System.out.println("\t\t * *");
System.out.println("\t\t * 1) New Game *");
System.out.println("\t\t * 2) Load Game *");
System.out.println("\t\t * 3) Save Game *");
System.out.println("\t\t * 4) Inventory *");
System.out.println("\t\t * 5) Options *");
System.out.println("\t\t * 6) Quit *");
System.out.println("\t\t * *");
System.out.println("\t\t *****************************************");
} // Now that we have finished coding our function we need to close its scope.
// Start our main() function for our program
public static void main(String[] args)
{
// Here we declare a type of "boolean". A boolean is usually a flag that holds
// a value: true or false (true is anything but 0, false is 0).
boolean bStillPlaying = true;
int choice = 0;
// In this case, let's make a boolean and set it to true, meaning we
// are "bStillPlaying" the game. When Quit is chosen we will set our boolean to false,
// ending the game loop. Once again, we put the 'b' in front of "StillPlaying" to show
// that it's a boolean. This provides good readable code.
// Let's create a while loop to continue until the user Presses '6' for quit.
// The loop says to do everything in its scope until bStillPlaying == false. Remember,
// a statement doesn't happen if 0 is the result from the expression.
while (bStillPlaying)
{
// All we need to do is call our function, like so. This will draw the menu.
DrawMenu();
// Since there are no parameters to pass in, we just closed the parenthesis and
// added a semicolon. That is how you "call" a function.
// Now comes the part where we need to be able to choose from the menu.
// Just like in previous tutorials, display and prompt and convert our data to an int.
System.out.println("Choose from the menu: "); // Prompt the user
// Below we create a Scanner object and read in an integer from the user
Scanner scan = new Scanner(System.in);
choice = scan.nextInt();
// Below we create a switch statement. I chose to use this instead of if/else statements.
// For a quick review on switch statements, this is just a cleaner way to check values of a variable.
switch (choice)
{
case 1: System.out.println("You chose a New Game!"); // if (choice == 1) print a message
break; // "break" breaks out of the switch statement
case 2: System.out.println("You chose to Load a Game!"); // if (choice == 2)
break; // Print the choice and break
case 3: System.out.println("You chose to Save a Game!"); // if (choice == 3)
break; // Print the choice and break
case 4: System.out.println("You chose your Inventory!"); // if (choice == 4)
break; // Print the choice and break
case 5: System.out.println("You chose Options!"); // if (choice == 5)
break; // Print the choice and break
case 6:
// bStillPlaying is set to false. This means, when the code goes back up to the top,
// while(bStillPlaying) will NOT be true and fail.
bStillPlaying = false; // if (choice == 6)
System.out.println("Game over already?"); // Print out the game over message
// A weird thing about switch statements is that you don't need
// brackets {} for more than one line, You just need a "break".
break;
}
// It's a good idea to label what the closing bracket is coming
// from if you can't see the whole scope, like below:
} // while (bStillPlaying)
}
}
///////////////////////////////////////////////////////////////////////////////
//
// * Quick Notes *
//
// Doesn't this already feel like a game? :) You are so close to making your
// own application that does cool things. Basically, what happened in this
// program is that we just created a custom function in our class that displayed
// a simple menu, then do a while loop to probe for the user's input. Depending
// on what the user chose, we displayed an appropriate message.
//
// When creating your own custom functions it's the same way you create your
// main() function:
//
// static void MyCustomFunction()
// {
// // Do stuff in here
// }
//
// You will want to put the function in your program's class or else the program
// won't compile. Unlike C/C++, you can't just put a function out in space.
//
// Finally, in order to call your function you just need to do this:
//
// MyCustomFunction();
//
// That's all there is to it, unless the function takes input/output parameters,
// but we will go over that in a later tutorial. So go and create your own
// custom functions and make sure you understand the syntax before moving on.
//
// Remember, that this tutorial does not handle error checking, so if the user puts
// in a 'q' instead of a 6 the program will not handle it correctly. We will go
// over more on error checking later on.
//
//
// © 2000-2006 GameTutorials