-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game
229 lines (164 loc) · 4.96 KB
/
Game
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package Com.Game;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game extends JPanel implements KeyListener, MouseListener{
private Board game = new Board();
private ScoreBoardFrame scoreBoardFrame = new ScoreBoardFrame();
private static Game newGame = new Game();
private Color c;
public Sound sound = new Sound();
public Sound music = new Sound();
private int count = 0;
static JFrame frame = new JFrame("2048");
private Display display = new Display();
public static void setUpGUI() { // untuk set up size yang tepat dan menambahkan keylistener beserta mouselistener
frame = new JFrame("2048");
frame.addKeyListener(newGame);
frame.addMouseListener(newGame);
frame.getContentPane().add(newGame);
frame.setSize(1280, 700);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g) { // untuk pewarnaan game.
super.paint(g);
Font fnt0 = new Font("Comic Sans", Font.CENTER_BASELINE, 50);
g.setFont(fnt0);
g.setColor(new Color(197, 155, 48));
g.drawString("2048", 600, 70);
Font fnt1 = new Font("Comic Sans", Font.CENTER_BASELINE, 15);
g.setFont(fnt1);
g.setColor(new Color(197, 155, 48));
g.drawString("Score: " + game.getScore(), 400 - 4 * String.valueOf( game.getScore() ).length(), 70);
g.drawString("Highest Tile: " + game.getHighTile(), 880 - 4 * String.valueOf(game.getHighTile()).length(), 70);
g.drawString("Use 'wasd' or Arrow Keys to move", 530, 600);
g.setColor(new Color (255, 226, 173));
g.fillRect(430, 100, 450, 450);
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
drawTiles(g, game.board[i][j], j * 100 + 460, i * 100 + 130);
}
}
if(game.gameOver()) {
count = 0;
stopMusic();
g.setColor(new Color (114, 9, 183));
g.fillRect(430, 100, 450, 450);
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
g.setColor(Color.RED);
g.fillRoundRect(j * 100 + 460, i * 100 + 130, 95, 95, 5, 5);
g.setColor(Color.BLACK);
g.drawString("GAME", j * 100 + 490, i * 100 + 170);
g.drawString("OVER", j * 100 + 490, i * 100 + 190);
}
}
}
display.render(g);
}
void drawTiles(Graphics g, Tile tile, int x, int y) { // untuk pewarnaan tile
int tileValue = tile.getValue();
int length = String.valueOf(tileValue).length();
g.setColor(new Color (247, 240, 222));
g.fillRoundRect(x, y, 95, 95, 5, 5); // besar kotak 95
g.setColor(Color.BLACK);
if(tileValue > 0) {
g.setColor(tile.getColor());
g.fillRoundRect(x, y, 95, 95, 5, 5);
g.setColor(Color.BLACK);
g.drawString("" + tileValue, x + 48 - 3 * length, y + 49);
}
}
@Override
public void keyPressed(KeyEvent e) { // mendeteksi arah gerak dari keyboard
String gameBoard;
if(e.getKeyChar() == 'w' || e.getKeyCode() == KeyEvent.VK_UP) {
game.up();
game.spawn();
gameBoard = game.toString();
frame.repaint();
}
else if(e.getKeyChar() == 's' || e.getKeyCode() == KeyEvent.VK_DOWN) {
game.down();
game.spawn();
gameBoard = game.toString();
frame.repaint();
}
else if(e.getKeyChar() == 'a' || e.getKeyCode() == KeyEvent.VK_LEFT) {
game.left();
game.spawn();
gameBoard = game.toString();
frame.repaint();
}
else if(e.getKeyChar() == 'd' || e.getKeyCode() == KeyEvent.VK_RIGHT) {
game.right();
game.spawn();
gameBoard = game.toString();
frame.repaint();
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) { // untuk mendeteksi saat button di pencet maka terjadi new game
int mx = e.getX();
int my = e.getY();
// public Rectangle newGame = new Rectangle(900, 100, 120, 50);
if(mx >= 900 && mx <= 1020) {
if(my >= 100 && my <= 180) {
count++;
game = new Board();
game.spawn();
game.spawn();
frame.repaint();
Board.value(0);
if(count == 1) playMusic(1);
}
// public Rectangle scoreBoard= new Rectangle(900, 200, 120, 50);
else if(my >= 200 && my <= 280) {
ScoreBoardFrame sbf = new ScoreBoardFrame();
sbf.setUp();
}
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void playMusic(int i) {
sound.setFile(i);
sound.play();
sound.loop();
}
public void stopMusic() {
sound.stop();
}
public void playSE(int i) {
// TODO Auto-generated method stub
music.setFile(i);
music.play();
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}