forked from vvulevv/Snake-Java-Console-Game
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreen.java
More file actions
231 lines (185 loc) · 4.4 KB
/
Screen.java
File metadata and controls
231 lines (185 loc) · 4.4 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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JPanel;
public class Screen extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static final int GAME_FIELD_ROLS = 30;
public static final int GAME_FIELD_COLS = 30;
private Thread thread;
@SuppressWarnings("unused")
private boolean running = false;
private BodyPart b;
private ArrayList<BodyPart> snake;
private Apple apple;
private ArrayList<Apple> apples;
private Random r;
private int xCoor = 0;
private int yCoor = HEIGHT / 40;
private int size = 5;
private int snakeSpeed = 30000;
private int snakeFinalSpeed = 6000;
private int speedSstep = 500;
boolean right = true;
boolean left = false;
boolean up = false;
boolean down = false;
private int ticks = 0;
private Key key;
public Screen() {
setFocusable(true);
key = new Key();
addKeyListener(key);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
r = new Random();
snake = new ArrayList<BodyPart>();
apples = new ArrayList<Apple>();
start();
}
public void tick() {
if (snake.size() == 0) {
b = new BodyPart(xCoor, yCoor, 20);
snake.add(b);
}
if (apples.size() == 0) {
int xCoor = r.nextInt(GAME_FIELD_ROLS);
int yCoor = r.nextInt(GAME_FIELD_COLS);
apple = new Apple(xCoor, yCoor, 20);
apples.add(apple);
}
for (int i = 0; i < apples.size(); i++) {
if ((xCoor == apples.get(i).getX())
&& (yCoor == apples.get(i).getY())) {
size++;
apples.remove(i);
i--;
if (snakeSpeed != snakeFinalSpeed) {
snakeSpeed -= speedSstep;
}
}
}
for (int i = 0; i < snake.size(); i++) {
if (xCoor == snake.get(i).getX() && yCoor == snake.get(i).getY()) {
if (i != snake.size() - 1) {
stop();
}
}
}
if (xCoor < 0 || xCoor > GAME_FIELD_ROLS - 1 || yCoor < 0
|| yCoor > GAME_FIELD_COLS - 1) {
stop();
}
ticks++;
if (ticks > snakeSpeed) {
if (right) {
xCoor++;
}
if (left) {
xCoor--;
}
if (up) {
yCoor--;
}
if (down) {
yCoor++;
}
ticks = 0;
b = new BodyPart(xCoor, yCoor, 20);
snake.add(b);
if (snake.size() > size) {
snake.remove(0);
}
}
}
public void paint(Graphics g) {
g.clearRect(0, 0, WIDTH, HEIGHT);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.DARK_GRAY);
for (int i = 0; i < WIDTH / 20; i++) {
g.drawLine(i * 20, 0, i * 20, HEIGHT);
}
for (int i = 0; i < HEIGHT / 20; i++) {
g.drawLine(0, i * 20, WIDTH, i * 20);
}
g.setColor(Color.BLACK);
g.fillRect(WIDTH - 199, 0, WIDTH, HEIGHT);
for (int i = 0; i < snake.size(); i++) {
snake.get(i).draw(g);
}
for (int i = 0; i < apples.size(); i++) {
apples.get(i).draw(g);
}
}
public void start() {
running = true;
thread = new Thread(this);
thread.start();
}
public void stop() {
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run() {
while (true) {
tick();
repaint();
}
}
private class Key implements KeyListener {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_RIGHT && !left) {
up = false;
down = false;
right = true;
}
if (key == KeyEvent.VK_LEFT && !right) {
up = false;
down = false;
left = true;
}
if (key == KeyEvent.VK_UP && !down) {
up = true;
right = false;
left = false;
}
if (key == KeyEvent.VK_DOWN && !up) {
down = true;
right = false;
left = false;
}
if (key == KeyEvent.VK_P) {
boolean pressOnce = true;
if (pressOnce) {
while (true) {
int keyPause = e.getKeyCode();
if (keyPause != KeyEvent.VK_P) {
pressOnce = false;
System.out.println(keyPause);
break;
}
}
} else {
}
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
}