-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakePanel.java
More file actions
142 lines (130 loc) · 3.65 KB
/
SnakePanel.java
File metadata and controls
142 lines (130 loc) · 3.65 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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
//bug: food can appear under the snake
@SuppressWarnings("serial")
public class SnakePanel extends JPanel implements ActionListener, KeyListener{
private int WIDTH = 200; //WIDTH and HEIGHT must be evenly divisible by blockSize
private int HEIGHT = 200;
private int speed = 100;
List<int[]> snakePos = new ArrayList<int[]>();
private int[] foodPos = new int[2];
int snakeDir;
int blockSize = 10;
Random rand = new Random();
Timer timer;
private int points;
private JLabel pointsPanel;
public SnakePanel(JLabel label){
super();
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setFocusable(true);
addKeyListener(this);
timer = new Timer(speed, this);
timer.setInitialDelay(speed);
timer.start();
int[] initialPosition = {WIDTH / 2, HEIGHT / 2};
snakePos.add(initialPosition);
foodPos[0] = rand.nextInt(WIDTH / blockSize) * blockSize;
foodPos[1] = rand.nextInt(HEIGHT / blockSize) * blockSize;
snakeDir = 3;
points = 0;
pointsPanel = label;
pointsPanel.setText("Points: " + points);
}
private boolean gameOver(){
int[] headPos = snakePos.get(0);
if(headPos[0] < 0 || headPos[0] > WIDTH - blockSize){
return true;
}
if(headPos[1] < 0 || headPos[1] > HEIGHT - blockSize){
return true;
}
for(int i = 1; i < snakePos.size(); i++){
int[] blockPos = snakePos.get(i);
if(headPos[0] == blockPos[0] && headPos[1] == blockPos[1]){
return true;
}
}
return false;
}
public void paintComponent(Graphics g){
g.setColor(Color.GRAY);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.WHITE);
g.fillRect(foodPos[0], foodPos[1], blockSize, blockSize);
g.setColor(Color.BLACK);
for(int[] block : snakePos){
g.fillRect(block[0], block[1], blockSize, blockSize);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(gameOver()){
snakePos.clear();
int[] initialPosition = {WIDTH / 2, HEIGHT / 2};
snakePos.add(initialPosition);
foodPos[0] = rand.nextInt(WIDTH / blockSize) * blockSize;
foodPos[1] = rand.nextInt(HEIGHT / blockSize) * blockSize;
snakeDir = 3;
points = 0;
pointsPanel.setText("Points: " + points);
}else{
int[] headPos = snakePos.get(0);
int[] newHead = new int[2];
newHead[0] = headPos[0];
newHead[1] = headPos[1];
switch(snakeDir){
case 1: newHead[1] -= blockSize;
break;
case 2: newHead[1] += blockSize;
break;
case 3: newHead[0] -= blockSize;
break;
case 4: newHead[0] += blockSize;
break;
}
snakePos.add(0, newHead);
if(foodPos[0] == headPos[0] && foodPos[1] == headPos[1]){
foodPos[0] = rand.nextInt(WIDTH / blockSize) * blockSize;
foodPos[1] = rand.nextInt(HEIGHT / blockSize) * blockSize;
points += 100;
pointsPanel.setText("Points: " + points);
}else{
snakePos.remove(snakePos.size() - 1);
}
}
repaint();
}
@Override
public void keyPressed(KeyEvent event) {
int keyCode = event.getKeyCode();
if(keyCode == KeyEvent.VK_UP && snakeDir != 2){
snakeDir = 1;
}else if(keyCode == KeyEvent.VK_DOWN && snakeDir != 1){
snakeDir = 2;
}else if(keyCode == KeyEvent.VK_LEFT && snakeDir != 4){
snakeDir = 3;
}else if(keyCode == KeyEvent.VK_RIGHT && snakeDir != 3){
snakeDir = 4;
}
}
@Override
public void keyReleased(KeyEvent e) {
//unused
}
@Override
public void keyTyped(KeyEvent e) {
//unused
}
}