Skip to content

Commit dedac55

Browse files
committed
Final version with food update
1 parent fe673c5 commit dedac55

File tree

22 files changed

+382
-776
lines changed

22 files changed

+382
-776
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
*.zip
44
*.bak
55
*.jar
6-
*.map
6+
*.map
7+
application.linux
8+
application.windows

SnakeGame/Food.pde

Lines changed: 0 additions & 36 deletions
This file was deleted.

SnakeGame/Position.pde

Lines changed: 0 additions & 52 deletions
This file was deleted.

SnakeGame/Snake.pde

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class Snake {
4646
* @return SnakeBody the snake body at the specified index
4747
*/
4848
public void updatePosition(SnakeMove movement) {
49-
snakeBody.get(0).setMovement(movement);
49+
getSnakeHead().setMovement(movement);
5050
}
5151

5252
/**
@@ -61,8 +61,8 @@ public class Snake {
6161
* Gets the first position of the snake.
6262
* @return Position The first position of the snake.
6363
*/
64-
public Position getFirstPosition() {
65-
return snakeBody.get(0).position;
64+
public int[] getFirstPosition() {
65+
return getSnakeHead().getPosition();
6666
}
6767

6868
/**
@@ -83,7 +83,8 @@ public class Snake {
8383
var movement = lastBody.getMovement();
8484
var offset = movement.getOffset();
8585
var body = new SnakeBody(movement, positions);
86-
body.position.sum(offset[0], offset[1]);
86+
body.setX(body.getX() + offset[0]);
87+
body.setY(body.getY() +offset[1]);
8788
addSnakeBody(body);
8889
}
8990

@@ -97,8 +98,8 @@ public class Snake {
9798
for (int index = snakeBody.size() - 1; index > 0; index--) {
9899
var currentBody = snakeBody.get(index);
99100
var previousBody = snakeBody.get(index - 1);
100-
currentBody.getPosition().setX(previousBody.getPosition().getX());
101-
currentBody.getPosition().setY(previousBody.getPosition().getY());
101+
currentBody.setX(previousBody.getX());
102+
currentBody.setY(previousBody.getY());
102103
currentBody.setMovement(previousBody.getMovement());
103104
}
104105
}
@@ -117,15 +118,15 @@ public class Snake {
117118
* Starts the snake at the specified position.
118119
* @param position The position to start the snake.
119120
*/
120-
public void startSnake(Integer[] position) {
121-
addSnakeBody(new SnakeBody(SnakeMove.UP, new Position(position)));
121+
public void startSnake(int[] position) {
122+
addSnakeBody(new SnakeBody(SnakeMove.UP, position));
122123
}
123124

124125
/**
125126
* Updates the position of all snake pieces of body.
126127
*/
127128
private void updateHeadPosition() {
128-
snakeBody.get(0).doMovement(SnakeGame.VELOCITY);
129+
getSnakeHead().doMovement(SnakeGame.VELOCITY);
129130
}
130131

131132

@@ -134,38 +135,44 @@ public class Snake {
134135
* @return true if there is a collision, false otherwise.
135136
*/
136137
private boolean checkCollisionWithBody() {
137-
var firstPosition = getFirstPosition();
138+
var firstBody = getSnakeHead();
138139
var result = false;
139140
for (int index = 1; index < snakeBody.size(); index++) {
140-
var bodyPos = snakeBody.get(index).getPosition();
141-
if (Math.abs(bodyPos.getX() - firstPosition.getX()) <= BODY_COLISION_TOLERANCE &&
142-
Math.abs(bodyPos.getY() - firstPosition.getY()) <= BODY_COLISION_TOLERANCE) {
141+
var body = snakeBody.get(index);
142+
if (Math.abs(body.getX() - firstBody.getX()) <= BODY_COLISION_TOLERANCE &&
143+
Math.abs(body.getY() - firstBody.getY()) <= BODY_COLISION_TOLERANCE) {
143144
result = true;
144145
break;
145146
}
146147
}
147148
return result;
148149
}
149150

151+
/**
152+
* Gets the snake's head.
153+
* @return SnakeBody The snake's head.
154+
*/
155+
public SnakeBody getSnakeHead() {
156+
return snakeBody.get(0);
157+
}
150158

151159
/**
152160
* Checks if the snake collides with the food.
153161
* @return true if the snake collides with the food, false otherwise.
154162
*/
155163
private boolean checkCollisionWithFood() {
156-
var firstPosition = getFirstPosition();
157-
var foodPosition = SnakeGame.food.getPosition();
158-
return (Math.abs(foodPosition.getX() - firstPosition.getX()) <= FOOD_COLISION_TOLERANCE &&
159-
Math.abs(foodPosition.getY() - firstPosition.getY()) <= FOOD_COLISION_TOLERANCE);
164+
var bodyPos = getSnakeHead();
165+
return (Math.abs(SnakeGame.FOOD_X - bodyPos.getX()) <= FOOD_COLISION_TOLERANCE &&
166+
Math.abs(SnakeGame.FOOD_Y - bodyPos.getY()) <= FOOD_COLISION_TOLERANCE);
160167
}
161168

162169
/**
163170
* Checks if the snake has collided with the game borders.
164171
* @return true if the snake has collided with the borders, false otherwise.
165172
*/
166173
private boolean checkCollisionWithBorders() {
167-
var position = getFirstPosition();
168-
return (position.getX() < SnakeGame.BORDER || position.getX() > SnakeGame.W - SnakeGame.BORDER - SnakeGame.SNAKE_SIZE_W ||
169-
position.getY() < SnakeGame.BORDER || position.getY() > SnakeGame.H - SnakeGame.BORDER - SnakeGame.SNAKE_SIZE_H);
174+
var body = getSnakeHead();
175+
return (body.getX() < SnakeGame.BORDER || body.getX() > SnakeGame.W - SnakeGame.BORDER - SnakeGame.SNAKE_SIZE_W ||
176+
body.getY() < SnakeGame.BORDER || body.getY() > SnakeGame.H - SnakeGame.BORDER - SnakeGame.SNAKE_SIZE_H);
170177
}
171178
}

SnakeGame/SnakeBody.pde

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,47 @@
66

77
public class SnakeBody {
88

9-
public SnakeBody(SnakeMove movement, Position position) {
9+
public SnakeBody(SnakeMove movement, int[] position) {
1010
this.position = position.clone();
1111
this.movement = movement;
1212
}
1313

1414
private SnakeMove movement;
15-
private Position position;
15+
private final int[] position;
1616

17-
public void setMovement(SnakeMove movement) {
18-
this.movement = movement;
17+
public int getX() {
18+
return position[0];
19+
}
20+
21+
public int getY() {
22+
return position[1];
23+
}
24+
25+
public void setX(int x) {
26+
position[0] = x;
1927
}
2028

21-
public void setPosition(Position position) {
22-
this.position = position;
29+
public void setY(int y) {
30+
position[1] = y;
2331
}
2432

25-
public Position getPosition() {
33+
public int[] getPosition() {
2634
return position;
2735
}
2836

37+
public void setMovement(SnakeMove movement) {
38+
this.movement = movement;
39+
}
40+
2941
public SnakeMove getMovement() {
3042
return movement;
3143
}
3244

3345
/**
3446
* Do a movement in the snake body with a velocity value
35-
* @param velocity Integer value of velocity
47+
* @param velocity int value of velocity
3648
*/
3749
public void doMovement(int velocity) {
38-
var positionAux = movement.run(position.toArray(), velocity);
39-
position.setX(positionAux[0]);
40-
position.setY(positionAux[1]);
50+
movement.run(position, velocity);
4151
}
4252
}

0 commit comments

Comments
 (0)