-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
61 lines (48 loc) · 1.64 KB
/
Copy pathGame.java
File metadata and controls
61 lines (48 loc) · 1.64 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Game implements Runnable {
public final static JTextField nick = new JTextField("Nickname", 6);
public static String getNick() {
if ((nick.getText() != null) && (nick.getText() != "")) {
String nickFormatted = nick.getText() + " ";
return nickFormatted.substring(0, 10);
}
return "Nickname ";
}
@Override
public void run() {
final JFrame frame = new JFrame("SuperSnake");
frame.setLocation(300, 200);
final JLabel score = new JLabel();
final GameBoard board = new GameBoard(score);
board.setPreferredSize(board.getPreferredSize());
frame.add(board, BorderLayout.NORTH);
final JPanel controlPanel = new JPanel();
frame.add(controlPanel, BorderLayout.SOUTH);
final JButton start = new JButton("Start");
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!board.playing) {
board.snake.clear();
board.start();
start.setText("Reset");
} else {
board.playing = false;
start.setText("Start");
}
}
});
controlPanel.add(start, BorderLayout.CENTER);
controlPanel.add(nick, BorderLayout.WEST);
controlPanel.add(score, BorderLayout.SOUTH);
controlPanel.setPreferredSize(new Dimension(board.getWidth(), 40));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
}
public static void main(String [] args) {
SwingUtilities.invokeLater(new Game());
}
}