-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathsnake_game_java
More file actions
162 lines (143 loc) · 4.02 KB
/
snake_game_java
File metadata and controls
162 lines (143 loc) · 4.02 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package Snake;
class Main {
static final int MAX_WIDTH = .., MAX_HEIGHT = ..;
PrintStream out;
//create coordinates for head, tail, apple
//create array of the whole snake
//create variables with: score, speed, direction, apple
Main() {
out = new PrintStream(System.out);
//initialize speed
}
void start() {
setSpeed(speed);
out.printf("Press an arrow to start the game\n");
//set in start position
while (true) {
game();
}
}
void setStartPosition() {
score = 0;
direction = apple = null;
newSnake();
appleCheck();
}
void game() {
if (//arrow is pressed) {
processEvent(//arrow);
} else {
//method: check if there has to be a new apple
//method: delete the current snake
//method: you can go through the "walls"/sides
//method: determine direction
//method: place (new) snake
//method: check if you are game over
showChanges();
}
}
void processEvent(Event event) {
if (//arrow left) {
if (//direction is not right) {
direction = "left";
}
} else if (//arrow right) {
if (//direction is not left) {
direction = "right";
}
} else if (//arrow up) {
if (//directon is not down) {
direction = "up";
}
} else if (//arrow down) {
if (//direction is not up) {
direction = "down";
}
}
}
void deleteCurrentSnake() {
//loop through the whole snake array and delete every element
}
void appleCheck() {
if (appleResult() == "true") {
setApple();
//place on the obtained coordinates an apple
out.printf("score= %d\n", score);
}
}
String appleResult() {
if (//start position) {
apple = "true";
} else if (//you hit the apple) {
apple = "true";
score++;
snake.numberOfElements++;
} else {
apple = "false";
}
return apple;
}
void setApple() {
Random randomGenerator = new Random();
coordinateApple = new Coordinate(randomGenerator.nextInt(MAX_WIDTH), randomGenerator.nextInt(MAX_HEIGHT));
if (//apple is placed on the snake) {
setApple();
}
}
void makePermeableSides() {
if (coordinateHeadSnake != null) {
if (//direction is right and coordinateHeadSnake.x is bigger than the width) {
coordinateHeadSnake = new Coordinate(-1, coordinateHeadSnake.y);
} else if (//direction is left and coordinateHeadSnake.x <= 0) {
coordinateHeadSnake = new Coordinate(MAX_WIDTH, coordinateHeadSnake.y);
} else if (//direction is down and coordinateHeadSnake.x is bigger than the height) {
coordinateHeadSnake = new Coordinate(coordinateHeadSnake.x, -1);
} else if (//direction is up and coordinateHeadSnake.x <= 0) {
coordinateHeadSnake = new Coordinate(coordinateHeadSnake.x, MAX_HEIGHT);
}
}
}
void setDirection() {
if (//not direction not null) {
if (//direction left) {
coordinateHeadSnake = new Coordinate(coordinateHeadSnake.x - 1, coordinateHeadSnake.y);
} else if (//direction right) {
coordinateHeadSnake = new Coordinate(coordinateHeadSnake.x + 1, coordinateHeadSnake.y);
} else if (//direction up) {
coordinateHeadSnake = new Coordinate(coordinateHeadSnake.x, coordinateHeadSnake.y - 1);
} else if (//direction down) {
coordinateHeadSnake = new Coordinate(coordinateHeadSnake.x, coordinateHeadSnake.y + 1);
}
snake.moveSnake(coordinateHeadSnake);
}
}
void placeSnake() {
//place the whole snake array
}
void checkGameOver() {
if (// the snake hits itself) {
out.printf("Game Over -_-\n" + "Press an arrow to start another game\n");
//clear the whole field
//reset ==> go to start position
}
}
void newSnake() {
coordinateHeadSnake = new Coordinate(0,1);
coordinateTail = new Coordinate(0,0);
snake = new Snake();
snake.addCoordinateAtTheBack(coordinateHeadSnake);
snake.addCoordinateAtTheBack(coordinateTail);
}
void place(int x, int y, Object type) {
//method that places a new element in the game
}
void showChanges() {
//method that shows your array
}
void setSpeed(speed) {
//method that sets framesPerSecond to speed you want
}
public static void main(String[] args) {
new Main().start();
}
}