Simplified some calculations
This commit is contained in:
parent
ad329447a9
commit
c7035ff2c5
@ -1,6 +1,7 @@
|
||||
package dev.lh;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
import dev.lh.ui.GameWindow;
|
||||
|
||||
@ -56,7 +57,7 @@ public class FoodFactory {
|
||||
|
||||
private long timeOfNextFood;
|
||||
|
||||
Point pFood = null;
|
||||
private Point pFood;
|
||||
|
||||
private Food nextFood = Food.white;
|
||||
|
||||
@ -75,27 +76,8 @@ public class FoodFactory {
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
public Food generateFood() {
|
||||
int nextFoodIs = (int) Math.floor(Math.random() * 5);
|
||||
switch (nextFoodIs) {
|
||||
|
||||
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();
|
||||
}
|
||||
int n = new Random().nextInt(Food.values().length + 1);
|
||||
nextFood = n == Food.values().length ? generateFood() : Food.values()[n];
|
||||
rectangleSize = nextFood.ordinal() + 2;
|
||||
setTimeToNextFoodMillis();
|
||||
return nextFood;
|
||||
@ -107,7 +89,9 @@ public class FoodFactory {
|
||||
*
|
||||
* @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
|
||||
@ -134,11 +118,9 @@ public class FoodFactory {
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
public Point generateFoodLocation(int width, int height) {
|
||||
pFood = new Point((int) Math.round(Math.random() * width), (int) Math.round(Math.random() * height));
|
||||
if (pFood.x < 50 || pFood.x > width - 50 || pFood.y < 50 || pFood.y > height - 50) {
|
||||
pFood.x = (pFood.x < 50) ? 50 : (pFood.x > width - 50) ? width - 50 : pFood.x;
|
||||
pFood.y = (pFood.y < 50) ? 50 : (pFood.y > height - 50) ? height - 50 : pFood.y;
|
||||
}
|
||||
assert (width > 100 && height > 100);
|
||||
Random r = new Random();
|
||||
pFood = new Point(r.nextInt(width - 100) + 50, r.nextInt(height - 100) + 50);
|
||||
return pFood;
|
||||
}
|
||||
|
||||
@ -178,7 +160,7 @@ public class FoodFactory {
|
||||
case blue:
|
||||
g.setColor(Color.blue);
|
||||
break;
|
||||
}// switch
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,9 +1,6 @@
|
||||
package dev.lh;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Point;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -54,7 +51,7 @@ public class Snake implements Updateable {
|
||||
|
||||
private static FoodFactory foodFactory = FoodFactory.getInstance();
|
||||
private static Endscreen endscreen;
|
||||
private Direction Richtung;
|
||||
private Direction direction;
|
||||
private int length;
|
||||
private List<Point> tiles = new ArrayList<>();
|
||||
private final int snakeSize = 10;
|
||||
@ -67,7 +64,7 @@ public class Snake implements Updateable {
|
||||
*/
|
||||
public Snake(int length) {
|
||||
this.length = length;
|
||||
Richtung = Direction.Right;
|
||||
direction = Direction.Right;
|
||||
// adding the initial tiles of the snake
|
||||
for (int i = 0; i < length; i++)
|
||||
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
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
public Direction getRichtung() { return Richtung; }
|
||||
public Direction getRichtung() { return direction; }
|
||||
|
||||
@Override
|
||||
public void nextFrame() {
|
||||
int velX = 0, velY = 0;
|
||||
switch (Richtung) {
|
||||
switch (direction) {
|
||||
case Up:
|
||||
velY = -snakeSize;
|
||||
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
|
||||
*/
|
||||
public void setRichtung(Direction richtung) { Richtung = richtung; }
|
||||
public void setDirection(Direction direction) { this.direction = direction; }
|
||||
}
|
||||
|
@ -54,7 +54,6 @@ public class Endscreen extends JDialog {
|
||||
Image resultImage = Toolkit.getDefaultToolkit()
|
||||
.getImage(this.getClass().getResource((score < goodOrBadResult) ? "/Try_Again.jpg" : "/1211548-200.png"));
|
||||
resultImage.flush();
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -33,7 +33,6 @@ public class GameWindow extends JFrame {
|
||||
*/
|
||||
public GameWindow(String title) {
|
||||
super(title);
|
||||
newFood();
|
||||
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
|
||||
setBounds(new Rectangle(size));
|
||||
setLocation(0, 0);
|
||||
@ -67,41 +66,47 @@ public class GameWindow extends JFrame {
|
||||
switch (e.getKeyCode()) {
|
||||
case KeyEvent.VK_W:
|
||||
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;
|
||||
case KeyEvent.VK_A:
|
||||
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;
|
||||
case KeyEvent.VK_S:
|
||||
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;
|
||||
case KeyEvent.VK_D:
|
||||
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;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
newFood();
|
||||
timer = new Timer(
|
||||
50,
|
||||
evt -> { s.nextFrame(); if (System.currentTimeMillis() >= foodFactory.getTimeOfNextFood()) newFood(); repaint(); });
|
||||
evt -> {
|
||||
s.nextFrame();
|
||||
if (System.currentTimeMillis() >= foodFactory.getTimeOfNextFood())
|
||||
newFood();
|
||||
repaint();
|
||||
}
|
||||
);
|
||||
timer.start();
|
||||
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates new food
|
||||
* Generates new food.
|
||||
*
|
||||
* @since Snake 1.1
|
||||
*/
|
||||
public void newFood() {
|
||||
foodFactory.generateFood();
|
||||
foodFactory.generateFoodLocation(getWidth(), getHeight());
|
||||
repaint();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -25,7 +25,7 @@ public class StartScreen extends JFrame {
|
||||
private JPanel contentPane;
|
||||
|
||||
/**
|
||||
* closes the application.
|
||||
* Closes the application.
|
||||
*/
|
||||
public static void close() { System.exit(0); }
|
||||
|
||||
@ -36,14 +36,7 @@ public class StartScreen extends JFrame {
|
||||
* @since Snake 1.0
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
EventQueue.invokeLater(() -> {
|
||||
try {
|
||||
StartScreen frame = new StartScreen();
|
||||
frame.setVisible(true);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
EventQueue.invokeLater(StartScreen::new);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -65,7 +58,6 @@ public class StartScreen extends JFrame {
|
||||
buPlay.setMnemonic(KeyEvent.VK_ENTER);
|
||||
buPlay.setFont(new Font("Times New Roman", Font.PLAIN, 16));
|
||||
buPlay.addActionListener(a -> {
|
||||
|
||||
Main.startGame();
|
||||
setVisible(false);
|
||||
dispose();
|
||||
@ -73,9 +65,8 @@ public class StartScreen extends JFrame {
|
||||
});
|
||||
contentPane.add(buPlay);
|
||||
contentPane.setLayout(null);
|
||||
|
||||
setVisible(true);
|
||||
} catch (Exception e) {
|
||||
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user