This commit is contained in:
2022-11-17 16:20:15 +01:00
parent ca5a8516fd
commit 43627048d8
31 changed files with 21896 additions and 19 deletions

View File

@@ -0,0 +1,32 @@
import javax.swing.JPanel;
import java.awt.*;
public class Cell extends JPanel {
private CellType type;
private static final int offset = 2;
private static final int arc = 5;
public Cell() {
super();
setOpaque(false);
type = CellType.EMPTY;
}
public CellType getType() {
return type;
}
public void setType(CellType type) {
this.type = type;
}
protected void paintComponent(Graphics g) {
Graphics ng = g.create();
((Graphics2D)ng).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
ng.setColor(type.getColor());
ng.fillRoundRect(offset, offset, getWidth() - offset * 2, getHeight() - offset * 2, arc, arc);
}
}

View File

@@ -0,0 +1,18 @@
import java.awt.Color;
public enum CellType {
SNAKE(Color.ORANGE),
EMPTY(Color.GREEN),
FRUIT(Color.RED),
VOID(Color.BLACK);
private Color c;
private CellType(Color c) {
this.c = c;
}
public Color getColor() {
return c;
}
}

View File

@@ -0,0 +1,33 @@
import java.util.Timer;
import java.util.TimerTask;
public class GameOverTask extends TimerTask {
private Timer parent;
private SnakePanel game;
private int i;
public GameOverTask(SnakePanel game, Timer parent) {
this.game = game;
this.parent = parent;
i = 0;
}
@Override
public void run() {
for (int x = i; x >= 0; x--) {
int y = i - x;
if (x < game.sizeX && y < game.sizeY) {
game.getGrid()[x][y].setType(CellType.VOID);
if (x == game.sizeX - 1 && y == game.sizeY - 1) {
parent.cancel();
break;
}
}
}
i++;
game.repaint();
}
}

View File

@@ -0,0 +1,15 @@
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
JFrame masterFrame = new JFrame("Snake");
masterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
masterFrame.setLocation(100, 100);
masterFrame.setSize(500, 500);
masterFrame.add(new SnakePanel());
masterFrame.setVisible(true);
}
}

View File

@@ -0,0 +1,79 @@
import java.awt.event.KeyEvent;
import java.util.LinkedList;
public class Snake {
private int headX;
private int headY;
private Cell[][] grid;
LinkedList<Cell> snakeCells;
private int length;
private SnakePanel parent;
private int direction = KeyEvent.VK_RIGHT;
public Snake(SnakePanel parent) {
this.parent = parent;
this.grid = parent.getGrid();
length = 3;
headX = parent.sizeX / 2;
headY = parent.sizeY / 2;
snakeCells = new LinkedList<Cell>();
grid[headY][headX].setType(CellType.SNAKE);
snakeCells.addFirst(grid[headX][headY]);
}
public int getDirection() {
return direction;
}
public void setDirection(int direction) {
this.direction = direction;
}
public void performMove() {
switch (direction) {
case (KeyEvent.VK_RIGHT):
headX++;
break;
case (KeyEvent.VK_LEFT):
headX--;
break;
case (KeyEvent.VK_UP):
headY--;
break;
case (KeyEvent.VK_DOWN):
headY++;
break;
}
if (headY >= parent.sizeY || headX >= parent.sizeX) {
parent.gameOver();
return;
}
Cell newCell = grid[headY][headX];
if (newCell == null || newCell.getType() == CellType.SNAKE) {
parent.gameOver();
return;
}
snakeCells.addFirst(newCell);
newCell.setType(CellType.SNAKE);
newCell.repaint();
if (snakeCells.size() > length) {
Cell c = snakeCells.removeLast();
c.setType(CellType.EMPTY);
c.repaint();
}
}
}

View File

@@ -0,0 +1,49 @@
import java.awt.event.*;
public class SnakeListener implements KeyListener {
private SnakePanel parent;
public SnakeListener(SnakePanel parent) {
this.parent = parent;
}
@Override
public void keyPressed(KeyEvent e) {
Snake s = parent.getSnake();
switch (e.getKeyCode()) {
case (KeyEvent.VK_LEFT):
s.setDirection(KeyEvent.VK_LEFT);
break;
case (KeyEvent.VK_RIGHT):
s.setDirection(KeyEvent.VK_RIGHT);
break;
case (KeyEvent.VK_UP):
s.setDirection(KeyEvent.VK_UP);
break;
case (KeyEvent.VK_DOWN):
s.setDirection(KeyEvent.VK_DOWN);
break;
default:
return;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}

View File

@@ -0,0 +1,58 @@
import javax.swing.JPanel;
import java.awt.*;
import java.util.Timer;
import java.util.TimerTask;
public class SnakePanel extends JPanel {
private Cell[][] grid;
private Timer moveTimer;
private Snake snake;
public final int sizeX = 25;
public final int sizeY = 25;
public SnakePanel() {
super();
grid = new Cell[sizeX][sizeY];
setLayout(new GridLayout(sizeY, sizeX));
for (int x = 0; x < sizeX; x++) {
for (int y = 0; y < sizeY; y++) {
grid[x][y] = new Cell();
add(grid[x][y]);
}
}
int i = 0;
this.snake = new Snake(this);
addKeyListener(new SnakeListener(this));
moveTimer = new Timer();
moveTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
requestFocus();
snake.performMove();
}
}, 200, 200);
}
public void gameOver() {
moveTimer.cancel();
Timer gameOverTimer = new Timer();
gameOverTimer.scheduleAtFixedRate(new GameOverTask(this, gameOverTimer), 0, 60);
}
public Cell[][] getGrid() {
return grid;
}
public Snake getSnake() {
return snake;
}
}