-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGamePanel.java
More file actions
196 lines (181 loc) · 6.19 KB
/
GamePanel.java
File metadata and controls
196 lines (181 loc) · 6.19 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.JPanel;
public class GamePanel extends JPanel implements ActionListener {
static final int SCREEN_WIDTH = 600;
static final int SCREEN_HEIGHT = 600;
static final int UNIT_SIZE = 20;
static final int GAME_UNITS = (SCREEN_WIDTH* SCREEN_HEIGHT/UNIT_SIZE);
static final int DELAY = 100;
final int x[] = new int[GAME_UNITS];
final int y[] = new int[GAME_UNITS];
int bodyParts = 6;
int appleEaten;
int appleX;
int appleY;
char direction = 'R';
boolean running = false;
Timer timer;
Random random;
GamePanel() {
random = new Random();
this.setPreferredSize(new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT));
this.setBackground(Color.black);
// this.setFocusable(true);
// this.requestFocusInWindow(); // Request keyboard focus
// Define key bindings
InputMap inputMap = this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = this.getActionMap();
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "left");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "right");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "up");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "down");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "restart");
actionMap.put("left", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (direction != 'R') {
direction = 'L';
}
}
});
actionMap.put("right", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (direction != 'L') {
direction = 'R';
}
}
});
actionMap.put("up", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (direction != 'D') {
direction = 'U';
}
}
});
actionMap.put("down", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (direction != 'U') {
direction = 'D';
}
}
});
actionMap.put("restart", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (!running) {
restartGame();
}
}
});
startGame();
}
public void startGame(){
newApple();
running = true;
timer = new Timer(DELAY,this);
timer.start();
this.setFocusable(true);
this.requestFocusInWindow();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g){
if(running) {
// for (int i = 0; i < SCREEN_HEIGHT / UNIT_SIZE; i++) {
// g.drawLine(i * UNIT_SIZE, 0, i * UNIT_SIZE, SCREEN_HEIGHT);
// g.drawLine(0, i * UNIT_SIZE, SCREEN_WIDTH, i * UNIT_SIZE);
// }
g.setColor(new Color(random.nextInt(255)));
g.fillOval(appleX, appleY, UNIT_SIZE, UNIT_SIZE);
for (int i = 0; i < bodyParts; i++) {
if (i == 0) {
g.setColor(Color.RED);
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
} else {
g.setColor(new Color(199, 30, 30));
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
}
}
}else{
gameOver(g);
}
}
public void newApple(){
appleX = random.nextInt((int)SCREEN_WIDTH/UNIT_SIZE)*UNIT_SIZE;
appleY = random.nextInt((int)SCREEN_HEIGHT/UNIT_SIZE)*UNIT_SIZE;
}
public void move(){
for(int i = bodyParts;i > 0; i--) {
x[i] = x[i - 1];
y[i] = y[i - 1];
}
switch (direction) {
case 'U' -> y[0] = y[0] - UNIT_SIZE;
case 'D' -> y[0] = y[0] + UNIT_SIZE;
case 'L' -> x[0] = x[0] - UNIT_SIZE;
case 'R' -> x[0] = x[0] + UNIT_SIZE;
}
}
public void checkApple(){
if ((x[0] == appleX) && (y[0] == appleY)){
bodyParts++;
appleEaten++;
newApple();
}
}
public void checkCollision() {
for (int i = bodyParts; i > 0; i--) {
if ((x[0] == x[i]) && (y[0] == y[i])) {
running = false;
break;
}
}
if (x[0] < 0 || x[0] >= SCREEN_WIDTH || y[0] < 0 || y[0] >= SCREEN_HEIGHT) {
running = false;
}
if (!running) {
timer.stop();
}
}
public void gameOver(Graphics g){
//Score
g.setColor(Color.white);
g.setFont(new Font("Ink Free",Font.BOLD,35));
g.setColor(Color.white);
FontMetrics metrics = getFontMetrics(g.getFont());
g.drawString("Score: "+ appleEaten, (SCREEN_WIDTH - metrics.stringWidth("Score: "+ appleEaten))/2, g.getFont().getSize());
g.setFont(new Font("Ink Free",Font.BOLD,70));
FontMetrics metrics2 = getFontMetrics(g.getFont());
g.drawString("Game Over", (SCREEN_WIDTH - metrics2.stringWidth("Game Over"))/2, SCREEN_HEIGHT/2); // TO GET GAME OVER IN CENTER
//restart the game
g.setFont(new Font("Ink Free", Font.BOLD, 30));
g.setColor(Color.white);
g.drawString("Press Enter to Restart", (SCREEN_WIDTH - metrics.stringWidth("Press Enter to Restart")) / 2, (SCREEN_HEIGHT / 2) + 50);
}
public void restartGame() {
bodyParts = 6;
appleEaten = 0;
direction = 'R';
running = false;
timer.stop();
// Reset snake position
for (int i = 0; i < bodyParts; i++) {
x[i] = 0;
y[i] = 0;
}
startGame();
repaint();
}
@Override
public void actionPerformed (ActionEvent e){
if (running){
move();
checkApple();
checkCollision();
}
repaint();
}
}