-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
119 lines (104 loc) · 3.17 KB
/
Game.java
File metadata and controls
119 lines (104 loc) · 3.17 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
package snake;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
/**
*
* @author Jirka
*/
public class Game {
private static final ArrayList<MyPoint> snake = new ArrayList<>(Arrays.asList(new MyPoint(12,12), new MyPoint(12,13), new MyPoint(12,14), new MyPoint(12,15)));
private int k = 0;//směr
private MyPoint last = new MyPoint(0,0);
private boolean gameOver = false;
private static MyPoint bod = new MyPoint(10,5);
private static int[] time = {0,0};
private static int level = 1;
Random random = new Random();
public void next(){
if (!gameOver){
last = snake.get(snake.size()-1);
if (snake.size()-1 >= 1){
for (int i = snake.size()-1; i >= 1; i--){
snake.get(i).setPoint(snake.get(i-1));
}
}
if (snake.get(0).getX() == bod.getX() && snake.get(0).getY() == bod.getY()){
snake.add(new MyPoint(last.getX(),last.getY()));
bod.setPoint(random.nextInt(25),random.nextInt(25));
}
switch(k){
case 0://w
if (snake.get(0).getY() > 0)
snake.get(0).decY();
else
snake.get(0).setY(24);
break;
case 1://s
if (snake.get(0).getY() < 24)
snake.get(0).incY();
else
snake.get(0).setY(0);
break;
case 2://a
if (snake.get(0).getX() > 0)
snake.get(0).decX();
else
snake.get(0).setX(24);
break;
case 3://d
if (snake.get(0).getX() < 24)
snake.get(0).incX();
else
snake.get(0).setX(0);
break;
}
for (int i = 1; i < snake.size(); i++){
if (snake.get(i).getX() == snake.get(0).getX() && snake.get(i).getY() == snake.get(0).getY()){
gameOver = true;
}
}
}
}
public void setKey(int k){
this.k = k;
}
public ArrayList<MyPoint> getSnake(){
return (snake);
}
public MyPoint getBod(){
return(bod);
}
public int[] getTime(){
return(time);
}
public void setTime(){
time[0] ++;
if(time[0] == 60)
{
time[0] = 0;
time[1] ++;
}
}
public void setLevel(int level){
this.level = level;
}
public int getLevel(){
return (level);
}
public boolean getGameOver(){
return (gameOver);
}
public void reset(){
snake.clear();
snake.add(new MyPoint(12,12));
snake.add(new MyPoint(12,13));
snake.add(new MyPoint(12,14));
snake.add(new MyPoint(12,15));
time[0] = 0;
time[1] = 0;
bod = new MyPoint(10,5);
k = 0;
gameOver = false;
}
}