-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.java
More file actions
41 lines (30 loc) · 901 Bytes
/
Snake.java
File metadata and controls
41 lines (30 loc) · 901 Bytes
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
public class Snake {
public int length = 2;
public int direction =0;
public int sX[] = new int[SnakeGameMain.WIDTH*SnakeGameMain.SCALE-1];
public int sY[] = new int[SnakeGameMain.HEIGHT*SnakeGameMain.SCALE-1];
public Snake(int x1, int y1, int x2, int y2){
sX[0]=x1;
sX[1]=x2;
sY[0]=y1;
sY[1]=y2;
}
public void move(){
for(int l=length;l>0;l--){
sX[l] = sX[l-1];
sY[l] = sY[l-1];
}
//up
if(direction == 0) sY[0]--;
//down
if(direction == 2) sY[0]++;
//right
if(direction == 1) sX[0]++;
//left
if(direction == 3) sX[0]--;
if(sY[0]> SnakeGameMain.HEIGHT) sY[0]=0;
if(sY[0]< 0) sY[0]=SnakeGameMain.HEIGHT-1;
if(sX[0]> SnakeGameMain.WIDTH) sX[0]=0;
if(sX[0]< 0) sX[0]=SnakeGameMain.WIDTH-1;
}
}