package com.litao;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
import java.util.Timer;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class WormStage extends JPanel
{
public static final int CELL_SIZE = 10;
public static final int CELL_COLS = 35;
public static final int CELL_ROWS = 35;
public static final int CELL_LEN = 12;
public static final Color FOOD = new Color(255, 192, 0);
public static final Color WORM = new Color(0, 176, 240);
public static final Color BACKGROUND = new Color(35, 31, 32);
private Random random = new Random();
private Timer timer = new Timer();
private Worm worm;
private Cell cell;
public WormStage()
{
setSize(CELL_COLS * CELL_SIZE, CELL_ROWS * CELL_SIZE);
}
public void action()
{
worm = new Worm();
cell = randomFood();
timer.schedule(new CreepTask(this), 0, 1000);
requestFocus();
addKeyListener(new KeyAdapter()
{
@Override
public void keyPressed(KeyEvent e)
{
switch (e.getKeyCode())
{
case KeyEvent.VK_DOWN:
worm.changeDirection(Worm.DOWM);
createFood();
break;
case KeyEvent.VK_UP:
worm.changeDirection(Worm.UP);
createFood();
break;
case KeyEvent.VK_LEFT:
worm.changeDirection(Worm.LEFT);
createFood();
break;
case KeyEvent.VK_RIGHT:
worm.changeDirection(Worm.RIGHT);
createFood();
break;
}
}
});
}
public void createFood()
{
if (worm.hit())
{
worm = new Worm();
cell = randomFood();
}
else
{
boolean eat = worm.creep(cell);
if (eat)
{
cell = randomFood();
}
}
repaint();
}
private Cell randomFood()
{
int x;
int y;
do
{
x = random.nextInt(CELL_COLS);
y = random.nextInt(CELL_ROWS);
} while (worm.contains(x, y));
return new Cell(x, y);
}
@Override
public void paint(Graphics g)
{
g.setColor(BACKGROUND);
g.fillRect(0, 0, getWidth(), getHeight());
if (worm != null)
{
worm.paint(g, WORM, BACKGROUND);
}
if (cell != null)
{
cell.paint(g, FOOD, BACKGROUND);
}
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(460, 470);
frame.setTitle("贪吃蛇<李涛版>");
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((dimension.width - frame.getWidth()) / 2,
(dimension.height - frame.getHeight()) / 2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLayout(null);
WormStage wormStage = new WormStage();
frame.setVisible(true);
int w = frame.getContentPane().getWidth();
int h = frame.getContentPane().getHeight();
wormStage.setLocation((w - wormStage.getWidth()) / 2, (h - wormStage
.getHeight()) / 2);
frame.getContentPane().add(wormStage);
wormStage.action();
}
}
package com.litao;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Arrays;
public class Worm
{
public static final int UP = 1;
public static final int DOWM = -1;
public static final int LEFT = 2;
public static final int RIGHT = -2;
Cell[] cells;
int currentDirection = DOWM;
public Worm()
{
cells = new Cell[WormStage.CELL_LEN];
for (int i = 0; i < cells.length; i++)
{
cells[i] = new Cell(i, 0);
}
}
public boolean hit()
{
return hit(currentDirection);
}
public boolean hit(int direction)
{
Cell nextHead = newHead(direction);
if (nextHead.getX() < 0 || nextHead.getY() < 0
|| nextHead.getX() >= WormStage.CELL_COLS
|| nextHead.getY() >= WormStage.CELL_ROWS)
{
return true;
}
if (this.contains(nextHead.getX(), nextHead.getY()))
{
return true;
}
return false;
}
public boolean creep(Cell food)
{
return creep(currentDirection,food);
}
public boolean creep(int direction, Cell food)
{
boolean eat = false;
if (direction + currentDirection != 0)
{
currentDirection = direction;
Cell newHead = newHead(direction);
if (newHead.getX() == food.getX() && newHead.getY() == food.getY())
{
cells = Arrays.copyOf(cells, cells.length + 1);
eat = true;
}
if (newHead != null)
{
for (int i = cells.length - 1; i >= 1; i--)
{
cells[i] = cells[i - 1];
}
cells[0] = newHead;
}
}
return eat;
}
public boolean changeDirection(int direction)
{
if (direction + currentDirection != 0)
{
currentDirection = direction;
return true;
}
return false;
}
public boolean contains(int x, int y)
{
for (int i = 0; i < cells.length; i++)
{
if (x == cells[i].getX() && y == cells[i].getY())
{
return true;
}
}
return false;
}
public void paint(Graphics g, Color a, Color b)
{
for (int i = 0; i < cells.length; i++)
{
cells[i].paint(g, a, b);
}
}
public Cell newHead(int direction)
{
Cell newHead = null;
Cell head = cells[0];
switch (direction)
{
case UP:
newHead = new Cell(head.getX(), head.getY() - 1);
break;
case DOWM:
newHead = new Cell(head.getX(), head.getY() + 1);
break;
case LEFT:
newHead = new Cell(head.getX() - 1, head.getY());
break;
case RIGHT:
newHead = new Cell(head.getX() + 1, head.getY());
break;
}
return newHead;
}
}
package com.litao;
import java.awt.Color;
import java.awt.Graphics;
public class Cell
{
private int x;
private int y;
public Cell()
{
}
public Cell(int x, int y)
{
this.x = x;
this.y = y;
}
public Cell(Cell cell)
{
this(cell.x, cell.y);
}
public int getX()
{
return x;
}
public void setX(int x)
{
this.x = x;
}
public int getY()
{
return y;
}
public void setY(int y)
{
this.y = y;
}
public void paint(Graphics g, Color a, Color b)
{
g.setColor(a);
g.fillRect(x * WormStage.CELL_SIZE, y * WormStage.CELL_SIZE,
WormStage.CELL_SIZE, WormStage.CELL_SIZE);
g.setColor(b);
g.drawRect(x * WormStage.CELL_SIZE, y * WormStage.CELL_SIZE,
WormStage.CELL_SIZE, WormStage.CELL_SIZE);
}
}
package com.litao;
import java.util.TimerTask;
public class CreepTask extends TimerTask
{
private WormStage wormStage;
public CreepTask(WormStage wormStage)
{
this.wormStage = wormStage;
}
@Override
public void run()
{
wormStage.createFood();
}
}