-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
121 lines (96 loc) · 3.12 KB
/
Copy pathMain.java
File metadata and controls
121 lines (96 loc) · 3.12 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
// imports
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
* Jan 16, 2022 <br>
* This class creates a window to take in the input for the number of players in the
* game. Then it creates the game and plays it.
*/
class Main
{
//Class variable used to check the number of player playing to pass into game constructor
public static int userSelection = -1;
//Creates menu and game frames
public static void main(String[] args)
{
// Create menu and x position and width
JFrame menu = new JFrame();
int xpos = 200;
int xwid = 200;
// Text explaining number of players
JLabel standardText = new JLabel("How many players do you want to participate in this game?");
standardText.setBounds(150,20,380,30);
//
JButton player2 = new JButton("2 player");
player2.setBounds(xpos,140,xwid,30);
JButton player3 = new JButton("3 player");
player3.setBounds(xpos,190,xwid,30);
JButton player4 = new JButton("4 player");
player4.setBounds(xpos,240,xwid,30);
JButton exit = new JButton("Exit the game");
exit.setBounds(xpos,290,xwid,30);
menu.add(standardText);
menu.add(player2);
menu.add(player3);
menu.add(player4);
menu.add(exit); // this button does not show, however, when you swap it with player 2 for ex. then it appears
menu.setVisible(true);
////////////////////////
// Creating the frame //
////////////////////////
menu.setSize(600,400);
menu.setLocationRelativeTo(null);
menu.setLayout(null);
menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//////////////////////////
// All Action Listeners //
//////////////////////////
player2.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
userSelection = 2;
}
});
player3.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
userSelection = 3;
}
});
player4.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
userSelection = 4;
}
});
exit.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0)
{
menu.dispose();
}
});
// Check if the player has launched the game
while(true)
{
System.out.print("");
if (userSelection >= 2 && userSelection <= 4)
{
// Close the menu window
menu.dispose();
// Make the game object and play
Game game = new Game(userSelection);
game.play();
break;
}
}
}
}