Skip to content

Commit e58926c

Browse files
authored
Snake Game
1 parent dcc6fba commit e58926c

File tree

2 files changed

+225
-0
lines changed

2 files changed

+225
-0
lines changed

Snake_Game/SnakeGme.java

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
import java.awt.*;
2+
import java.awt.event.ActionEvent;
3+
import java.awt.event.ActionListener;
4+
import java.awt.event.KeyAdapter;
5+
import java.awt.event.KeyEvent;
6+
import java.util.Random;
7+
import javax.swing.*;
8+
9+
public class SnakeGme extends JPanel implements ActionListener {
10+
11+
private final int TILE_SIZE = 25;
12+
private final int BOARD_WIDTH = 800;
13+
private final int BOARD_HEIGHT = 800;
14+
private final int ALL_TILES = (BOARD_WIDTH * BOARD_HEIGHT) / (TILE_SIZE * TILE_SIZE);
15+
private final int DELAY = 140;
16+
17+
private final int[] x = new int[ALL_TILES];
18+
private final int[] y = new int[ALL_TILES];
19+
20+
private int snakeLength;
21+
private int foodX;
22+
private int foodY;
23+
24+
private boolean leftDirection = false;
25+
private boolean rightDirection = true;
26+
private boolean upDirection = false;
27+
private boolean downDirection = false;
28+
private boolean inGame = true;
29+
30+
private Timer timer;
31+
32+
public SnakeGme() {
33+
initBoard();
34+
}
35+
36+
private void initBoard() {
37+
addKeyListener(new TAdapter());
38+
setBackground(Color.black);
39+
setFocusable(true);
40+
41+
setPreferredSize(new Dimension(BOARD_WIDTH, BOARD_HEIGHT));
42+
loadGame();
43+
}
44+
45+
private void loadGame() {
46+
snakeLength = 3;
47+
48+
for (int z = 0; z < snakeLength; z++) {
49+
x[z] = 50 - z * TILE_SIZE;
50+
y[z] = 50;
51+
}
52+
53+
locateFood();
54+
55+
timer = new Timer(DELAY, this);
56+
timer.start();
57+
}
58+
59+
@Override
60+
public void paintComponent(Graphics g) {
61+
super.paintComponent(g);
62+
doDrawing(g);
63+
}
64+
65+
private void doDrawing(Graphics g) {
66+
if (inGame) {
67+
g.setColor(Color.red);
68+
g.fillRect(foodX, foodY, TILE_SIZE, TILE_SIZE);
69+
70+
for (int z = 0; z < snakeLength; z++) {
71+
if (z == 0) {
72+
g.setColor(Color.green);
73+
} else {
74+
g.setColor(Color.yellow);
75+
}
76+
g.fillRect(x[z], y[z], TILE_SIZE, TILE_SIZE);
77+
}
78+
79+
Toolkit.getDefaultToolkit().sync();
80+
} else {
81+
gameOver(g);
82+
}
83+
}
84+
85+
private void gameOver(Graphics g) {
86+
String msg = "Game Over";
87+
Font small = new Font("Helvetica", Font.BOLD, 14);
88+
FontMetrics metr = getFontMetrics(small);
89+
90+
g.setColor(Color.white);
91+
g.setFont(small);
92+
g.drawString(msg, (BOARD_WIDTH - metr.stringWidth(msg)) / 2, BOARD_HEIGHT / 2);
93+
}
94+
95+
private void checkFood() {
96+
if ((x[0] == foodX) && (y[0] == foodY)) {
97+
snakeLength++;
98+
locateFood();
99+
}
100+
}
101+
102+
private void move() {
103+
for (int z = snakeLength; z > 0; z--) {
104+
x[z] = x[z - 1];
105+
y[z] = y[z - 1];
106+
}
107+
108+
if (leftDirection) {
109+
x[0] -= TILE_SIZE;
110+
}
111+
112+
if (rightDirection) {
113+
x[0] += TILE_SIZE;
114+
}
115+
116+
if (upDirection) {
117+
y[0] -= TILE_SIZE;
118+
}
119+
120+
if (downDirection) {
121+
y[0] += TILE_SIZE;
122+
}
123+
}
124+
125+
private void checkCollision() {
126+
for (int z = snakeLength; z > 0; z--) {
127+
if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
128+
inGame = false;
129+
}
130+
}
131+
132+
if (y[0] >= BOARD_HEIGHT) {
133+
inGame = false;
134+
}
135+
136+
if (y[0] < 0) {
137+
inGame = false;
138+
}
139+
140+
if (x[0] >= BOARD_WIDTH) {
141+
inGame = false;
142+
}
143+
144+
if (x[0] < 0) {
145+
inGame = false;
146+
}
147+
148+
if (!inGame) {
149+
timer.stop();
150+
}
151+
}
152+
153+
private void locateFood() {
154+
Random random = new Random();
155+
foodX = random.nextInt(BOARD_WIDTH / TILE_SIZE) * TILE_SIZE;
156+
foodY = random.nextInt(BOARD_HEIGHT / TILE_SIZE) * TILE_SIZE;
157+
}
158+
159+
@Override
160+
public void actionPerformed(ActionEvent e) {
161+
if (inGame) {
162+
checkFood();
163+
checkCollision();
164+
move();
165+
}
166+
167+
repaint();
168+
}
169+
170+
private class TAdapter extends KeyAdapter {
171+
@Override
172+
public void keyPressed(KeyEvent e) {
173+
int key = e.getKeyCode();
174+
175+
if ((key == KeyEvent.VK_LEFT) && (!rightDirection)) {
176+
leftDirection = true;
177+
upDirection = false;
178+
downDirection = false;
179+
}
180+
181+
if ((key == KeyEvent.VK_RIGHT) && (!leftDirection)) {
182+
rightDirection = true;
183+
upDirection = false;
184+
downDirection = false;
185+
}
186+
187+
if ((key == KeyEvent.VK_UP) && (!downDirection)) {
188+
upDirection = true;
189+
rightDirection = false;
190+
leftDirection = false;
191+
}
192+
193+
if ((key == KeyEvent.VK_DOWN) && (!upDirection)) {
194+
downDirection = true;
195+
rightDirection = false;
196+
leftDirection = false;
197+
}
198+
}
199+
}
200+
}

Snake_Game/app.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import javax.swing.*;
2+
3+
public class app extends JFrame {
4+
5+
public app() {
6+
initUI();
7+
}
8+
9+
private void initUI() {
10+
add(new SnakeGme());
11+
setResizable(false);
12+
pack();
13+
14+
setTitle("Snake");
15+
setLocationRelativeTo(null);
16+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
17+
}
18+
19+
public static void main(String[] args) {
20+
SwingUtilities.invokeLater(() -> {
21+
JFrame ex = new app();
22+
ex.setVisible(true);
23+
});
24+
}
25+
}

0 commit comments

Comments
 (0)