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

version3

parent b1f541b8
No related branches found
No related tags found
No related merge requests found
......@@ -9,21 +9,29 @@ public class SnakeGame {
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 version3;
public enum Direction {
NORTH, SOUTH, EAST, WEST
}
package version3;
public class MainFrame {
public static void main(String[] args) {
new SnakeGameFrame().setVisible(true);
}
}
package version3;
public class Position {
public final int line;
public final int column;
public Position(int line, int column) {
this.line = line;
this.column = column;
}
public boolean isSamePosition(Position pos) {
if (isLine(pos.line) && isColumn(pos.column))
return true;
return false;
}
public boolean equals(Object obj) {
if (obj.getClass() == this.getClass())
return isSamePosition((Position) obj);
return false;
}
public boolean isColumn(int col) {
return col == column;
}
public boolean isLine(int line) {
return line == this.line;
}
public Position getNext(Direction direction) {
if (direction == Direction.NORTH)
return new Position(line + 1, column);
if (direction == Direction.EAST)
return new Position(line, column + 1);
if (direction == Direction.SOUTH)
return new Position(line - 1, column);
return new Position(line, column - 1);
}
@Override
public String toString() {
return "(" + column + "," + line + ")";
}
}
package version3;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
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() {
direction = Direction.NORTH;
int startL = GRID_LINES / 2;
int startC = GRID_COLUMNS / 2;
gameOver = false;
setApple();
segments = new ArrayList<Position>();
segments.add(new Position(startL, startC));
segments.add(new Position(startL + 1, startC));
segments.add(new Position(startL + 2, startC));
}
public void setApple() {
Random rand = new Random();
apple = new Position(rand.nextInt(GRID_LINES + 1), rand.nextInt(GRID_LINES + 1));
}
public void advance() {
}
public boolean isGameOver() {
return gameOver;
}
public void changeDirection(Direction direction) {
}
public boolean isSnakeAt(Position position) {
for (Position pos : segments)
if (position.isSamePosition(pos))
return true;
return false;
}
public boolean isAppleAt(Position position) {
return position.isSamePosition(apple);
}
public void print() {
for (int L =0; L < GRID_LINES; L++) {
for (int c =0; c<GRID_COLUMNS;c++) {
Position tmp = new Position(L, c);
if(isAppleAt(tmp))
System.out.print(" A ");
else if(isSnakeAt(tmp))
System.out.print(" S ");
else
System.out.print(" _ ");
}
System.out.println();
}
}
// public void printLine() {
// String line = "";
// for (int c =0; c<GRID_COLUMNS;c++) {
// if(apple.isColumn(c) && apple.isLine(c))
// }
//
// }
}
package version3;
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 version3;
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 version3;
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 version3;
public class TestInitialize {
public static void main(String[] args) {
SnakeGame game = new SnakeGame();
game.print();
}
}
package version3;
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