-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.java
297 lines (240 loc) · 7.45 KB
/
Board.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//Making ths a Swing component so that it can be added in Snake class Constructor
public class Board extends JPanel implements ActionListener {
// globalizing the images so that it can be used for other methods as well
private Image apple;
private Image dot;
private Image head;
// Area of the Frame
private final int ALL_DOTS = 2500;
private final int DOT_SIZE =10;
// Random generator
private final int Random_position = 29;
// apple coordinate variable
private int apple_x;
private int apple_y;
private final int x[] = new int[ALL_DOTS];
private final int y[]= new int[ALL_DOTS];
private boolean leftDirection =false;
private boolean rightDirection = true;
private boolean upDirection =false;
private boolean downDirection = false;
// for game reference
private boolean inGame = true;
private boolean gameState = false;
private int dots;
private Timer timer;
private int score = 0;
Board(){
// creating object and adding event of the adapter
addKeyListener(new Adapter());
setBackground(Color.BLACK);
setFocusable(true);
// image needs to be loaded before initializing the game
loadImage();
// initializing the game
initGame();
}
public void loadImage(){
ImageIcon img1 = new ImageIcon(ClassLoader.getSystemResource("icons/apple.png"));
apple = img1.getImage();
ImageIcon img2 = new ImageIcon(ClassLoader.getSystemResource("icons/dot.png"));
dot = img2.getImage();
ImageIcon img3 = new ImageIcon(ClassLoader.getSystemResource("icons/head.png"));
head = img3.getImage();
}
public void initGame(){
dots =3;
for(int i=0; i<dots;i++){
// starting vertical height
y[i]=50;
// starting horizontal height
x[i]=50 -i*DOT_SIZE;
}
// calling the locateApple method;
locateApple();
// for increasing the snake
timer = new Timer(140, this);
timer.start();
}
public void locateApple(){
int r =(int)(Math.random()*Random_position);
apple_x =r * DOT_SIZE;
r =(int)(Math.random()*Random_position);
apple_y =r*DOT_SIZE;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(!gameState){
gameStart(g);
}
else if(inGame){
draw(g);
scoreBoard(g);
}
else{
gameOver(g);
}
}
// image Draw garna lai
public void draw(Graphics g){
if(inGame){
g.drawImage(apple, apple_x, apple_y, this);
for(int i =0 ; i<dots;i++){
if(i==0){
// if zero head should appear first
g.drawImage(head, x[i],y[i],this);
}
// else dots should connect
else {
g.drawImage(dot, x[i], y[i], this);
}
}
Toolkit.getDefaultToolkit().sync();
}
else{
gameOver(g);
}
}
public void gameStart(Graphics g){
String msg = "PLEASE PRESS SPACE TO START";
Font font = new Font("SANS_SERIF", Font.BOLD, 25);
FontMetrics metrics = getFontMetrics(font);
g.setFont(font);
g.setColor(Color.GREEN);
int x = (500 - metrics.stringWidth(msg)) / 2;
int y = 500 / 2;
g.drawString(msg, x, y);
}
// gameOVer
public void gameOver(Graphics g){
String msg = "GAME OVER";
Font font = new Font("SANS_SERIF", Font.BOLD, 35);
FontMetrics metrics = getFontMetrics(font);
g.setFont(font);
g.setColor(Color.RED);
int x = (500 - metrics.stringWidth(msg)) / 2;
int y = 500 / 2;
g.drawString(msg, x, y);
String scoremsg = "Score: " + score;
Font scoreFont = new Font("SANS_SERIF", Font.BOLD,35);
FontMetrics scoremetrics = getFontMetrics(scoreFont);
g.setFont(scoreFont);
g.setColor(Color.GREEN);
int scorex= (500 - scoremetrics.stringWidth(scoremsg))/2;
int scorey = y +metrics.getHeight() +20;
g.drawString(scoremsg, scorex,scorey);
}
public void move(){
for(int i =dots; i>0;i--){
// moving the body part (ek step agadi leuxa)
x[i] =x[i-1];
y[i] = y[i-1];
}
// checking the movement for the left side
if(leftDirection){
x[0]= x[0] -DOT_SIZE;
}
if(rightDirection){
x[0]= x[0]+DOT_SIZE;
}
if(upDirection){
y[0] = y[0] -DOT_SIZE;
}
if(downDirection){
y[0]= y[0]+ DOT_SIZE;
}
// to check if it is moving the head part
// x[0] += DOT_SIZE;
// y[0] += DOT_SIZE;
}
// to check if the head got the apple or not
public void checkapple(){
if((x[0]== apple_x) &&(y[0] ==apple_y)){
dots++;
score++;
locateApple();
}
}
// Scoreboard for the game
public void scoreBoard(Graphics g){
if(inGame) {
String msg = "SCORE: " + score;
Font font = new Font("SANS_SERIF", Font.BOLD, 35);
FontMetrics metrics = getFontMetrics(font);
g.setFont(font);
g.setColor(Color.GREEN);
int x = 500 - metrics.stringWidth(msg) - 10;
int y = 30;
g.drawString(msg, x, y);
}
}
// to check for collision
public void checkCollision(){
// checking if head is touches the body
for(int i =dots; i>0; i--){
// x[0] means head and x[i] means rest of the body
if((i>4) && (x[0]==x[i]) && (y[0] == y[i])){
inGame = false;
}
}
// if it touches the boudnary
if(y[0]>=500){
inGame =false;
}
if(x[0]>=500){
inGame =false;
}
if(x[0]<0){
inGame =false;
}
if(y[0]<0){
inGame =false;
}
// if inGame false xa vane snake move hunu vayena
if(!inGame){
timer.stop();
}
}
public void actionPerformed(ActionEvent e){
if(inGame) {
checkapple();
checkCollision();
move();
}
repaint();
}
// making a inner class
public class Adapter extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
// this is to check if the left arrow key is pressed or not
if(key == KeyEvent.VK_LEFT && (!rightDirection)){
leftDirection =true;
upDirection =false;
downDirection =false;
}
if(key ==KeyEvent.VK_RIGHT &&(!leftDirection)){
rightDirection =true;
upDirection =false;
downDirection =false;
}
if(key ==KeyEvent.VK_UP &&(!downDirection )){
rightDirection =false;
upDirection =true;
leftDirection =false;
}
if(key ==KeyEvent.VK_DOWN &&(!upDirection)){
rightDirection =false;
leftDirection =false;
downDirection =true;
}
if(key ==KeyEvent.VK_SPACE){
gameState = true;
}
}
}
}