Created
April 22, 2018 20:49
-
-
Save wldomiciano/92fd8ac6939b7dc6711be2ac8bd1b8ca to your computer and use it in GitHub Desktop.
A simple Snake clone implementation in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import javax.swing.*; | |
import java.awt.event.*; | |
class Game extends JPanel implements ActionListener { | |
final int TIMEOUT = 125; | |
final int SIZE = 25; | |
final int BOARD_COLS = 10; | |
final int BOARD_ROWS = 10; | |
final int BOARD_SIZE = (BOARD_COLS * BOARD_ROWS); | |
int head = 0; | |
int apple = 45; | |
int direction = 1; | |
int[] tail = new int[BOARD_SIZE]; | |
int length = 0; | |
int i = 0; | |
Timer timer = new Timer(TIMEOUT, this); | |
Graphics ctx = null; | |
public static void main(String[] args) { | |
JFrame window = new JFrame(); | |
window.add(new Game()); | |
window.setResizable(false); | |
window.pack(); | |
window.setTitle("Snake"); | |
window.setLocationRelativeTo(null); | |
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
window.setVisible(true); | |
} | |
public Game() { | |
addKeyListener(new KeyAdapter() { | |
@Override public void keyPressed(KeyEvent e) { | |
if (e.getKeyCode() == KeyEvent.VK_UP) setDirection(-BOARD_COLS); | |
else if (e.getKeyCode() == KeyEvent.VK_DOWN) setDirection( BOARD_COLS); | |
else if (e.getKeyCode() == KeyEvent.VK_LEFT) setDirection(-1); | |
else if (e.getKeyCode() == KeyEvent.VK_RIGHT) setDirection( 1); | |
} | |
}); | |
setBackground(Color.black); | |
setFocusable(true); | |
setDoubleBuffered(true); | |
setPreferredSize(new Dimension(BOARD_COLS * SIZE, BOARD_ROWS * SIZE)); | |
timer.start(); | |
} | |
@Override | |
public void paintComponent(Graphics g) { | |
super.paintComponent(g); | |
ctx = g; | |
update(); | |
drawBoard(); | |
} | |
@Override | |
public void actionPerformed(ActionEvent e) { repaint(); } | |
void draw(int position, Color color) { | |
int x = position % BOARD_COLS; | |
int y = position / BOARD_COLS; | |
ctx.setColor(color); | |
ctx.fillRect( x * SIZE, y * SIZE, SIZE, SIZE ); | |
} | |
int random() { | |
return (int) (Math.random() * BOARD_SIZE); | |
} | |
boolean isOnLimits() { | |
return (direction == 1 && head % BOARD_COLS == 0) || | |
(direction == -1 && (head + 1) % BOARD_COLS == 0); | |
} | |
boolean hasCollisionWithTail(int position) { | |
for (int i = 0; i < length; i++) | |
if (tail[i] == position) return true; | |
return false; | |
} | |
void placeApple() { | |
do apple = random(); | |
while (head == apple || hasCollisionWithTail(apple)); | |
} | |
void setDirection(int dir) { | |
if (dir != -direction || length == 0) | |
direction = dir; | |
} | |
void drawBoard() { | |
for (int i = 0; i < length; ++i) | |
draw(tail[i], Color.GREEN); | |
draw(head, Color.BLUE); | |
draw(apple, Color.RED); | |
} | |
void move() { | |
tail[i] = head; | |
head += direction; | |
if (++i >= length) i = 0; | |
if (isOnLimits()) | |
head -= BOARD_COLS * direction; | |
else if (head < 0) | |
head += BOARD_SIZE; | |
else if (head >= BOARD_SIZE) | |
head -= BOARD_SIZE; | |
} | |
void update() { | |
move(); | |
if (head == apple) { | |
placeApple(); | |
tail[length++] = head; | |
} else if (hasCollisionWithTail(head)) timer.stop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment