Skip to content
Snippets Groups Projects
Commit b1f541b8 authored by BAUER Oscar's avatar BAUER Oscar
Browse files

version2

parent d2e4de1c
No related branches found
No related tags found
No related merge requests found
package version2;
public enum Direction {
NORTH, SOUTH, EAST, WEST
}
package version2;
public class MainFrame {
public static void main(String[] args) {
new SnakeGameFrame().setVisible(true);
}
}
package version2;
public class Position {
public final int line;
public final int column;
public Position(int line, int column) {
this.line = line;
this.column = column;
}
@Override
public String toString() {
return "(" + column + "," + line + ")";
}
}
package version2;
import java.util.List;
public class SnakeGame {
public static final int GRID_LINES = 12;
public static final int GRID_COLUMNS = 12;
private List<Position> segments;
private Direction direction;
private boolean gameOver;
private Position apple;
public SnakeGame() {
initialize();
}
public void initialize() {
}
public void advance() {
}
public boolean isGameOver() {
return gameOver;
}
public void changeDirection(Direction direction) {
}
public boolean isSnakeAt(Position position) {
return false;
}
public boolean isAppleAt(Position position) {
return false;
}
}
package version2;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
@SuppressWarnings("serial")
public class SnakeGameFrame extends JFrame {
public SnakeGameFrame() {
setTitle("Snake");
setSize(new Dimension(400, 400));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
add(new SnakeGamePanel());
}
}
package version2;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class SnakeGamePanel extends JPanel {
private final SnakeGame snakeGame = new SnakeGame();
private final Timer timer = new Timer();
private static final int MIN_BORDER_SIZE = 40;
public SnakeGamePanel() {
initializeKeyListener();
initializeTimer();
}
@Override
public void paint(Graphics g) {
for (int line = 0; line < SnakeGame.GRID_LINES; line++)
for (int column = 0; column < SnakeGame.GRID_COLUMNS; column++)
paintPosition(g, line, column);
}
private void paintPosition(Graphics g, int line, int column) {
int x = getX(column);
int y = getY(line);
g.setColor(getColorAt(line, column));
g.fillRect(x, y, getCellSize(), getCellSize());
g.setColor(Color.LIGHT_GRAY);
g.drawRect(x, y, getCellSize(), getCellSize());
}
private Color getColorAt(int line, int column) {
if (snakeGame.isSnakeAt(new Position(line, column)))
return snakeGame.isGameOver() ? Color.MAGENTA : Color.GREEN;
return snakeGame.isAppleAt(new Position(line, column)) ? Color.RED : Color.BLUE;
}
public int getCellSize() {
int cellMaximumHeight = (getHeight() - (2 * MIN_BORDER_SIZE)) / SnakeGame.GRID_LINES;
int cellMaximumWidth = (getWidth() - (2 * MIN_BORDER_SIZE)) / SnakeGame.GRID_COLUMNS;
return Math.min(cellMaximumWidth, cellMaximumHeight);
}
private int getXBorder() {
return (getWidth() - (SnakeGame.GRID_COLUMNS * getCellSize())) / 2;
}
private int getYBorder() {
return (getHeight() - (SnakeGame.GRID_LINES * getCellSize())) / 2;
}
private int getX(int column) {
return getXBorder() + column * getCellSize();
}
private int getY(int line) {
return getYBorder() + line * getCellSize();
}
private void initializeKeyListener() {
setFocusable(true);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP)
snakeGame.changeDirection(Direction.SOUTH);
else if (e.getKeyCode() == KeyEvent.VK_DOWN)
snakeGame.changeDirection(Direction.NORTH);
else if (e.getKeyCode() == KeyEvent.VK_LEFT)
snakeGame.changeDirection(Direction.WEST);
else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
snakeGame.changeDirection(Direction.EAST);
else if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
snakeGame.initialize();
}
});
}
private void initializeTimer() {
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
snakeGame.advance();
repaint();
}
}, 1000, 200);
}
}
\ No newline at end of file
package version2;
public class TestDirection {
public static void main(String[] args) {
Direction direction1 = null;
System.out.println("Direction initiale : " + direction1);
direction1 = Direction.NORTH;
System.out.println("Nouvelle direction : " + direction1);
System.out.println(direction1 == Direction.NORTH);
System.out.println(direction1 == Direction.EAST);
}
}
package version2;
public class TestPosition {
public static void main(String[] args) {
Position pos1 = new Position(3, 4);
System.out.println(pos1);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment