-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakeGame.java
More file actions
183 lines (160 loc) · 5.43 KB
/
SnakeGame.java
File metadata and controls
183 lines (160 loc) · 5.43 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
// Java program to implement Snake Game using JFrame
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;
import java.util.Random;
public class SnakeGame extends JFrame {
public SnakeGame() {
add(new GamePanel());
setTitle("Snake Game");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new SnakeGame();
frame.setVisible(true);
});
}
}
class GamePanel extends JPanel implements ActionListener {
private final int TILE_SIZE = 10;
private final int BOARD_WIDTH = 600;
private final int BOARD_HEIGHT = 400;
private LinkedList<Point> snake;
private Point food;
private int directionX, directionY;
private boolean gameOver;
private Timer timer;
public GamePanel() {
setPreferredSize(new Dimension(BOARD_WIDTH, BOARD_HEIGHT));
setBackground(new Color(250, 235, 215));
setFocusable(true);
// Initialize game variables
snake = new LinkedList<>();
snake.add(new Point(BOARD_WIDTH / 2, BOARD_HEIGHT / 2));
directionX = TILE_SIZE;
directionY = 0;
spawnFood();
gameOver = false;
// Key listener for snake movement
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (gameOver) return;
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
if (directionX == 0) {
directionX = -TILE_SIZE;
directionY = 0;
}
break;
case KeyEvent.VK_RIGHT:
if (directionX == 0) {
directionX = TILE_SIZE;
directionY = 0;
}
break;
case KeyEvent.VK_UP:
if (directionY == 0) {
directionY = -TILE_SIZE;
directionX = 0;
}
break;
case KeyEvent.VK_DOWN:
if (directionY == 0) {
directionY = TILE_SIZE;
directionX = 0;
}
break;
}
}
});
// Timer to handle game updates every 100 ms
timer = new Timer(100, this);
timer.start();
}
private void spawnFood() {
Random rand = new Random();
int foodX = rand.nextInt(BOARD_WIDTH / TILE_SIZE) * TILE_SIZE;
int foodY = rand.nextInt(BOARD_HEIGHT / TILE_SIZE) * TILE_SIZE;
food = new Point(foodX, foodY);
}
private void checkCollision() {
Point head = snake.getFirst();
for (int i = 1; i < snake.size(); i++) {
if (head.equals(snake.get(i))) {
gameOver = true;
return;
}
}
// Wall collision
if (head.x < 0 || head.x >= BOARD_WIDTH || head.y < 0 || head.y >= BOARD_HEIGHT) {
gameOver = true;
}
}
private void updateSnake() {
Point head = new Point(snake.getFirst());
head.translate(directionX, directionY);
snake.addFirst(head);
if (head.equals(food)) {
spawnFood();
} else {
snake.removeLast();
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (gameOver) {
repaint();
return;
}
updateSnake();
checkCollision();
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (gameOver) {
g.setColor(Color.RED);
g.setFont(new Font("Helvetica", Font.BOLD, 20));
String message = "Game Over! Press R to Restart or Q to Quit";
FontMetrics metrics = g.getFontMetrics();
g.drawString(message, (BOARD_WIDTH - metrics.stringWidth(message)) / 2, BOARD_HEIGHT / 2);
} else {
// Draw food
g.setColor(new Color(139, 69, 19));
g.fillRect(food.x, food.y, TILE_SIZE, TILE_SIZE);
// Draw snake
g.setColor(Color.BLACK);
for (Point p : snake) {
g.fillRect(p.x, p.y, TILE_SIZE, TILE_SIZE);
}
}
}
@Override
protected void processKeyEvent(KeyEvent e) {
if (gameOver) {
if (e.getID() == KeyEvent.KEY_PRESSED) {
if (e.getKeyCode() == KeyEvent.VK_R) {
restartGame();
} else if (e.getKeyCode() == KeyEvent.VK_Q) {
System.exit(0);
}
}
}
super.processKeyEvent(e);
}
private void restartGame() {
snake.clear();
snake.add(new Point(BOARD_WIDTH / 2, BOARD_HEIGHT / 2));
directionX = TILE_SIZE;
directionY = 0;
spawnFood();
gameOver = false;
timer.start();
}
}