-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnakeGame.java
176 lines (156 loc) · 5.55 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
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 {
int boardHeight;
int boardWidth;
int tileSize = 25;
// Snake
Tile SnakeHead;
ArrayList<Tile> snakeBody;
// Food
Tile food;
Random random;
ImageIcon images = new ImageIcon("foodIcon.jpg");
Image image = images.getImage();
// ImageIcon snakeImage = new ImageIcon("snake.jpg");
// Image snkImage = snakeImage.getImage();
// game logic
Timer gameLoop;
int velocityX;
int velocityY;
boolean gameOver = false;
private class Tile {
int x, y;
Tile(int x, int y) {
this.x = x;
this.y = y;
}
}
SnakeGame(int boardHeight, int boardWidth) {
this.boardHeight = boardHeight;
this.boardWidth = boardWidth;
SnakeHead = new Tile(5, 5);
setPreferredSize(new Dimension(this.boardHeight, this.boardWidth));
setBackground(Color.black);
addKeyListener(this);
setFocusable(true);
food = new Tile(10, 10);
random = new Random();
placeFood();
velocityX = 0;
velocityY = 1;
gameLoop = new Timer(150, this);
gameLoop.start();
snakeBody = new ArrayList<Tile>();
}
public void placeFood() {
food.x = random.nextInt(boardWidth / tileSize);
food.y = random.nextInt(boardHeight / tileSize);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public boolean collision(Tile tile1, Tile tile2) {
return tile1.x == tile2.x && tile1.y == tile2.y;
}
public void draw(Graphics g) {
// Grid
// for (int i = 0; i < boardHeight / tileSize; i++) {
// g.drawLine(i * tileSize, 0, i * tileSize, boardHeight);
// g.drawLine(0, i * tileSize, boardWidth, i * tileSize);
// }
// food
if (image != null) {
g.drawImage(image, food.x * tileSize, food.y * tileSize,tileSize,tileSize, this);
}else{
g.setColor(Color.red);
g.fill3DRect(food.x * tileSize, food.y * tileSize, tileSize, tileSize, true);
}
// g.fillRect(food.x * tileSize, food.y * tileSize, tileSize, tileSize);
// SnakeHead
// if(snkImage !=null){
// g.drawImage(snkImage, SnakeHead.x * tileSize, SnakeHead.y * tileSize, tileSize, tileSize, this);
// }else{
g.setColor(Color.green);
// g.fillRect(SnakeHead.x * tileSize, SnakeHead.y * tileSize, tileSize,
// tileSize);
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.fillRect(snakePart.x * tileSize, snakePart.y * tileSize, tileSize,
// tileSize);
g.fill3DRect(snakePart.x * tileSize, snakePart.y * tileSize, tileSize, tileSize, true);
}
// Score
g.setFont(new Font("Arial", Font.PLAIN, 16));
if (gameOver) {
g.setColor(Color.red);
g.drawString("Game Over: " + String.valueOf(snakeBody.size()), tileSize - 16, tileSize);
} else {
g.drawString("Score: " + String.valueOf(snakeBody.size()), tileSize - 16, tileSize);
}
}
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;
}
}
// Snake head
SnakeHead.x += velocityX;
SnakeHead.y += velocityY;
// game over condition
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 > boardWidth) {
gameOver = true;
}
}
public void actionPerformed(ActionEvent e) {
move();
repaint();
if (gameOver) {
gameLoop.stop();
}
}
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;
}
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
}
// velocity is change position over time