Repository navigation
Expand file tree
/
Copy pathsnakegame.java
More file actions
154 lines (138 loc) · 4.33 KB
/
Copy pathsnakegame.java
File metadata and controls
154 lines (138 loc) · 4.33 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
public class SnakeGame extends JPanel implements ActionListener {
private final int TILE_SIZE = 30;
private final int GRID_SIZE = 20;
private final int SCREEN_SIZE = TILE_SIZE * GRID_SIZE;
private final int[] x = new int[GRID_SIZE * GRID_SIZE];
private final int[] y = new int[GRID_SIZE * GRID_SIZE];
private int snakeLength;
private int foodX, foodY;
private char direction = 'R'; // Start moving right
private boolean running = true;
private Timer timer;
SnakeGame() {
setPreferredSize(new Dimension(SCREEN_SIZE, SCREEN_SIZE));
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(new MyKeyAdapter());
startGame();
}
public void startGame() {
snakeLength = 3;
for (int i = 0; i < snakeLength; i++) {
x[i] = 100 - i * TILE_SIZE;
y[i] = 100;
}
spawnFood();
timer = new Timer(150, this); // Speed control
timer.start();
}
public void spawnFood() {
Random random = new Random();
foodX = random.nextInt(GRID_SIZE) * TILE_SIZE;
foodY = random.nextInt(GRID_SIZE) * TILE_SIZE;
}
@Override
public void actionPerformed(ActionEvent e) {
if (running) {
move();
checkCollision();
checkFood();
}
repaint();
}
public void move() {
for (int i = snakeLength; i > 0; i--) {
x[i] = x[i - 1];
y[i] = y[i - 1];
}
switch (direction) {
case 'U' -> y[0] -= TILE_SIZE;
case 'D' -> y[0] += TILE_SIZE;
case 'L' -> x[0] -= TILE_SIZE;
case 'R' -> x[0] += TILE_SIZE;
}
}
public void checkFood() {
if (x[0] == foodX && y[0] == foodY) {
snakeLength++;
spawnFood();
}
}
public void checkCollision() {
// Wall collision
if (x[0] < 0 || x[0] >= SCREEN_SIZE || y[0] < 0 || y[0] >= SCREEN_SIZE) {
running = false;
}
// Self collision
for (int i = 1; i < snakeLength; i++) {
if (x[0] == x[i] && y[0] == y[i]) {
running = false;
}
}
if (!running)
timer.stop();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (running) {
// Draw food
g.setColor(Color.RED);
g.fillOval(foodX, foodY, TILE_SIZE, TILE_SIZE);
// Draw snake
for (int i = 0; i < snakeLength; i++) {
if (i == 0) {
g.setColor(Color.GREEN); // Snake head
} else {
g.setColor(Color.YELLOW); // Snake body
}
g.fillRect(x[i], y[i], TILE_SIZE, TILE_SIZE);
}
} else {
// Game Over
g.setColor(Color.RED);
g.setFont(new Font("Arial", Font.BOLD, 40));
g.drawString("Game Over!", SCREEN_SIZE / 4, SCREEN_SIZE / 2);
}
}
// Controls
private class MyKeyAdapter extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT -> {
if (direction != 'R')
direction = 'L';
}
case KeyEvent.VK_RIGHT -> {
if (direction != 'L')
direction = 'R';
}
case KeyEvent.VK_UP -> {
if (direction != 'D')
direction = 'U';
}
case KeyEvent.VK_DOWN -> {
if (direction != 'U')
direction = 'D';
}
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake Game");
SnakeGame snakeGame = new SnakeGame();
frame.add(snakeGame);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
}