-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake.java
145 lines (123 loc) · 4.19 KB
/
Snake.java
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
//Jeffrey Phung 10-18-2015
/*
* This class calls upon Board.java to run the Game.
*/
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;
public class Snake{
private JFrame snake;
private JPanel homeScreen;
private JButton easy;
private JButton medium;
private JButton hard;
private JButton play;
private int difficulty;
public void setUpHomeScreen(boolean restart){
difficulty = 200 ; // default medium speed
snake = new JFrame();
snake.setLayout(new BorderLayout());
if(restart){
snake.dispose();
snake = new JFrame();
snake.setLayout(new BorderLayout());
}
//home screen panel
homeScreen = new JPanel(new BorderLayout());
homeScreen.setPreferredSize(new Dimension(320, 240));
JLabel bg = new JLabel();
bg.setPreferredSize(new Dimension(320, 240));
ImageIcon icon = new ImageIcon("HomeBG.gif");
icon.getImage().flush();
bg.setIcon(icon);
homeScreen.add(bg);
easy = new JButton("Easy");
medium = new JButton("Medium");
hard = new JButton("Hard");
play = new JButton("Play");
play.addMouseListener(new MouseListener(){
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
snake.remove(play);
snake.remove(easy);
snake.remove(medium);
snake.remove(hard);
snake.getContentPane().remove(homeScreen);
//game panel
Board board = new Board(difficulty);
snake.add(board);
board.requestFocusInWindow();
snake.getContentPane().validate();
snake.getContentPane().repaint();
}
});
easy.addMouseListener(new MouseListener(){
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
difficulty = 300;
easy.setBackground(Color.WHITE);
medium.setBackground(Color.BLACK);
hard.setBackground(Color.BLACK);
}
});
medium.addMouseListener(new MouseListener(){
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e){
difficulty = 200;
medium.setBackground(Color.WHITE);
easy.setBackground(Color.BLACK);
hard.setBackground(Color.BLACK);
}
});
hard.addMouseListener(new MouseListener(){
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e){
difficulty = 100;
hard.setBackground(Color.WHITE);
easy.setBackground(Color.BLACK);
medium.setBackground(Color.BLACK);
}
});
easy.setForeground(Color.RED);
easy.setBackground(Color.BLACK);
easy.setFocusPainted(false);
medium.setForeground(Color.RED);
medium.setBackground(Color.BLACK);
medium.setFocusPainted(false);
hard.setForeground(Color.RED);
hard.setBackground(Color.BLACK);
hard.setFocusPainted(false);
JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
buttonsPanel.setBackground(Color.BLACK);
buttonsPanel.add(easy);
buttonsPanel.add(medium);
buttonsPanel.add(hard);
buttonsPanel.add(play);
buttonsPanel.repaint();
homeScreen.add(buttonsPanel, BorderLayout.SOUTH);
snake.add(homeScreen, BorderLayout.CENTER);
snake.setTitle("Snake Game");
snake.setResizable(false);
snake.pack();
snake.setVisible(true);
snake.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main (String [] args){
Snake snake = new Snake();
snake.setUpHomeScreen(false);
}
} //end snake class