Witam,
Zacząłem uczyć się libgdx i piszę snake, ale mam problem taki jak w tytule czyli części na siebie nachodzą zamiast być obok siebie. Kod:
package com.patrykkrajewski.snake.entities;
import java.util.ArrayList;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.patrykkrajewski.snake.SnakeGame;
public class Snake {
private ArrayList<Part> parts = new ArrayList<Part>();
private Texture partTexture = new Texture("part.jpg");
private final float VELOCITY = 300.f;
private final int STARTING_PARTS = 15;
private enum Directions {
LEFT, RIGHT, UP, DOWN
}
private Directions direction = Directions.LEFT;
public Snake() {
for (int i = 0; i < STARTING_PARTS; i++) {
System.out.println(i);
addPart();
}
}
private void addPart() {
Part tmp = new Part(partTexture);
if (parts.size() == 0) {
tmp.setPosition(SnakeGame.WIDTH / 2, SnakeGame.HEIGHT / 2);
} else {
tmp.x = parts.get(parts.size() - 1).x + partTexture.getWidth();
tmp.y = parts.get(parts.size() - 1).y;
}
parts.add(tmp);
}
public void draw(SpriteBatch spriteBatch) {
for (Part p : parts) {
p.draw(spriteBatch);
}
}
private void move() {
for (int i = parts.size() - 1; i > 0; i--) {
parts.get(i).x = parts.get(i - 1).x;
parts.get(i).y = parts.get(i - 1).y;
}
Rectangle tmp = parts.get(0);
switch (direction) {
case LEFT:
tmp.x -= VELOCITY * Gdx.graphics.getDeltaTime();
break;
case RIGHT:
tmp.x += VELOCITY * Gdx.graphics.getDeltaTime();
break;
case UP:
tmp.y += VELOCITY * Gdx.graphics.getDeltaTime();
break;
case DOWN:
tmp.y -= VELOCITY * Gdx.graphics.getDeltaTime();
break;
}
}
private void handleInputs() {
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
direction = Directions.LEFT;
} else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
direction = Directions.RIGHT;
} else if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
direction = Directions.UP;
} else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
direction = Directions.DOWN;
}
}
public void update() {
handleInputs();
move();
}
}
Wie ktoś przez co jest to spowodowane?
EDIT: Próbowałem funkcji które dodają przestrzeń między blokami, ale przez to wąż ciągle się prostował.