Simplified some calculations

This commit is contained in:
Kai S. K. Engelbart 2020-06-30 19:16:59 +02:00
parent ad329447a9
commit c7035ff2c5
No known key found for this signature in database
GPG Key ID: 0A48559CA32CB48F
5 changed files with 34 additions and 60 deletions

View File

@ -1,6 +1,7 @@
package dev.lh; package dev.lh;
import java.awt.*; import java.awt.*;
import java.util.Random;
import dev.lh.ui.GameWindow; import dev.lh.ui.GameWindow;
@ -56,7 +57,7 @@ public class FoodFactory {
private long timeOfNextFood; private long timeOfNextFood;
Point pFood = null; private Point pFood;
private Food nextFood = Food.white; private Food nextFood = Food.white;
@ -75,27 +76,8 @@ public class FoodFactory {
* @since Snake 1.0 * @since Snake 1.0
*/ */
public Food generateFood() { public Food generateFood() {
int nextFoodIs = (int) Math.floor(Math.random() * 5); int n = new Random().nextInt(Food.values().length + 1);
switch (nextFoodIs) { nextFood = n == Food.values().length ? generateFood() : Food.values()[n];
case 0:
nextFood = Food.white;
break;
case 1:
nextFood = Food.yellow;
break;
case 2:
nextFood = Food.orange;
break;
case 3:
nextFood = Food.red;
break;
case 4:
nextFood = Food.blue;
break;
default:
nextFood = generateFood();
}
rectangleSize = nextFood.ordinal() + 2; rectangleSize = nextFood.ordinal() + 2;
setTimeToNextFoodMillis(); setTimeToNextFoodMillis();
return nextFood; return nextFood;
@ -107,7 +89,9 @@ public class FoodFactory {
* *
* @since Snake 1.0 * @since Snake 1.0
*/ */
public void setTimeToNextFoodMillis() { timeOfNextFood = System.currentTimeMillis() + (int) Math.round(Math.random() * 15000 + 1000); } public void setTimeToNextFoodMillis() {
timeOfNextFood = System.currentTimeMillis() + new Random().nextInt(15000) + 1000;
}
/** /**
* @return the type of the next food * @return the type of the next food
@ -134,11 +118,9 @@ public class FoodFactory {
* @since Snake 1.0 * @since Snake 1.0
*/ */
public Point generateFoodLocation(int width, int height) { public Point generateFoodLocation(int width, int height) {
pFood = new Point((int) Math.round(Math.random() * width), (int) Math.round(Math.random() * height)); assert (width > 100 && height > 100);
if (pFood.x < 50 || pFood.x > width - 50 || pFood.y < 50 || pFood.y > height - 50) { Random r = new Random();
pFood.x = (pFood.x < 50) ? 50 : (pFood.x > width - 50) ? width - 50 : pFood.x; pFood = new Point(r.nextInt(width - 100) + 50, r.nextInt(height - 100) + 50);
pFood.y = (pFood.y < 50) ? 50 : (pFood.y > height - 50) ? height - 50 : pFood.y;
}
return pFood; return pFood;
} }
@ -178,7 +160,7 @@ public class FoodFactory {
case blue: case blue:
g.setColor(Color.blue); g.setColor(Color.blue);
break; break;
}// switch }
} }
/** /**

View File

@ -1,9 +1,6 @@
package dev.lh; package dev.lh;
import java.awt.Color; import java.awt.*;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -54,7 +51,7 @@ public class Snake implements Updateable {
private static FoodFactory foodFactory = FoodFactory.getInstance(); private static FoodFactory foodFactory = FoodFactory.getInstance();
private static Endscreen endscreen; private static Endscreen endscreen;
private Direction Richtung; private Direction direction;
private int length; private int length;
private List<Point> tiles = new ArrayList<>(); private List<Point> tiles = new ArrayList<>();
private final int snakeSize = 10; private final int snakeSize = 10;
@ -67,7 +64,7 @@ public class Snake implements Updateable {
*/ */
public Snake(int length) { public Snake(int length) {
this.length = length; this.length = length;
Richtung = Direction.Right; direction = Direction.Right;
// adding the initial tiles of the snake // adding the initial tiles of the snake
for (int i = 0; i < length; i++) for (int i = 0; i < length; i++)
tiles.add(new Point(320 - snakeSize * i, 240)); tiles.add(new Point(320 - snakeSize * i, 240));
@ -114,12 +111,12 @@ public class Snake implements Updateable {
* @return the current {@link Direction} of the snake * @return the current {@link Direction} of the snake
* @since Snake 1.0 * @since Snake 1.0
*/ */
public Direction getRichtung() { return Richtung; } public Direction getRichtung() { return direction; }
@Override @Override
public void nextFrame() { public void nextFrame() {
int velX = 0, velY = 0; int velX = 0, velY = 0;
switch (Richtung) { switch (direction) {
case Up: case Up:
velY = -snakeSize; velY = -snakeSize;
break; break;
@ -173,8 +170,8 @@ public class Snake implements Updateable {
} }
/** /**
* @param richtung the new {@link Direction} of the snake * @param direction the new {@link Direction} of the snake
* @since Snake 1.0 * @since Snake 1.0
*/ */
public void setRichtung(Direction richtung) { Richtung = richtung; } public void setDirection(Direction direction) { this.direction = direction; }
} }

View File

@ -54,7 +54,6 @@ public class Endscreen extends JDialog {
Image resultImage = Toolkit.getDefaultToolkit() Image resultImage = Toolkit.getDefaultToolkit()
.getImage(this.getClass().getResource((score < goodOrBadResult) ? "/Try_Again.jpg" : "/1211548-200.png")); .getImage(this.getClass().getResource((score < goodOrBadResult) ? "/Try_Again.jpg" : "/1211548-200.png"));
resultImage.flush(); resultImage.flush();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -33,7 +33,6 @@ public class GameWindow extends JFrame {
*/ */
public GameWindow(String title) { public GameWindow(String title) {
super(title); super(title);
newFood();
Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(new Rectangle(size)); setBounds(new Rectangle(size));
setLocation(0, 0); setLocation(0, 0);
@ -67,41 +66,47 @@ public class GameWindow extends JFrame {
switch (e.getKeyCode()) { switch (e.getKeyCode()) {
case KeyEvent.VK_W: case KeyEvent.VK_W:
case KeyEvent.VK_UP: case KeyEvent.VK_UP:
if (!s.getRichtung().equals(Direction.Down)) s.setRichtung(Direction.Up); if (!s.getRichtung().equals(Direction.Down)) s.setDirection(Direction.Up);
break; break;
case KeyEvent.VK_A: case KeyEvent.VK_A:
case KeyEvent.VK_LEFT: case KeyEvent.VK_LEFT:
if (!s.getRichtung().equals(Direction.Right)) s.setRichtung(Direction.Left); if (!s.getRichtung().equals(Direction.Right)) s.setDirection(Direction.Left);
break; break;
case KeyEvent.VK_S: case KeyEvent.VK_S:
case KeyEvent.VK_DOWN: case KeyEvent.VK_DOWN:
if (!s.getRichtung().equals(Direction.Up)) s.setRichtung(Direction.Down); if (!s.getRichtung().equals(Direction.Up)) s.setDirection(Direction.Down);
break; break;
case KeyEvent.VK_D: case KeyEvent.VK_D:
case KeyEvent.VK_RIGHT: case KeyEvent.VK_RIGHT:
if (!s.getRichtung().equals(Direction.Left)) s.setRichtung(Direction.Right); if (!s.getRichtung().equals(Direction.Left)) s.setDirection(Direction.Right);
break; break;
} }
} }
}); });
newFood();
timer = new Timer( timer = new Timer(
50, 50,
evt -> { s.nextFrame(); if (System.currentTimeMillis() >= foodFactory.getTimeOfNextFood()) newFood(); repaint(); }); evt -> {
s.nextFrame();
if (System.currentTimeMillis() >= foodFactory.getTimeOfNextFood())
newFood();
repaint();
}
);
timer.start(); timer.start();
setVisible(true); setVisible(true);
} }
/** /**
* Generates new food * Generates new food.
* *
* @since Snake 1.1 * @since Snake 1.1
*/ */
public void newFood() { public void newFood() {
foodFactory.generateFood(); foodFactory.generateFood();
foodFactory.generateFoodLocation(getWidth(), getHeight()); foodFactory.generateFoodLocation(getWidth(), getHeight());
repaint();
} }
/** /**

View File

@ -25,7 +25,7 @@ public class StartScreen extends JFrame {
private JPanel contentPane; private JPanel contentPane;
/** /**
* closes the application. * Closes the application.
*/ */
public static void close() { System.exit(0); } public static void close() { System.exit(0); }
@ -36,14 +36,7 @@ public class StartScreen extends JFrame {
* @since Snake 1.0 * @since Snake 1.0
*/ */
public static void main(String[] args) { public static void main(String[] args) {
EventQueue.invokeLater(() -> { EventQueue.invokeLater(StartScreen::new);
try {
StartScreen frame = new StartScreen();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
});
} }
/** /**
@ -65,7 +58,6 @@ public class StartScreen extends JFrame {
buPlay.setMnemonic(KeyEvent.VK_ENTER); buPlay.setMnemonic(KeyEvent.VK_ENTER);
buPlay.setFont(new Font("Times New Roman", Font.PLAIN, 16)); buPlay.setFont(new Font("Times New Roman", Font.PLAIN, 16));
buPlay.addActionListener(a -> { buPlay.addActionListener(a -> {
Main.startGame(); Main.startGame();
setVisible(false); setVisible(false);
dispose(); dispose();
@ -73,9 +65,8 @@ public class StartScreen extends JFrame {
}); });
contentPane.add(buPlay); contentPane.add(buPlay);
contentPane.setLayout(null); contentPane.setLayout(null);
setVisible(true);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }