-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnakeGame.java
180 lines (145 loc) · 4.6 KB
/
SnakeGame.java
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
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;
int y;
Tile(int x, int y){
this.x = x;
this.y = y;
}
}
int boardWidth;
int boardHeight;
int tileSize = 20;
Tile snakehead;
ArrayList<Tile> snakebody;
Tile food;
Random random;
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);
snakehead = new Tile(5,5);
snakebody = new ArrayList<Tile>();
food = new Tile(15,15);
random = new Random();
placeFood();
velocityx = 1;
velocityy = 0;
gameLoop = new Timer(100, this);
gameLoop.start();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g){
// grid(x1,y1,x2,y2)
// for(int i=0; i<boardWidth/tileSize; i++){
// g.setColor(Color.gray);
// g.drawLine(i*tileSize, 0, i*tileSize, boardHeight);
// g.drawLine(0, i*tileSize, boardWidth, i*tileSize);
// }
// snake head
g.setColor(Color.green);
g.fill3DRect(snakehead.x * tileSize, snakehead.y * tileSize, tileSize, tileSize, true);
// snake body
for(int i=0; i<snakebody.size(); i++){
Tile snakepart = snakebody.get(i);
g.fill3DRect(snakepart.x * tileSize, snakepart.y * tileSize, tileSize, tileSize, true);
}
//food
g.setColor(Color.yellow);
g.fill3DRect(food.x * tileSize, food.y * tileSize, tileSize, tileSize, true);
// score
g.setFont(new Font("Montserrat", Font.PLAIN, 14));
if(gameover){
g.setColor(Color.magenta);
g.drawString("Game Over: " + String.valueOf(snakebody.size()), tileSize-15, tileSize);
}
else{
g.setColor(Color.cyan);
g.drawString("Score: " + String.valueOf(snakebody.size()), tileSize-15, tileSize);
}
}
public void placeFood(){
food.x = random.nextInt(boardWidth/tileSize); // 600/20 = 25
food.y = random.nextInt(boardHeight/tileSize); // 600/20 = 25
}
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();
}
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;
}
}
snakehead.x += velocityx;
snakehead.y += velocityy;
// gameOVER
for(int i=0; i<snakebody.size(); i++){
Tile snakepart = snakebody.get(i);
if(collision(snakehead, snakepart)){
gameover = true;
}
}
if(snakehead.x*tileSize<0 || snakehead.x*tileSize>boardWidth || snakehead.y*tileSize<0 || snakehead.y*tileSize>boardHeight){
gameover = true;
}
}
@Override
public void actionPerformed(ActionEvent e){
move();
repaint();
if(gameover){
gameLoop.stop();
}
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_W && velocityy!=1){
velocityx = 0;
velocityy = -1;
}
else if (e.getKeyCode() == KeyEvent.VK_S && velocityy!=-1){
velocityx = 0;
velocityy = 1;
}
else if (e.getKeyCode() == KeyEvent.VK_A && velocityx!=1){
velocityx = -1;
velocityy = 0;
}
else if (e.getKeyCode() == KeyEvent.VK_D && velocityx!=-1){
velocityx = 1;
velocityy = 0;
}
}
// DO NOT NEED
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
}