-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakeGame.java
More file actions
237 lines (204 loc) · 7.02 KB
/
SnakeGame.java
File metadata and controls
237 lines (204 loc) · 7.02 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
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
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.*;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.awt.event.ActionListener;
public class SnakeGame extends JFrame implements KeyListener, ActionListener {
static ArrayList<Integer> arrayx = new ArrayList<Integer>(); // to store x point of the snake
static ArrayList<Integer> arrayy = new ArrayList<Integer>();// to store y point of the snake
Timer timer;
int up = 38;
int down = 40;
int left = 37;
int right = 39;
int direction = right;
int appleY = 0;
int appleX = 0;
int h = 0;
int prev_g = 1000000000;
public SnakeGame() {
newFood();
addKeyListener(this);
timer = new Timer(30, this);
timer.start();
}
public static void main(String args[]) {
for (int i = 0; i < 200; i++) {
arrayx.add(i);
arrayy.add(100);
}
SnakeGame jf = new SnakeGame();
jf.setTitle("Snake game");
jf.setVisible(true);
jf.setSize(500, 500);
}
@Override
public void paint(Graphics g) {
int headX = arrayx.get(arrayx.size() - 1);
int headY = arrayy.get(arrayy.size() - 1);
int g_moving_h = Math.abs(headX - appleX) + Math.abs(headY - appleY);
g.setColor(Color.BLACK);
g.fillRect(0, 0, 500, 500);
for (int i = 0; i < arrayx.size(); i++) {
g.setColor(Color.BLUE);
g.fillRect(arrayx.get(i), arrayy.get(i), 1, 1);
}
g.setColor(Color.RED);
g.fillRect(appleX, appleY, 10, 10);
if (g_moving_h > prev_g) {
if (direction == left || direction == right) {
int upG = calG(up);
int downG = calG(down);
if (upG < downG) {
direction = up;
} else {
direction = down;
}
} else if (direction == up || direction == down) {
int leftG = calG(left);
int rightG = calG(right);
if (leftG < rightG) {
direction = left;
} else {
direction = right;
}
}
}
prev_g = g_moving_h;
}
public Boolean isSnakeBit() {
goAccordingToDirect(direction);
int headX = arrayx.get(arrayx.size() - 1);
int headY = arrayy.get(arrayy.size() - 1);
{
Boolean yDouble = arrayy.indexOf(headY) != (arrayy.size() - 1);
Boolean xDouble = headX == arrayx.get(arrayy.indexOf(headY));
if (yDouble && xDouble) {
// Undo
arrayx.remove(arrayx.size() - 1);
arrayy.remove(arrayy.size() - 1);
return true;
}
}
{
Boolean xDouble = arrayx.indexOf(headX) != (arrayx.size() - 1);
Boolean yDouble = headY == arrayy.get(arrayx.indexOf(headX));
if (yDouble && xDouble) {
// Undo
arrayx.remove(arrayx.size() - 1);
arrayy.remove(arrayy.size() - 1);
return true;
}
}
// Undo
arrayx.remove(arrayx.size() - 1);
arrayy.remove(arrayy.size() - 1);
return false;
}
public void goAccordingToDirect(int _direction) {
if (_direction == right) {
int lastEle = arrayx.get(arrayx.size() - 1);
int newPos = lastEle + 1;
arrayx.add(newPos);
lastEle = arrayy.get(arrayy.size() - 1);
arrayy.add(lastEle);
}
if (_direction == down) {
int lastEle = arrayx.get(arrayx.size() - 1);
arrayx.add(lastEle);
lastEle = arrayy.get(arrayy.size() - 1);
int newPos = lastEle + 1;
arrayy.add(newPos);
}
if (_direction == left) {
int head = arrayx.get(arrayx.size() - 1);
int newPos = head - 1;
arrayx.add(newPos);
head = arrayy.get(arrayy.size() - 1);
arrayy.add(head);
}
if (_direction == up) {
int head = arrayx.get(arrayx.size() - 1);
arrayx.add(head);
head = arrayy.get(arrayy.size() - 1);
int newPos = head - 1;
arrayy.add(newPos);
}
}
public int calG(int _direction) {
goAccordingToDirect(_direction);
int headX = arrayx.get(arrayx.size() - 1);
int headY = arrayy.get(arrayy.size() - 1);
int g_moving_h = Math.abs(headX - appleX) + Math.abs(headY - appleY);
// Undo
arrayx.remove(arrayx.size() - 1);
arrayy.remove(arrayy.size() - 1);
return g_moving_h;
}
public void keyTyped(KeyEvent k) {
}
@Override
public void keyPressed(KeyEvent keyEvent) {
direction = keyEvent.getKeyCode();
}
public void keyReleased(KeyEvent k) {
}
public void newFood() {
appleY = (int) (Math.random() * 90 + 5) * 5;
appleX = (int) (Math.random() * 90 + 5) * 5;
int headX = arrayx.get(arrayx.size() - 1);
int headY = arrayy.get(arrayy.size() - 1);
h = Math.abs(headX - appleX) + Math.abs(headY - appleY);
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (isSnakeBit()) {
if (direction == left || direction == right) {
direction = down;
} else if (direction == up || direction == down) {
direction = left;
}
}
if (isSnakeBit()) {
if (direction == left || direction == right) {
direction = up;
} else if (direction == up || direction == down) {
direction = right;
}
}
int headX = arrayx.get(arrayx.size() - 1);
int headY = arrayy.get(arrayy.size() - 1);
if (((appleY + 10) >= headY && appleY <= headY) && ((appleX + 10) >= headX && appleX <= headX)) {
System.out.println("food eaten");
newFood();
} else {
arrayx.remove(0);
arrayy.remove(0);
}
goAccordingToDirect(direction);
headX = arrayx.get(arrayx.size() - 1);
headY = arrayy.get(arrayy.size() - 1);
{
Boolean yDouble = arrayy.indexOf(headY) != (arrayy.size() - 1);
Boolean xDouble = headX == arrayx.get(arrayy.indexOf(headY));
if (yDouble && xDouble) {
System.out.println(direction);
System.out.println("GAME OVER");
System.exit(0);
}
}
{
Boolean xDouble = arrayx.indexOf(headX) != (arrayx.size() - 1);
Boolean yDouble = headY == arrayy.get(arrayx.indexOf(headX));
if (yDouble && xDouble) {
System.out.println(direction);
System.out.println("GAME OVER");
System.exit(0);
}
}
repaint();
}
}