-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snake.java
166 lines (134 loc) · 3.94 KB
/
Snake.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
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
/* lstting
* cis 120 f18
* hw 09
*/
public class Snake implements Comparable<Snake> {
private int length;
private int[][] position;
private boolean left;
private boolean right;
private boolean up;
private boolean down;
private Image blob;
public Snake(int max) {
length = 3;
position = new int[max][2]; // 2 values (x,y) stored for each segment
for (int i = 0; i < length; i++) {
position[i][0] = 50 - Model.SEGMENT_SIZE * i; // set head x position at 50
position[i][1] = 200; // set head y position at 200
}
left = false;
right = true; // snake starts off moving to the right
up = false;
down = false;
loadImage(); // load snake body img
}
public int getSize() {
return length;
}
public void incrSize() {
int xTail = position[length-1][0];
int yTail = position[length-1][1];
if (left) {
position[length][0] = xTail + Model.SEGMENT_SIZE; // set new segment x to right of tail
} else if (right) {
position[length][0] = xTail - Model.SEGMENT_SIZE; // set new segment x to left of tail
}
if (up) {
position[length][1] = yTail - Model.SEGMENT_SIZE; // set new segment y to below tail
} else if (down) {
position[length][1] = yTail + Model.SEGMENT_SIZE; // set new segment y to above tail
}
length++; // increase length
}
public void move() {
for (int i = length-1; i > 0 ; i--) {
position[i][0] = position[i-1][0]; // move each segment up to prev segment's coords
position[i][1] = position[i-1][1];
}
// move head accordingly
if (left) {
position[0][0] -= Model.SEGMENT_SIZE;
} else if (right) {
position[0][0] += Model.SEGMENT_SIZE;
}
if (up) {
position[0][1] -= Model.SEGMENT_SIZE;
} else if (down) {
position[0][1] += Model.SEGMENT_SIZE;
}
}
// checks if the snake has bumped into itself or into the board boundaries
public boolean bumper() {
for (int i = length-1; i > 0; i--) {
if (position[0][0] == position[i][0] && position[0][1] == position[i][1]) {
return false;
}
}
if (position[0][0] < 0 || position[0][0] > Model.BOARD_WIDTH-10 ||
position[0][1] < 0 || position[0][1] > Model.BOARD_HEIGHT-10) {
return false;
}
return true;
}
public void eatCandy(Candy c) {
if (position[0][0] == c.getX() && position[0][1] == c.getY()) { // if overlap
c.setEaten(); // set boolean of candy
for (int i = 0; i < c.getScore(); i++) { // increase length of snake
incrSize(); // for each point of the candy's score
}
}
}
public void loadImage() {
ImageIcon b = new ImageIcon(this.getClass().getResource("/img/body.png"));
blob = b.getImage();
}
public void draw(Graphics g, Model m) {
for (int i = 0; i < length; i++) {
g.drawImage(blob, position[i][0], position[i][1], Model.SEGMENT_SIZE-1, Model.SEGMENT_SIZE-1, m);
}
}
public boolean getLeft() {
return left;
}
public boolean getRight() {
return right;
}
public boolean getUp() {
return up;
}
public boolean getDown() {
return down;
}
public void goLeft() {
left = true;
right = false;
up = false;
down = false;
}
public void goRight() {
left = false;
right = true;
up = false;
down = false;
}
public void goUp() {
left = false;
right = false;
up = true;
down = false;
}
public void goDown() {
left = false;
right = false;
up = false;
down = true;
}
@Override
public int compareTo(Snake o) {
return 0;
}
}