-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakeGame.java
More file actions
225 lines (191 loc) · 6.58 KB
/
Copy pathSnakeGame.java
File metadata and controls
225 lines (191 loc) · 6.58 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
public class SnakeGame extends JPanel implements ActionListener, KeyListener {
private class Tile {
int x;
int y;
Tile(int x, int y) {
this.x = x;
this.y = y;
}
}
int boardWidth;
int boardHeight;
int tileSize = 25;
// Snake
Tile snakeHead;
ArrayList<Tile> snakeBody;
// Food
Tile food;
Random random;
// Game logic
Timer gameLoop;
int velocityX;
int velocityY;
boolean gameOver = false;
SnakeGame(int boardWidth, int boardHeight) {
this.boardWidth = boardWidth;
this.boardHeight = boardHeight;
setPreferredSize(new Dimension(this.boardWidth, this.boardHeight));
setBackground(Color.black);
addKeyListener(this);
setFocusable(true);
initializeGame();
}
private void initializeGame() {
snakeHead = new Tile(5, 5);
snakeBody = new ArrayList<Tile>();
food = new Tile(10, 10);
random = new Random();
placeFood();
velocityX = 0;
velocityY = 0;
gameOver = false;
if (gameLoop != null) {
gameLoop.stop();
}
gameLoop = new Timer(100, this);
gameLoop.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g) {
if (gameOver) {
// Display game over message
g.setColor(Color.white);
g.setFont(new Font("Arial", Font.BOLD, 36));
String gameOverText = "Game Over";
int textWidth = g.getFontMetrics().stringWidth(gameOverText);
g.drawString(gameOverText, (boardWidth - textWidth) / 2, boardHeight / 2 - 20);
g.setFont(new Font("Arial", Font.PLAIN, 24));
String scoreText = "Score: " + String.valueOf(snakeBody.size());
textWidth = g.getFontMetrics().stringWidth(scoreText);
g.drawString(scoreText, (boardWidth - textWidth) / 2, boardHeight / 2 + 10);
String restartText = "Press SPACE to restart or '0' to exit";
textWidth = g.getFontMetrics().stringWidth(restartText);
g.drawString(restartText, (boardWidth - textWidth) / 2, boardHeight / 2 + 40);
} else {
// Food
g.setColor(Color.red);
g.fillOval(food.x * tileSize, food.y * tileSize, tileSize, tileSize);
// Snake Head
g.setColor(Color.green);
g.fillRect(snakeHead.x * tileSize, snakeHead.y * tileSize, tileSize, tileSize);
// Snake Body
for (int i = 0; i < snakeBody.size(); i++) {
Tile snakePart = snakeBody.get(i);
g.fillRect(snakePart.x * tileSize, snakePart.y * tileSize, tileSize, tileSize);
}
// Score
g.setColor(Color.white);
g.setFont(new Font("Arial", Font.PLAIN, 16));
g.drawString("Score: " + String.valueOf(snakeBody.size()), 10, 20);
}
}
public void placeFood() {
boolean validPosition = false;
while (!validPosition) {
food.x = random.nextInt(boardWidth / tileSize);
food.y = random.nextInt(boardHeight / tileSize);
// Check if the food position overlaps with the snake
validPosition = true;
if (collision(snakeHead, food)) {
validPosition = false;
} else {
for (Tile snakePart : snakeBody) {
if (collision(snakePart, food)) {
validPosition = false;
break;
}
}
}
}
}
public boolean collision(Tile tile1, Tile tile2) {
return tile1.x == tile2.x && tile1.y == tile2.y;
}
public void move() {
if (collision(snakeHead, food)) {
snakeBody.add(new Tile(food.x, food.y));
placeFood();
}
// Snake Body
for (int i = snakeBody.size() - 1; i >= 0; i--) {
Tile snakePart = snakeBody.get(i);
if (i == 0) {
snakePart.x = snakeHead.x;
snakePart.y = snakeHead.y;
} else {
Tile prevSnakePart = snakeBody.get(i - 1);
snakePart.x = prevSnakePart.x;
snakePart.y = prevSnakePart.y;
}
}
// Snake Head
snakeHead.x += velocityX;
snakeHead.y += velocityY;
// Wrap around screen
snakeHead.x = (snakeHead.x + boardWidth / tileSize) % (boardWidth / tileSize);
snakeHead.y = (snakeHead.y + boardHeight / tileSize) % (boardHeight / tileSize);
// Check for collision with snake body
for (int i = 0; i < snakeBody.size(); i++) {
Tile snakePart = snakeBody.get(i);
if (collision(snakeHead, snakePart)) {
gameOver = true;
break;
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
move();
repaint();
if (gameOver) {
gameLoop.stop();
}
}
@Override
public void keyPressed(KeyEvent e) {
if (gameOver) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
initializeGame();
} else if (e.getKeyChar() == '0') {
System.exit(0);
}
} else {
if (e.getKeyCode() == KeyEvent.VK_UP && velocityY != 1) {
velocityX = 0;
velocityY = -1;
} else if (e.getKeyCode() == KeyEvent.VK_DOWN && velocityY != -1) {
velocityX = 0;
velocityY = 1;
} else if (e.getKeyCode() == KeyEvent.VK_LEFT && velocityX != 1) {
velocityX = -1;
velocityY = 0;
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT && velocityX != -1) {
velocityX = 1;
velocityY = 0;
}else if (e.getKeyChar() == '0') {
System.exit(0);
}
}
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake");
SnakeGame snakeGame = new SnakeGame(600, 600);
frame.add(snakeGame);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}