Skip to content

Commit 33cfe88

Browse files
committed
Background change and add soft movement
1 parent b338884 commit 33cfe88

File tree

11 files changed

+396
-289
lines changed

11 files changed

+396
-289
lines changed

SnakeGame/Snake.pde

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,17 @@ public class Snake {
167167
bodyY[0] < SnakeGame.BORDER || bodyY[0] > SnakeGame.H - SnakeGame.BORDER - SnakeGame.SNAKE_SIZE_H);
168168
}
169169

170+
/**
171+
* Fix body grid when snake moves over Y
172+
*/
170173
private void fixBodyYGrid() {
171174
gap = (byte)(bodyY[0] % SnakeGame.GRID_SIZE);
172175
bodyY[0] = (short) (gap < 5? bodyY[0] - gap : bodyY[0] + gap);
173176
}
174177

178+
/**
179+
* Fix body grid when snake moves over X
180+
*/
175181
private void fixBodyXGrid() {
176182
gap = (byte)(bodyY[0] % SnakeGame.GRID_SIZE);
177183
bodyX[0] = (short) (gap < 5? bodyX[0] - gap : bodyX[0] + gap);

SnakeGame/SnakeGame.pde

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@ private static Snake snake;
99
private static SnakeMove lastMove = null;
1010
private static Random random;
1111

12+
public static boolean isRunning;
1213
private static PFont FONT_SCORE, FONT_GAME;
14+
private static short[] FOOD_COLOR;
1315
public static final short[] BG_COLOR = {0, 0, 0};
1416
public static final short W = 600, H = 400;
15-
public static final byte SNAKE_SIZE_W = 10, SNAKE_SIZE_H = 10, BORDER = 20, GRID_SIZE = 10, VELOCITY = 10;
16-
private static short[] FOOD_COLOR;
17-
public static byte MOVE_INTERVAL = 100;
18-
private static int score;
19-
public static boolean isRunning;
17+
public static final byte SNAKE_SIZE_W = 10, SNAKE_SIZE_H = 10, BORDER = 20, GRID_SIZE = 10, VELOCITY = 10, FOOD_SIZE = 8;
18+
private static byte gapGrid, MOVE_INTERVAL = 100;
19+
private static short score;
2020
private static long currentTime = 0l, lastMoveTime;
2121
private static short FOOD_X, FOOD_Y;
22-
private static byte gapGrid;
22+
23+
2324

2425
/**
2526
* Setup configuration of the game
@@ -118,7 +119,7 @@ void drawBorders() {
118119
void drawFood() {
119120
stroke(255, 255, 255);
120121
fill(FOOD_COLOR[0], FOOD_COLOR[1], FOOD_COLOR[2]);
121-
rect(FOOD_X, FOOD_Y, GRID_SIZE, GRID_SIZE);
122+
rect(FOOD_X, FOOD_Y, FOOD_SIZE, FOOD_SIZE);
122123
}
123124

124125
/** GAME LOGIC **/

SnakeGame/application.linux/source/Snake.pde

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,23 @@ public class Snake {
1313
private short[] bodyX, bodyY;
1414
private short arrIndex;
1515
private SnakeMove[] movements;
16+
private byte gap;
1617

1718
public Snake() {
1819
init();
1920
}
2021

22+
public short getPositionX(int index) {
23+
return bodyX[index];
24+
}
25+
26+
public short getPositionY(int index) {
27+
return bodyY[index];
28+
}
29+
30+
/**
31+
* Init the arrays and run the calc to the screen
32+
*/
2133
private void init() {
2234
arrIndex = 0;
2335
var pieces = piecesPerScreen();
@@ -26,7 +38,11 @@ public class Snake {
2638
movements = new SnakeMove[pieces];
2739
}
2840

29-
public int piecesPerScreen() {
41+
/**
42+
* Calc how much pieces per screen the snake can handle
43+
* @return int result of calc
44+
*/
45+
private int piecesPerScreen() {
3046
return (SnakeGame.W * SnakeGame.H) / (SnakeGame.SNAKE_SIZE_W * SnakeGame.SNAKE_SIZE_H);
3147
}
3248

@@ -48,19 +64,11 @@ public class Snake {
4864
movements[0] = movement;
4965
}
5066

51-
/**
52-
* Gets the first position of the snake.
53-
* @return Position The first position of the snake.
54-
*/
55-
public short[] getFirstPosition() {
56-
return new short[]{bodyX[0], bodyY[0]};
57-
}
58-
5967
/**
6068
* Destroys the snake by clearing its body and positions.
6169
*/
6270
public void destroySnake() {
63-
init();
71+
arrIndex = 0;
6472
}
6573

6674
/**
@@ -69,20 +77,20 @@ public class Snake {
6977
* The new body is positioned one movement offset away from the last body.
7078
*/
7179
public void newBody() {
72-
short[] lastPosition = {bodyX[arrIndex], bodyY[arrIndex]};
73-
var movement = movements[arrIndex];
74-
var offset = movement.getOffset();
80+
SnakeMove movement = movements[arrIndex];
81+
bodyX[arrIndex+1] = (short) (getPositionX(arrIndex) + movement.getOffsetX());
82+
bodyY[arrIndex+1] = (short) (getPositionY(arrIndex) + movement.getOffsetY());
83+
movements[arrIndex+1] = movement;
7584
arrIndex++;
76-
bodyX[arrIndex] = (short)(lastPosition[0] + offset[0]);
77-
bodyY[arrIndex] = (short)(lastPosition[1] + offset[1]);
78-
movements[arrIndex] = movement;
7985
}
8086

87+
/**
88+
* Get the size of array
89+
*/
8190
public short getSize() {
8291
return (short) (arrIndex + 1);
8392
}
8493

85-
8694
/**
8795
* Updates the movement of the snake and checks for collisions with food, borders and body.
8896
* If the snake collides with food, a new food is created and the score is updated.
@@ -109,32 +117,31 @@ public class Snake {
109117

110118
/**
111119
* Starts the snake at the specified position.
112-
* @param position The position to start the snake.
120+
* @param positionX The position X to start the snake.
121+
* @param positionY The position Y to start the snake.
113122
*/
114-
public void startSnake(short[] position) {
115-
bodyX[0] = position[0];
116-
bodyY[0] = position[1];
123+
public void startSnake(short positionX, short positionY) {
124+
bodyX[0] = positionX;
125+
bodyY[0] = positionY;
117126
movements[0] = SnakeMove.UP;
118127
}
119128

120129
/**
121-
* Updates the position of all snake pieces of body.
130+
* Updates the position of snake head body
122131
*/
123132
private void updateHeadPosition() {
124133
doMovement(movements[0], SnakeGame.VELOCITY);
125134
}
126135

127-
128136
/**
129137
* Checks if the snake's head collides with its body.
130138
* @return true if there is a collision, false otherwise.
131139
*/
132140
private boolean checkCollisionWithBody() {
133141
var result = false;
134142
for (int index = 1; index < getSize(); index++) {
135-
short[] body = {bodyX[index], bodyY[index]};
136-
if (Math.abs(body[0] - bodyX[0]) <= BODY_COLISION_TOLERANCE &&
137-
Math.abs(body[1] - bodyY[0]) <= BODY_COLISION_TOLERANCE) {
143+
if (Math.abs(bodyX[index] - bodyX[0]) <= BODY_COLISION_TOLERANCE &&
144+
Math.abs(bodyY[index] - bodyY[0]) <= BODY_COLISION_TOLERANCE) {
138145
result = true;
139146
break;
140147
}
@@ -160,23 +167,38 @@ public class Snake {
160167
bodyY[0] < SnakeGame.BORDER || bodyY[0] > SnakeGame.H - SnakeGame.BORDER - SnakeGame.SNAKE_SIZE_H);
161168
}
162169

170+
private void fixBodyYGrid() {
171+
gap = (byte)(bodyY[0] % SnakeGame.GRID_SIZE);
172+
bodyY[0] = (short) (gap < 5? bodyY[0] - gap : bodyY[0] + gap);
173+
}
174+
175+
private void fixBodyXGrid() {
176+
gap = (byte)(bodyY[0] % SnakeGame.GRID_SIZE);
177+
bodyX[0] = (short) (gap < 5? bodyX[0] - gap : bodyX[0] + gap);
178+
}
179+
163180
/**
164181
* Do a movement in the snake body with a velocity value
182+
* @param movement actual snakeMove
165183
* @param velocity short value of velocity
166184
*/
167185
public void doMovement(SnakeMove movement, short velocity) {
168186
switch(movement) {
169187
case UP:
170188
bodyY[0] -= velocity;
189+
fixBodyYGrid();
171190
break;
172191
case DOWN:
173192
bodyY[0] += velocity;
193+
fixBodyYGrid();
174194
break;
175195
case LEFT:
176196
bodyX[0] -= velocity;
197+
fixBodyXGrid();
177198
break;
178199
case RIGHT:
179200
bodyX[0] += velocity;
201+
fixBodyXGrid();
180202
break;
181203
}
182204
}

0 commit comments

Comments
 (0)