Updated missing Javadoc in whole repository
* reformatted whole repository * fixed bug enabling 180° turnaround
This commit is contained in:
@ -9,86 +9,136 @@ import java.util.List;
|
||||
|
||||
import dev.lh.ui.GameWindow;
|
||||
|
||||
/**
|
||||
* Project: <strong>Snake</strong><br>
|
||||
* File: <strong>Snake.java</strong><br>
|
||||
* Created: <strong>11 Mar 2020</strong><br>
|
||||
*
|
||||
* @author Leon Hofmeister
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
public class Snake implements Updateable {
|
||||
|
||||
public static enum Direction{
|
||||
Left, Right, Up, Down;
|
||||
/**
|
||||
* This enum contains all possible directions for the {@link Snake}.<br>
|
||||
* <br>
|
||||
* Project: <strong>Snake</strong><br>
|
||||
* File: <strong>Snake.java</strong><br>
|
||||
* Created: <strong>11 Mar 2020</strong><br>
|
||||
*
|
||||
* @author Leon Hofmeister
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
public static enum Direction {
|
||||
/**
|
||||
* Use if the snake should head left.
|
||||
*/
|
||||
Left,
|
||||
|
||||
/**
|
||||
* Use if the snake should head right.
|
||||
*/
|
||||
Right,
|
||||
|
||||
/**
|
||||
* Use if the snake should head up.
|
||||
*/
|
||||
Up,
|
||||
|
||||
/**
|
||||
* Use if the snake should head down.
|
||||
*/
|
||||
Down;
|
||||
}
|
||||
private Direction Richtung;
|
||||
private int length;
|
||||
private List<Point> tiles = new ArrayList<>();
|
||||
private static FoodFactory foodFactory = FoodFactory.getInstance();
|
||||
private final int snakeSize = 10;
|
||||
|
||||
|
||||
private Direction Richtung;
|
||||
private int length;
|
||||
private List<Point> tiles = new ArrayList<>();
|
||||
private static FoodFactory foodFactory = FoodFactory.getInstance();
|
||||
private final int snakeSize = 10;
|
||||
|
||||
/**
|
||||
* Constructs a new Snake with the given length in tiles.
|
||||
*
|
||||
* @param length the length of the snake in tiles
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
public Snake(int length) {
|
||||
this.length = length;
|
||||
Richtung = Direction.Left;
|
||||
|
||||
for(int i = 0; i<length;i++) {
|
||||
tiles.add(new Point(320-50*i, 240));
|
||||
}
|
||||
|
||||
}//End Constructor
|
||||
this.length = length;
|
||||
Richtung = Direction.Left;
|
||||
|
||||
for (int i = 0; i < length; i++)
|
||||
tiles.add(new Point(320 - 50 * i, 240));
|
||||
|
||||
}// End Constructor
|
||||
|
||||
@Override
|
||||
public void tick() {
|
||||
public void nextFrame() {
|
||||
int velX = 0, velY = 0;
|
||||
switch(Richtung) {
|
||||
|
||||
case Up:
|
||||
velY=-snakeSize;
|
||||
break;
|
||||
case Down:
|
||||
velY=snakeSize;
|
||||
break;
|
||||
case Left:
|
||||
velX=-snakeSize;
|
||||
break;
|
||||
case Right:
|
||||
velX=snakeSize;
|
||||
break;
|
||||
}//switch
|
||||
Point next = (Point) tiles.get(0).clone(), cur;
|
||||
tiles.get(0).x+=velX;
|
||||
tiles.get(0).y+=velY;
|
||||
|
||||
|
||||
for(int i = 1;i<length;i++) {
|
||||
cur = tiles.get(i);
|
||||
tiles.set(i,(Point) next.clone());
|
||||
next = cur;
|
||||
}//for
|
||||
// if(tiles.get(0).x<=0||tiles.get(0).x>=)
|
||||
if(foodFactory.checkCollision(new Rectangle(tiles.get(0).x, tiles.get(0).y, snakeSize, snakeSize))){
|
||||
addLength(foodFactory.getAdditionalLength());
|
||||
GameWindow game = Main.getGame();
|
||||
game.newFood();
|
||||
}
|
||||
|
||||
}//End tick
|
||||
switch (Richtung) {
|
||||
|
||||
case Up:
|
||||
velY = -snakeSize;
|
||||
break;
|
||||
case Down:
|
||||
velY = snakeSize;
|
||||
break;
|
||||
case Left:
|
||||
velX = -snakeSize;
|
||||
break;
|
||||
case Right:
|
||||
velX = snakeSize;
|
||||
break;
|
||||
}// switch
|
||||
Point next = (Point) tiles.get(0).clone(), cur;
|
||||
tiles.get(0).x += velX;
|
||||
tiles.get(0).y += velY;
|
||||
|
||||
for (int i = 1; i < length; i++) {
|
||||
cur = tiles.get(i);
|
||||
tiles.set(i, (Point) next.clone());
|
||||
next = cur;
|
||||
} // for
|
||||
// if(tiles.get(0).x<=0||tiles.get(0).x>=)
|
||||
if (foodFactory.checkCollision(new Rectangle(tiles.get(0).x, tiles.get(0).y, snakeSize, snakeSize))) {
|
||||
addLength(foodFactory.getAdditionalLength());
|
||||
GameWindow game = Main.getGame();
|
||||
game.newFood();
|
||||
}
|
||||
|
||||
}// End tick
|
||||
|
||||
@Override
|
||||
public void render(Graphics g) {
|
||||
g.setColor(Color.green);
|
||||
|
||||
for (int i = 0; i<length;i++) {
|
||||
|
||||
for (int i = 0; i < length; i++)
|
||||
g.fillRect(tiles.get(i).x, tiles.get(i).y, snakeSize, snakeSize);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}//End render
|
||||
|
||||
public Direction getRichtung() {
|
||||
return Richtung;
|
||||
}
|
||||
}// End render
|
||||
|
||||
public void setRichtung(Direction richtung) {
|
||||
Richtung = richtung;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the current {@link Direction} of the snake
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
public Direction getRichtung() { return Richtung; }
|
||||
|
||||
/**
|
||||
* @param richtung the new {@link Direction} of the snake
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
public void setRichtung(Direction richtung) { Richtung = richtung; }
|
||||
|
||||
/**
|
||||
* Adds the given length to the current snake object
|
||||
*
|
||||
* @param additional the number of tiles to add
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
public void addLength(int additional) {
|
||||
for(int i=0;i<additional;i++) {
|
||||
tiles.add(null);
|
||||
}
|
||||
length+=additional;
|
||||
Point last = tiles.get(tiles.size() - 1);
|
||||
for (int i = 0; i < additional; i++)
|
||||
tiles.add(last);
|
||||
length += additional;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user