-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphics.java
152 lines (130 loc) · 4.38 KB
/
Graphics.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Graphics extends JPanel implements ActionListener {
static final int WIDTH = 800;
static final int HEIGHT = 800;
static final int TICK_SIZE = 50;
static final int BOARD_SIZE = (WIDTH * HEIGHT) / (TICK_SIZE * TICK_SIZE);
final Font font = new Font("TimesRoman", Font.BOLD, 30);
int[] snakePosX = new int[BOARD_SIZE];
int[] snakePosY = new int[BOARD_SIZE];
int snakeLength;
Food food;
int foodEaten;
char direction = 'R';
boolean isMoving = false;
final Timer timer = new Timer(150, this);
public Graphics() {
this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
this.setBackground(Color.WHITE);
this.setFocusable(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (isMoving) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
if (direction != 'R') {
direction = 'L';
}
break;
case KeyEvent.VK_RIGHT:
if (direction != 'L') {
direction = 'R';
}
break;
case KeyEvent.VK_UP:
if (direction != 'D') {
direction = 'U';
}
break;
case KeyEvent.VK_DOWN:
if (direction != 'U') {
direction = 'D';
}
break;
}
} else {
start();
}
}
});
start();
}
protected void start() {
snakePosX = new int[BOARD_SIZE];
snakePosY = new int[BOARD_SIZE];
snakeLength = 5;
foodEaten = 0;
direction = 'R';
isMoving = true;
spawnFood();
timer.start();
}
@Override
protected void paintComponent(java.awt.Graphics g) {
super.paintComponent(g);
if (isMoving) {
g.setColor(Color.BLUE);
g.fillOval(food.getPosX(), food.getPosY(), TICK_SIZE, TICK_SIZE);
g.setColor(Color.DARK_GRAY);
for (int i = 0; i < snakeLength; i++) {
g.fillRect(snakePosX[i], snakePosY[i], TICK_SIZE, TICK_SIZE);
}
} else {
String scoreText = String.format("The End... Score: %d... Press any key to play again!", foodEaten);
g.setColor(Color.BLACK);
g.setFont(font);
g.drawString(scoreText, (WIDTH - getFontMetrics(g.getFont()).stringWidth(scoreText)) / 2, HEIGHT / 2);
}
}
protected void move() {
for (int i = snakeLength; i > 0; i--) {
snakePosX[i] = snakePosX[i-1];
snakePosY[i] = snakePosY[i-1];
}
switch (direction) {
case 'U' -> snakePosY[0] -= TICK_SIZE;
case 'D' -> snakePosY[0] += TICK_SIZE;
case 'L' -> snakePosX[0] -= TICK_SIZE;
case 'R' -> snakePosX[0] += TICK_SIZE;
}
}
protected void spawnFood() {
food = new Food();
}
protected void eatFood() {
if ((snakePosX[0] == food.getPosX()) && (snakePosY[0] == food.getPosY())) {
snakeLength++;
foodEaten++;
spawnFood();
}
}
protected void collisionTest() {
for (int i = snakeLength; i > 0; i--) {
if ((snakePosX[0] == snakePosX[i]) && (snakePosY[0] == snakePosY[i])) {
isMoving = false;
break;
}
}
if (snakePosX[0] < 0 || snakePosX[0] > WIDTH - TICK_SIZE || snakePosY[0] < 0 || snakePosY[0] > HEIGHT - TICK_SIZE) {
isMoving = false;
}
if (!isMoving) {
timer.stop();
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (isMoving) {
move();
collisionTest();
eatFood();
}
repaint();
}
}