Skip to content

Instantly share code, notes, and snippets.

@ziplokk1
Created February 11, 2014 01:04
Show Gist options
  • Save ziplokk1/8927461 to your computer and use it in GitHub Desktop.
Save ziplokk1/8927461 to your computer and use it in GitHub Desktop.
package main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Random;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Board extends JPanel {
public static final int BOARD_WIDTH = 30;
public static final int BOARD_HEIGHT = 20;
Tile.Type[][] tiles = new Tile.Type[BOARD_WIDTH][BOARD_HEIGHT];
private Snake snake;
private Point fruitLoc;
public Board(Snake snake) {
this.setDoubleBuffered(true);
this.snake = snake;
for(int x = 0; x < tiles.length; x++) { //Initialize all tiles to be null and empty
for(int y = 0; y < tiles[x].length; y++) {
tiles[x][y] = null;
}
}
for(int i = 0; i < snake.snakeBody.size(); i++) { //Set tiles that contain the snake to Tile.Type.BODY
Point bodyPoint = snake.snakeBody.get(i);
tiles[bodyPoint.x][bodyPoint.y] = Tile.Type.BODY;
}
generateFruit();
setPreferredSize(new Dimension(BOARD_WIDTH * Tile.TILE_SIZE, BOARD_HEIGHT * Tile.TILE_SIZE));
setBackground(Color.BLACK);
}
public void generateFruit() {
Random r = new Random();
int fruitLocX = r.nextInt(BOARD_WIDTH);
int fruitLocY = r.nextInt(BOARD_HEIGHT);
tiles[fruitLocX][fruitLocY] = Tile.Type.FRUIT;
fruitLoc = new Point(fruitLocX, fruitLocY);
}
/*
* @Params
* x = x coordinate of tile
* y = y coordinate of tile
* t = Tile.Type
*/
public void setTile(int x, int y, Tile.Type t) {
tiles[x][y] = t;
}
public void setTile(Point p, Tile.Type t) {
tiles[p.x][p.y] = t;
}
/*
* @Params
* x = x coordinate of tile
* y = y coordinate of tile
*/
public Tile.Type getTileAt(int x, int y) {
return tiles[x][y];
}
public Tile.Type getTileAt(Point p) {
return tiles[p.x][p.y];
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
//Paint the grid on the background
g.setColor(Color.LIGHT_GRAY);
for(int x = 0; x < tiles.length; x++) {
for(int y = 0; y < tiles[x].length; y++) {
g.drawLine(0, y * Tile.TILE_SIZE, BOARD_WIDTH * Tile.TILE_SIZE, y * Tile.TILE_SIZE);
}
g.drawLine(x * Tile.TILE_SIZE, 0, x * Tile.TILE_SIZE, BOARD_HEIGHT * Tile.TILE_SIZE);
}
//Paint the body of the snake on the grid
for(int x = 0; x < tiles.length; x++) {
for(int y = 0; y < tiles[x].length; y++) {
Point currentLoc = new Point(x, y);
if(snake.snakeBody.contains(currentLoc)) {
drawSquare(currentLoc, g);
}
}
}
drawSquare(fruitLoc, g);
}
/*
* Draws a square at the given point based the Tile.Type at point p
*/
public void drawSquare(Point p, Graphics g) {
Tile.Type currentTile = getTileAt(p);
if(currentTile == Tile.Type.BODY) {
g.setColor(Color.GREEN);
g.fillRect(p.x * Tile.TILE_SIZE + 1, p.y * Tile.TILE_SIZE + 1, Tile.TILE_SIZE - 1, Tile.TILE_SIZE - 1);
} else if(currentTile == Tile.Type.FRUIT) {
g.setColor(Color.BLACK);
g.fillRect(p.x * Tile.TILE_SIZE + 1, p.y * Tile.TILE_SIZE + 1, Tile.TILE_SIZE - 1, Tile.TILE_SIZE - 1);
g.setColor(Color.RED);
g.fillOval(p.x * Tile.TILE_SIZE + 5, p.y * Tile.TILE_SIZE + 5, Tile.TILE_SIZE - 10, Tile.TILE_SIZE - 10);
} else if(currentTile == Tile.Type.BLOCK) {
g.setColor(Color.LIGHT_GRAY);
g.fillRect(p.x * Tile.TILE_SIZE + 1, p.y * Tile.TILE_SIZE + 1, Tile.TILE_SIZE - 1, Tile.TILE_SIZE - 1);
} else if(currentTile == null || currentTile.equals(null)) {
g.setColor(Color.BLACK);
}
}
/*
* Updates the graphics when the snake is moved
*/
public void onUpdate(Graphics g) {
for(int i = 0; i < snake.snakeBody.size(); i++) {
setTile(snake.snakeBody.get(i), Tile.Type.BODY);
drawSquare(snake.snakeBody.get(i), g);
}
this.update(g);
}
}
package main;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class Main extends DirectionListener {
private Snake snake;
private Board board;
private JFrame frame;
public Main() {
initialize();
}
public void initialize() {
snake = new Snake();
board = new Board(snake);
frame = new JFrame("Snake Game");
frame.add(board);
frame.addKeyListener(this);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new Main();
}
@Override
void directionLeft() {
snake.moveLeft();
board.onUpdate(board.getGraphics());
}
@Override
void directionRight() {
snake.moveRight();
board.onUpdate(board.getGraphics());
}
@Override
void directionUp() {
snake.moveUp();
board.onUpdate(board.getGraphics());
}
@Override
void directionDown() {
snake.moveDown();
board.onUpdate(board.getGraphics());
}
}
abstract class DirectionListener implements KeyListener {
@Override
public void keyPressed(KeyEvent arg0) {
switch(arg0.getKeyCode()) {
case KeyEvent.VK_W:
case KeyEvent.VK_UP:
directionUp();
break;
case KeyEvent.VK_A:
case KeyEvent.VK_LEFT:
directionLeft();
break;
case KeyEvent.VK_S:
case KeyEvent.VK_DOWN:
directionDown();
break;
case KeyEvent.VK_D:
case KeyEvent.VK_RIGHT:
directionRight();
break;
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
abstract void directionLeft();
abstract void directionRight();
abstract void directionUp();
abstract void directionDown();
}
package main;
import java.awt.Point;
import java.util.LinkedList;
class Snake {
public static enum Direction {
UP, DOWN, LEFT, RIGHT;
}
/*
* An array containing points of the initial body.
* It's easier to loop through the array and add them to the snake
* instead of manually adding each one
*/
private final Point[] snakeBodyInit = {new Point(14, 9), new Point(15, 9),
new Point(16, 9), new Point(17, 9), new Point(18, 9)};
LinkedList<Point> snakeBody = new LinkedList<Point>();
/*
* Direction the snake is currently heading
*/
private Direction heading = Direction.LEFT;
public Snake() {
for(int i = 0; i < snakeBodyInit.length; i++) { //Adds all the body parts of the snake to the linked list
snakeBody.add(snakeBodyInit[i]);
}
}
/*
* Used to push the elements of the list down the list.
* Adds param to head of list and removes the tail
*/
private void pushList(Point p) {
snakeBody.addFirst(p);
snakeBody.removeLast();
}
public void moveDown() {
if(heading != Direction.UP) {
pushList(new Point(snakeBody.getFirst().x, snakeBody.getFirst().y + 1));
heading = Direction.DOWN;
}
System.out.println(snakeBody);
}
public void moveRight() {
if(heading != Direction.LEFT) { //Make sure the snake cant turn back on itself
pushList(new Point(snakeBody.getFirst().x + 1, snakeBody.getFirst().y));
heading = Direction.RIGHT;
}
System.out.println(snakeBody);
}
public void moveUp() {
if(heading != Direction.DOWN) {
pushList(new Point(snakeBody.getFirst().x, snakeBody.getFirst().y - 1));
heading = Direction.UP;
}
System.out.println(snakeBody);
}
public void moveLeft() {
if(heading != Direction.RIGHT) {
pushList(new Point(snakeBody.getFirst().x - 1, snakeBody.getFirst().y));
heading = Direction.LEFT;
}
System.out.println(snakeBody);
}
}
package main;
public class Tile {
public static final int TILE_SIZE = 20;
public static enum Type {
BODY, FRUIT, BLOCK;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment