Optimized Ticking and Rendering #5
40
src/main/dev/lh/Food.java
Normal file
40
src/main/dev/lh/Food.java
Normal file
@ -0,0 +1,40 @@
|
||||
delvh
commented
Review
```suggestion
* @since Snake 1.2
```
delvh
commented
Review
```suggestion
* @since Snake 1.2
```
delvh
commented
Review
```suggestion
* @since Snake 1.2
```
delvh
commented
Review
```suggestion
* @since Snake 1.2
```
delvh
commented
Review
```suggestion
* @since Snake 1.2
```
delvh
commented
Review
```suggestion
* @since Snake 1.2
```
delvh
commented
Review
```suggestion
* @since Snake 1.2
```
delvh
commented
Review
```suggestion
* @since Snake 1.2
```
|
||||
package dev.lh;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
import java.awt.Color;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
import java.awt.Graphics2D;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
import java.awt.Rectangle;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* Project: <strong>Snake</strong><br>
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* File: <strong>Food.java</strong><br>
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* Created: <strong>01.07.2020</strong><br>
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @author Kai S. K. Engelbart
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.1
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public final class Food implements Updateable {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private final Color color;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private final int lengthBonus;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private final Rectangle bounds;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public Food(Color color, int lengthBonus, Rectangle bounds) {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
this.color = color;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
this.lengthBonus = lengthBonus;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
this.bounds = bounds;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public void checkCollision(Snake snake) {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
if (bounds.intersects(snake.getHead())) {}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
@Override
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public void render(Graphics2D g) {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
g.setColor(color);
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
g.fill(bounds);
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public int getLengthBonus() { return lengthBonus; }
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public Rectangle getBounds() { return bounds; }
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
@ -2,202 +2,68 @@ package dev.lh;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
import static java.awt.Color.*;
|
||||
|
||||
import java.awt.*;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
import java.awt.Color;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
import java.awt.Rectangle;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
import java.util.Random;
|
||||
|
||||
import dev.lh.ui.GameWindow;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
* Generates food items with predefined properties at random positions and calculates the next
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* spawning time.
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* <p>
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* Project: <strong>Snake</strong><br>
|
||||
* File: <strong>FoodFactory.java</strong><br>
|
||||
* Created: <strong>11 Mar 2020</strong><br>
|
||||
*
|
||||
* @author Leon Hofmeister
|
||||
* @author Kai S. K. Engelbart
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
public class FoodFactory {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public final class FoodFactory {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private int width, height;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private long nextSpawnTime;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private Random random = new Random();
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private static final Color[] FOOD_COLORS = {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
WHITE, YELLOW, ORANGE, RED, BLUE
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
};
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private static final int[] FOOD_LENGTH_BONUSES = {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
40, 15, 6, 2, 1
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
};
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
/**
|
||||
* This enum contains all possible variations of foods. The higher the ordinal
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* of an element, the less it is worth.<br>
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* <br>
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* Project: <strong>Snake</strong><br>
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* File: <strong>FoodFactory.java</strong><br>
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* Created: <strong>11 Mar 2020</strong><br>
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @author Leon Hofmeister
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.0
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* Initializes a food factory.
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @param width the width of the viewport
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @param height the height of the viewport
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.1
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
public enum Food {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public FoodFactory(int width, int height) {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
this.width = width;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
this.height = height;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* Use if white food is wanted.
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
white(
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
WHITE, 40
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
),
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* Use if yellow food is wanted.
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
yellow(
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
YELLOW, 15
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
),
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* Use if orange food is wanted.
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
orange(
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
ORANGE, 6
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
),
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* Use if red food is wanted.
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
red(
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
RED, 2
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
),
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* Use if blue food is wanted.
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
blue(
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
BLUE, 1
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @return a new food item
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.1
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public synchronized Food spawn() {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
nextSpawnTime = System.currentTimeMillis() + random.nextInt(15000) + 1000;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
int seed = random.nextInt(5);
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
return new Food(
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
FOOD_COLORS[seed],
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
FOOD_LENGTH_BONUSES[seed],
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
new Rectangle(random.nextInt(width - 100) + 50,
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
random.nextInt(height - 100) + 50,
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
seed * 10,
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
seed * 10
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
)
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
);
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* The color of the food item.
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public final Color color;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* The length bonus of the food item.
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public final int lengthBonus;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private Food(Color color, int lengthBonus) {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
this.color = color;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
this.lengthBonus = lengthBonus;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private static FoodFactory foodFactory = new FoodFactory();
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private long timeOfNextFood;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private Point pFood;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private Food nextFood = Food.white;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private int rectangleSize = 6;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private FoodFactory() {}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @return the (singleton) instance of FoodFactory
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.0
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public static FoodFactory getInstance() { return foodFactory; }
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @return a new {@link Food} object without its position
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.0
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public Food generateFood() {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
nextFood = Food.values()[new Random().nextInt(Food.values().length)];
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
rectangleSize = nextFood.ordinal() + 2;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
setTimeToNextFoodMillis();
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
return nextFood;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the amount of time that needs to pass before the next food object
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* will be constructed.
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.0
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @return the time after which a new food item should be spawned
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.1
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
public void setTimeToNextFoodMillis() {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
timeOfNextFood = System.currentTimeMillis() + new Random().nextInt(15000) + 1000;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @return the type of the next food
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.0
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public Food getNextFood() { return nextFood; }
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @param nextFood the type the next food should have
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.0
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public void setNext(Food nextFood) { this.nextFood = nextFood; }
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @return the time at which a new food object will be automatically created
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.0
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public long getTimeOfNextFood() { return timeOfNextFood; }
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @param width the width of the currently used {@link GameWindow}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @param height the height of the currently used {@link GameWindow}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @return the position of the new {@link Food} object
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.0
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public Point generateFoodLocation(int width, int height) {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
assert (width > 100 && height > 100);
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
Random r = new Random();
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
return pFood = new Point(r.nextInt(width - 100) + 50, r.nextInt(height - 100) + 50);
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @return the size of the corresponding food (length = width)
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.0
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public int getRectangleSize() { return rectangleSize; }
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @return the location of the currently displayed food
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.0
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public Point getFoodLocation() { return pFood; }
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* Sets the color of the given {@link Graphics} object according to the type of
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* food.
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @param g the graphics object to paint
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.0
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public void colorOfFood(Graphics g) {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
g.setColor(nextFood.color);
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @param g the {@link Graphics} object used to paint the current food object
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.0
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public void paintFood(Graphics g) {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
colorOfFood(g);
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
g.fillRect(pFood.x, pFood.y, 5 * rectangleSize, 5 * rectangleSize);
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @param snakeHead the the head of a {@link Snake} object
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @return true if the current food intersects with the snake head
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.0
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public boolean checkCollision(Rectangle snakeHead) {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
int s = rectangleSize * 5;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
Rectangle food = new Rectangle(pFood, new Dimension(s, s));
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
return food.intersects(snakeHead);
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @return the length that will be added to the snake
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.0
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public int getAdditionalLength() {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
return nextFood.lengthBonus;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public long getNextSpawnTime() { return nextSpawnTime; }
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
46
src/main/dev/lh/Handler.java
Normal file
46
src/main/dev/lh/Handler.java
Normal file
@ -0,0 +1,46 @@
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
package dev.lh;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
import java.awt.Graphics2D;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* Manages the state of game objects.
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* <p>
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* Project: <strong>Snake</strong><br>
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* File: <strong>Handler.java</strong><br>
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* Created: <strong>01.07.2020</strong><br>
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @author Kai S. K. Engelbart
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.1
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public final class Handler implements Updateable {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private Snake snake = new Snake(7);
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private FoodFactory foodFactory;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private volatile Food food;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public Handler(Snake snake, FoodFactory foodFactory) {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
this.snake = snake;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
this.foodFactory = foodFactory;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
food = foodFactory.spawn();
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
@Override
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public void tick() {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
snake.tick();
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
food.tick();
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
// Check for food collision
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
if (snake.getHead().intersects(food.getBounds())) {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
snake.addLength(food.getLengthBonus());
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
food = foodFactory.spawn();
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
if (System.currentTimeMillis() > foodFactory.getNextSpawnTime())
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
food = foodFactory.spawn();
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
@Override
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public void render(Graphics2D g) {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
snake.render(g);
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
food.render(g);
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
@ -7,7 +7,6 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import dev.lh.ui.Endscreen;
|
||||
import dev.lh.ui.GameWindow;
|
||||
|
||||
/**
|
||||
* Project: <strong>Snake</strong><br>
|
||||
@ -51,7 +50,6 @@ public class Snake implements Updateable {
|
||||
DOWN;
|
||||
}
|
||||
|
||||
private static FoodFactory foodFactory = FoodFactory.getInstance();
|
||||
private static Endscreen endscreen;
|
||||
private Direction direction = Direction.RIGHT;
|
||||
private int length;
|
||||
@ -103,14 +101,8 @@ public class Snake implements Updateable {
|
||||
Main.getGame().close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the current {@link Direction} of the snake
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
public Direction getRichtung() { return direction; }
|
||||
|
||||
@Override
|
||||
public void nextFrame() {
|
||||
public void tick() {
|
||||
int velX = 0, velY = 0;
|
||||
switch (direction) {
|
||||
case UP:
|
||||
@ -140,18 +132,11 @@ public class Snake implements Updateable {
|
||||
return;
|
||||
}
|
||||
// TODO: Test on Linux
|
||||
if (!Main.getGame().getBounds().contains(tiles.get(0))) {
|
||||
if (!Main.getGame().getBounds().contains(getHead())) {
|
||||
gameOver();
|
||||
System.out.println("Snake went out of bounds.");
|
||||
return;
|
||||
}
|
||||
// TODO: Move to Food class
|
||||
// Case if snake eats food
|
||||
if (foodFactory.checkCollision(tiles.get(0))) {
|
||||
addLength(foodFactory.getAdditionalLength());
|
||||
GameWindow game = Main.getGame();
|
||||
game.newFood();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -160,9 +145,21 @@ public class Snake implements Updateable {
|
||||
tiles.forEach(g::fill);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the current {@link Direction} of the snake
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
public Direction getDirection() { return direction; }
|
||||
|
||||
/**
|
||||
* @param direction the new {@link Direction} of the snake
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
public void setDirection(Direction direction) { this.direction = direction; }
|
||||
|
||||
/**
|
||||
* @return a rectangle representing the head of the snake
|
||||
* @since Snake 1.1
|
||||
*/
|
||||
public Rectangle getHead() { return tiles.get(0); }
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ public interface Updateable {
|
||||
*
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
void nextFrame();
|
||||
default void tick() {}
|
||||
|
||||
/**
|
||||
* Renders the object.
|
||||
@ -28,5 +28,5 @@ public interface Updateable {
|
||||
* @param g the graphics object that is used to render this object
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
void render(Graphics2D g);
|
||||
default void render(Graphics2D g) {}
|
||||
}
|
||||
|
98
src/main/dev/lh/Viewport.java
Normal file
98
src/main/dev/lh/Viewport.java
Normal file
@ -0,0 +1,98 @@
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
package dev.lh;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
import java.awt.Canvas;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
import java.awt.Color;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
import java.awt.Graphics2D;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
import java.util.Timer;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
import java.util.TimerTask;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* Implements a hardware-accelerated rendering loop.
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* <p>
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* Project: <strong>Snake</strong><br>
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* File: <strong>Viewport.java</strong><br>
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* Created: <strong>01.07.2020</strong><br>
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @author Kai S. K. Engelbart
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.0
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public class Viewport extends Canvas {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private static final long serialVersionUID = 1L;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
// Enable OpenGL hardware acceleration
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
static {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
System.setProperty("sun.java2d.trace", "timestamp,log,count");
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
System.setProperty("sun.java2d.transaccel", "True");
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
System.setProperty("sun.java2d.opengl", "True");
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private Updateable gameRoot;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private Timer timer = new Timer();
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private TimerTask renderTask;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @param gameRoot the game object responsible for updating the rest
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.0
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public Viewport(Updateable gameRoot) {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
this.gameRoot = gameRoot;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
setIgnoreRepaint(true);
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* Starts the render task.
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.1
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public void start() {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
if (renderTask != null)
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
renderTask.cancel();
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
else
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
createBufferStrategy(2);
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
renderTask = new TimerTask() {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private long lastTime = System.currentTimeMillis();
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
@Override
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public void run() {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
long time = System.currentTimeMillis();
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
double dt = (time - lastTime) * 1E-3;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
lastTime = time;
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
gameRoot.tick();
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
render();
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
};
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
timer.schedule(renderTask, 0, 100);
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
/**
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* Stops the render task.
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
* @since Snake 1.1
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
*/
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
public void stop() {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
renderTask.cancel();
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
private void render() {
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
Graphics2D g = (Graphics2D) getBufferStrategy().getDrawGraphics();
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
// Clear the screen
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
g.setColor(Color.BLACK);
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
g.fillRect(0, 0, getWidth(), getHeight());
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
// Perform the actual rendering
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
gameRoot.render(g);
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
// Flip buffers
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
g.dispose();
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
getBufferStrategy().show();
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
// Synchronize with display refresh rate
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
getToolkit().sync();
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
||||
}
|
||||
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
```suggestion
* @since Snake 1.2
```
|
@ -1,15 +1,14 @@
|
||||
package dev.lh.ui;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.Timer;
|
||||
|
||||
import dev.lh.FoodFactory;
|
||||
import dev.lh.Snake;
|
||||
import dev.lh.*;
|
||||
import dev.lh.Snake.Direction;
|
||||
|
||||
/**
|
||||
@ -22,21 +21,20 @@ import dev.lh.Snake.Direction;
|
||||
*/
|
||||
public class GameWindow extends JFrame {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private Snake s = new Snake(7);
|
||||
private FoodFactory foodFactory = FoodFactory.getInstance();
|
||||
private Timer timer;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private Viewport viewport;
|
||||
|
||||
/**
|
||||
* @param title the title of the frame
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
public GameWindow(String title) {
|
||||
// Initialize window
|
||||
super(title);
|
||||
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
|
||||
setBounds(new Rectangle(size));
|
||||
setLocation(0, 0);
|
||||
setLocationRelativeTo(null);
|
||||
setMinimumSize(size);
|
||||
setPreferredSize(size);
|
||||
setMaximumSize(size);
|
||||
@ -44,19 +42,14 @@ public class GameWindow extends JFrame {
|
||||
setResizable(false);
|
||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
|
||||
add(new JPanel() {
|
||||
// Initialize game objects
|
||||
Snake snake = new Snake(7);
|
||||
FoodFactory foodFactory = new FoodFactory(getWidth(), getHeight());
|
||||
Handler handler = new Handler(snake, foodFactory);
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.setColor(Color.black);
|
||||
g.fillRect(0, 0, getWidth(), getHeight());
|
||||
s.render((Graphics2D) g);
|
||||
foodFactory.paintFood(g);
|
||||
}
|
||||
});
|
||||
// Initialize viewport
|
||||
viewport = new Viewport(handler);
|
||||
add(viewport);
|
||||
|
||||
addKeyListener(new KeyAdapter() {
|
||||
|
||||
@ -66,56 +59,40 @@ public class GameWindow extends JFrame {
|
||||
switch (e.getKeyCode()) {
|
||||
case KeyEvent.VK_W:
|
||||
case KeyEvent.VK_UP:
|
||||
if (!s.getRichtung().equals(Direction.DOWN)) s.setDirection(Direction.UP);
|
||||
if (!snake.getDirection().equals(Direction.DOWN))
|
||||
snake.setDirection(Direction.UP);
|
||||
break;
|
||||
case KeyEvent.VK_A:
|
||||
case KeyEvent.VK_LEFT:
|
||||
if (!s.getRichtung().equals(Direction.RIGHT)) s.setDirection(Direction.LEFT);
|
||||
if (!snake.getDirection().equals(Direction.RIGHT))
|
||||
snake.setDirection(Direction.LEFT);
|
||||
break;
|
||||
case KeyEvent.VK_S:
|
||||
case KeyEvent.VK_DOWN:
|
||||
if (!s.getRichtung().equals(Direction.UP)) s.setDirection(Direction.DOWN);
|
||||
if (!snake.getDirection().equals(Direction.UP))
|
||||
snake.setDirection(Direction.DOWN);
|
||||
break;
|
||||
case KeyEvent.VK_D:
|
||||
case KeyEvent.VK_RIGHT:
|
||||
if (!s.getRichtung().equals(Direction.LEFT)) s.setDirection(Direction.RIGHT);
|
||||
if (!snake.getDirection().equals(Direction.LEFT))
|
||||
snake.setDirection(Direction.RIGHT);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
newFood();
|
||||
timer = new Timer(
|
||||
50,
|
||||
evt -> {
|
||||
s.nextFrame();
|
||||
if (System.currentTimeMillis() >= foodFactory.getTimeOfNextFood())
|
||||
newFood();
|
||||
repaint();
|
||||
}
|
||||
);
|
||||
timer.start();
|
||||
|
||||
setVisible(true);
|
||||
viewport.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates new food.
|
||||
*
|
||||
* @since Snake 1.1
|
||||
*/
|
||||
public void newFood() {
|
||||
foodFactory.generateFood();
|
||||
foodFactory.generateFoodLocation(getWidth(), getHeight());
|
||||
}
|
||||
|
||||
/**
|
||||
* Disposes this frame
|
||||
* Disposes this frame.
|
||||
*
|
||||
* @since Snake 1.1
|
||||
*/
|
||||
public void close() {
|
||||
timer.stop();
|
||||
viewport.stop();
|
||||
setVisible(false);
|
||||
dispose();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user