Repository navigation
Expand file tree
/
Copy pathSnakeGame.java
More file actions
175 lines (152 loc) · 5.41 KB
/
Copy pathSnakeGame.java
File metadata and controls
175 lines (152 loc) · 5.41 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
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
public class SnakeGame extends JPanel implements ActionListener, KeyListener {
private class Tile {
int x, y;
String label;
Tile(int x, int y, String label) {
this.x = x;
this.y = y;
this.label = label;
}
}
int boardWidth, boardHeight;
int tileSize = 25;
Tile snakeHead;
ArrayList<Tile> snakeBody;
Tile food;
Random random;
int velocityX = 1, velocityY = 0;
Timer gameLoop;
boolean gameOver = false;
String[] foodSequence = ("GYANPRAKASHTIWARI").split("");
int foodIndex = 0;
int numberStart = 0;
SnakeGame(int boardWidth, int boardHeight) {
this.boardWidth = boardWidth;
this.boardHeight = boardHeight;
setPreferredSize(new Dimension(boardWidth, boardHeight));
setBackground(Color.black);
addKeyListener(this);
setFocusable(true);
snakeHead = new Tile(5, 5, "");
snakeBody = new ArrayList<>();
food = new Tile(10, 10, foodSequence[foodIndex]);
random = new Random();
gameLoop = new Timer(150, this);
gameLoop.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g) {
g.setColor(Color.darkGray);
for (int i = 0; i < boardWidth / tileSize; i++) {
g.drawLine(i * tileSize, 0, i * tileSize, boardHeight);
g.drawLine(0, i * tileSize, boardWidth, i * tileSize);
}
// Food
g.setColor(Color.orange);
g.fillOval(food.x * tileSize, food.y * tileSize, tileSize, tileSize);
g.setColor(Color.black);
g.setFont(new Font("Arial", Font.BOLD, 14));
g.drawString(food.label, food.x * tileSize + 7, food.y * tileSize + 17);
// Snake Head
g.setColor(new Color(144, 238, 144));
g.fillRoundRect(snakeHead.x * tileSize, snakeHead.y * tileSize, tileSize, tileSize, 10, 10);
// Snake Body with labels
g.setColor(new Color(0, 180, 0));
for (Tile part : snakeBody) {
g.fillRoundRect(part.x * tileSize, part.y * tileSize, tileSize, tileSize, 10, 10);
g.setColor(Color.black);
g.setFont(new Font("Arial", Font.BOLD, 14));
g.drawString(part.label, part.x * tileSize + 7, part.y * tileSize + 17);
g.setColor(new Color(0, 180, 0));
}
// Score
g.setFont(new Font("Arial", Font.BOLD, 16));
g.setColor(Color.white);
if (gameOver) {
g.setColor(Color.red);
g.drawString("Game Over! Score: " + snakeBody.size(), tileSize, tileSize);
} else {
g.drawString("Score: " + snakeBody.size(), tileSize, tileSize);
}
}
public void placeFood() {
food.x = random.nextInt(boardWidth / tileSize);
food.y = random.nextInt(boardHeight / tileSize);
if (foodIndex < foodSequence.length) {
food.label = foodSequence[foodIndex];
} else {
food.label = String.valueOf(numberStart++);
}
}
public void move() {
if (collision(snakeHead, food)) {
snakeBody.add(new Tile(food.x, food.y, food.label));
foodIndex++;
placeFood();
}
for (int i = snakeBody.size() - 1; i >= 0; i--) {
Tile part = snakeBody.get(i);
if (i == 0) {
part.x = snakeHead.x;
part.y = snakeHead.y;
} else {
Tile prev = snakeBody.get(i - 1);
part.x = prev.x;
part.y = prev.y;
}
}
snakeHead.x += velocityX;
snakeHead.y += velocityY;
// Wrap-around
if (snakeHead.x < 0) snakeHead.x = boardWidth / tileSize - 1;
if (snakeHead.x >= boardWidth / tileSize) snakeHead.x = 0;
if (snakeHead.y < 0) snakeHead.y = boardHeight / tileSize - 1;
if (snakeHead.y >= boardHeight / tileSize) snakeHead.y = 0;
for (Tile part : snakeBody) {
if (collision(snakeHead, part)) {
gameOver = true;
}
}
}
public boolean collision(Tile t1, Tile t2) {
return t1.x == t2.x && t1.y == t2.y;
}
@Override
public void actionPerformed(ActionEvent e) {
move();
repaint();
if (gameOver) {
gameLoop.stop();
}
}
@Override
public void keyPressed(KeyEvent e) {
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;
}
}
@Override public void keyTyped(KeyEvent e) {}
@Override public void keyReleased(KeyEvent e) {}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake Game");
SnakeGame gamePanel = new SnakeGame(600, 600);
frame.add(gamePanel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}