-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
184 lines (168 loc) · 4.59 KB
/
Game.java
File metadata and controls
184 lines (168 loc) · 4.59 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package matchingGame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
public class Game extends JFrame {
private JPanel contentPane;
private int numCards = 20;
private Deck deck;
private int numCardsTurned;
private Card cardOne;
private Card cardTwo;
private boolean match;
private JPanel gamePanel;
private JPanel bottomPanel;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Game frame = new Game();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Game()
{
resetVariables();
setUpMainDisplay();
makeTitle();
makePadding();
makeBottomPanel();
makeReset();
makeGamePanel();
makeButtons();
}
private void resetVariables()
{
numCardsTurned = 0;
cardOne = null;
cardTwo = null;
match = false;
deck = new Deck(numCards);
}
private void setUpMainDisplay()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1100, 1000);
contentPane = new JPanel();
contentPane.setBackground(Color.BLACK);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
private void makeTitle()
{
JLabel Title = new JLabel("Matching Game");
Title.setBorder(new EmptyBorder(20, 0, 20, 0));
Title.setForeground(Color.WHITE);
Title.setFont(new Font("Bauhaus 93", Font.PLAIN, 60));
Title.setHorizontalAlignment(SwingConstants.CENTER);
contentPane.add(Title, BorderLayout.NORTH);
}
private void makePadding()
{
//left
JPanel leftPadding = new JPanel();
leftPadding.setBackground(Color.BLACK);
leftPadding.setBorder(new EmptyBorder(0, 20, 0, 0));
contentPane.add(leftPadding, BorderLayout.WEST);
//right
JPanel rightPadding = new JPanel();
rightPadding.setBorder(new EmptyBorder(0, 0, 0, 20));
rightPadding.setBackground(Color.BLACK);
contentPane.add(rightPadding, BorderLayout.EAST);
}
private void makeBottomPanel()
{
bottomPanel = new JPanel();
bottomPanel.setBorder(new EmptyBorder(20, 0, 20, 0));
bottomPanel.setBackground(Color.BLACK);
contentPane.add(bottomPanel, BorderLayout.SOUTH);
}
private void makeReset()
{
JButton btnRestart = new JButton("Restart");
btnRestart.setBorder(new EmptyBorder(10, 10, 10, 10));
btnRestart.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 20));
btnRestart.setBackground(Color.WHITE);
bottomPanel.add(btnRestart);
btnRestart.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
resetVariables();
gamePanel.removeAll();
gamePanel.revalidate();
gamePanel.repaint();
makeButtons();
}
});
}
private void makeGamePanel()
{
gamePanel = new JPanel();
gamePanel.setBackground(Color.BLACK);
contentPane.add(gamePanel, BorderLayout.CENTER);
gamePanel.setLayout(new GridLayout(0, 5, 0, 0));
}
private void makeButtons()
{
for(int i = 0; i < numCards; i++)
{
Card currentCard = deck.getCard(i);
//make a new button
JButton button = new JButton("");
currentCard.setButton(button);
button.setIcon(new ImageIcon(GameOldIdea.class.getResource("/matchingGame/img/pipes.png")));
gamePanel.add(button);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
//card is not already turned over
if (!currentCard.equals(cardOne) && !currentCard.equals(cardTwo))
{
button.setIcon(new ImageIcon(GameOldIdea.class.getResource("/matchingGame/img/" + currentCard.getImg())));
numCardsTurned++;
if (numCardsTurned == 3)
{
//turn cards back over
if (!match)
{
cardOne.getButton().setIcon(new ImageIcon(GameOldIdea.class.getResource("/matchingGame/img/pipes.png")));
cardTwo.getButton().setIcon(new ImageIcon(GameOldIdea.class.getResource("/matchingGame/img/pipes.png")));
}
numCardsTurned = 1;
match = false;
cardTwo = null;
}
if (numCardsTurned == 1)
{
cardOne = currentCard;
}
if (numCardsTurned == 2)
{
cardTwo = currentCard;
if (cardOne.getId() == cardTwo.getId())
match = true;
}
}
}
});
}
}
}