-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakeGame.java
More file actions
100 lines (93 loc) · 3.5 KB
/
SnakeGame.java
File metadata and controls
100 lines (93 loc) · 3.5 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SnakeGame extends JPanel implements ActionListener {
static final int SCREEN_WIDTH = 600;
static final int SCREEN_HEIGHT = 600;
static final int UNIT_SIZE = 25;
static final int GAME_UNITS = (SCREEN_WIDTH * SCREEN_HEIGHT) / UNIT_SIZE;
final int x[] = new int[GAME_UNITS];
final int y[] = new int[GAME_UNITS];
java.util.Random random = new java.util.Random();
int bodyParts = 6;
int applesEaten;
int appleX = random.nextInt((int) (SCREEN_WIDTH / UNIT_SIZE)) * UNIT_SIZE;
int appleY = random.nextInt((int) (SCREEN_HEIGHT / UNIT_SIZE)) * UNIT_SIZE;
char direction = 'R';
boolean running = true;
SnakeGame() {
this.setPreferredSize(new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT));
this.setFocusable(true);
this.addKeyListener(new MyKeyAdapter());
new Timer(150, this).start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
String text = running ? "Score: " + applesEaten + " | ruzhila.cn" : "Game Over";
if (running) {
g.fillOval(appleX, appleY, UNIT_SIZE, UNIT_SIZE);
g.setColor(Color.green);
for (int i = 0; i < bodyParts; i++) {
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
}
}
g.setColor(running ? Color.blue : Color.red);
g.setFont(new Font("Ink Free", Font.BOLD, 40));
g.drawString(text, (SCREEN_WIDTH - getFontMetrics(g.getFont()).stringWidth(text)) / 2, g.getFont().getSize());
}
public void move() {
for (int i = bodyParts; i > 0; i--) {
x[i] = x[(i - 1)];
y[i] = y[(i - 1)];
}
if (direction == 'U') {
y[0] -= UNIT_SIZE;
} else if (direction == 'D') {
y[0] += UNIT_SIZE;
} else if (direction == 'L') {
x[0] -= UNIT_SIZE;
} else if (direction == 'R') {
x[0] += UNIT_SIZE;
}
}
public class MyKeyAdapter extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT && direction != 'R') {
direction = 'L';
} else if (keyCode == KeyEvent.VK_RIGHT && direction != 'L') {
direction = 'R';
} else if (keyCode == KeyEvent.VK_UP && direction != 'D') {
direction = 'U';
} else if (keyCode == KeyEvent.VK_DOWN && direction != 'U') {
direction = 'D';
}
}
}
public void actionPerformed(ActionEvent e) {
if (running) {
move();
if ((x[0] == appleX) && (y[0] == appleY)) {
applesEaten++;
appleX = random.nextInt((int) (SCREEN_WIDTH / UNIT_SIZE)) * UNIT_SIZE;
appleY = random.nextInt((int) (SCREEN_HEIGHT / UNIT_SIZE)) * UNIT_SIZE;
}
for (int i = bodyParts; i > 0; i--) {
if ((x[0] == x[i]) && (y[0] == y[i])) {
running = false;
}
}
if (x[0] < 0 || x[0] > SCREEN_WIDTH || y[0] < 0 || y[0] > SCREEN_HEIGHT) {
running = false;
}
}
repaint();
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new SnakeGame());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}