62,614
社区成员
发帖
与我相关
我的任务
分享
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.LinkedList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SnakeTest {
public static void main(String[] args) {
start();
}
public static void start() {
Snake snake = new Snake();
Food food = new Food();
GamePanel gamePanel = new GamePanel(snake, food);
MyControl control = new MyControl(snake, food, gamePanel);
gamePanel.setFocusable(true);
snake.addTouchListener(control);
gamePanel.addKeyListener(control);
JFrame gameFrame = new JFrame();
gameFrame.setSize(300, 300);
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.setResizable(false);
gameFrame.setLocationRelativeTo(null);
gameFrame.add(gamePanel);
control.startGame();
gameFrame.setVisible(true);
}
}
/**
* 食物类
* @author Administrator
*
*/
class Food {
private Point p;
private int x;
private int y;
public Food(){
x = new Random().nextInt(MyGrid.GRID_X -1);
y = new Random().nextInt(MyGrid.GRID_Y - 1);
p = new Point(x, y);
}
/**
* 显示食物
*/
public void showMe(Graphics g){
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.red);
g2.fillRect(x * MyGrid.WHITH, y * MyGrid.HEIGH, MyGrid.WHITH, MyGrid.HEIGH);
}
public void creatNewFood(){
x = new Random().nextInt(MyGrid.GRID_X);
y = new Random().nextInt(MyGrid.GRID_Y);
p = new Point(x, y);
}
/**
* 食物被蛇碰到
* @param snake
*/
public boolean isEatBySnake(Snake snake){
LinkedList<Point> snakeBodies = snake.getSnakeBody();
for (Point point : snakeBodies) {
if(point.equals(this.p)){
creatNewFood();
return true;
}
}
return false;
}
}
/**
* 蛇类
* @author Administrator
*
*/
class Snake {
private LinkedList<Point> snakeBody;
private TouchListener listener;
private int x;
private int y;
public static final int UPPER = -1;
public static final int DOWN = 1;
public static final int LEFT = -3;
public static final int RIGHT = 3;
private Point head;
private Point tail;
private int direction;
private int oldDirection;
public Snake() {
init();
}
public void init(){
snakeBody = new LinkedList<>();
direction = 0;
oldDirection = 0;
x = 2;
y = 2;
for (int i = 0; i < 3; i++) {
Point p = new Point(x++, y);
snakeBody.add(p);
direction = RIGHT;
}
}
private Point newHead(int x, int y) {
return new Point(x, y);
}
public LinkedList<Point> getSnakeBody() {
return snakeBody;
}
/**
* 添加监听器,判断蛇是否碰到食物,障碍物,自己
*
* @param listener
*/
public void addTouchListener(TouchListener listener) {
this.listener = listener;
}
/**
* 蛇移动
*/
public void move() {
tail = snakeBody.removeFirst();
head = snakeBody.getLast();
x = head.x;
y = head.y;
if (oldDirection + direction == 0) {
this.direction = oldDirection;
}
switch (direction) {
case UPPER:
if (y == 0) {
y = MyGrid.GRID_Y - 1;
}
y--;
break;
case DOWN:
if (y == MyGrid.GRID_Y - 1) {
y = 0;
}
y++;
break;
case LEFT:
if (x == 0) {
x = MyGrid.GRID_X - 1;
}
x--;
break;
case RIGHT:
if (x == MyGrid.GRID_X - 1) {
x = 0;
}
x++;
}
head = newHead(x, y);
snakeBody.addLast(head);
oldDirection = this.direction;
listener.touchListener(this);
}
/**
* 蛇改变方向
*/
public void changeDirection(int direction) {
this.direction = direction;
}
public void eatFood() {
snakeBody.addFirst(tail);
}
/**
* 蛇显示
*
* @param g
*/
public void showMe(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.blue);
for (Point point : snakeBody) {
g2.fillRect(point.x * MyGrid.WHITH, point.y * MyGrid.HEIGH,
MyGrid.WHITH, MyGrid.HEIGH);
}
}
/**
* 蛇碰到自己
*
* @param snake
* @return
*/
public boolean touchBySelf(Snake snake) {
return false;
}
}
/**
* 格子
* @author Administrator
*
*/
class MyGrid {
public static final int WHITH = 10;
public static final int HEIGH = 10;
public static final int GRID_X = 29;
public static final int GRID_Y = 29;
}
interface TouchListener {
void touchListener(Snake snake);
}
/**
* 游戏面板
* @author Administrator
*
*/
class GamePanel extends JPanel{
/**
*
*/
private static final long serialVersionUID = 952972903675008331L;
private Snake snake;
private Food food;
public GamePanel(Snake snake, Food food) {
super();
this.snake = snake;
this.food = food;
}
public void paintGamePanel(){
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
snake.showMe(g);
food.showMe(g);
}
}
/**
* 控制器
* 按F2重新开始,按p暂停再按继续,方向键↑ ↓ → ←
* @author Administrator
*
*/
class MyControl extends KeyAdapter implements TouchListener{
private boolean pause = true;
private boolean restart = false;
private Snake snake;
private Food food;
private GamePanel gamePanel;
public MyControl(Snake snake, Food food, GamePanel gamePanel) {
super();
this.snake = snake;
this.food = food;
this.gamePanel = gamePanel;
}
@Override
public void keyPressed(KeyEvent e) {
int keycode = e.getKeyCode();
switch (keycode) {
case KeyEvent.VK_UP:
keycode = Snake.UPPER;
snake.changeDirection(keycode);
break;
case KeyEvent.VK_DOWN:
keycode = Snake.DOWN;
snake.changeDirection(keycode);
break;
case KeyEvent.VK_LEFT:
keycode = Snake.LEFT;
snake.changeDirection(keycode);
break;
case KeyEvent.VK_RIGHT:
keycode = Snake.RIGHT;
snake.changeDirection(keycode);
break;
case KeyEvent.VK_P:
pause = !pause;
break;
case KeyEvent.VK_F2:
restart = !restart;
break;
}
}
@Override
public void touchListener(Snake snake) {
if(this.snake.touchBySelf(snake)){
}
if(this.food.isEatBySnake(snake)){
snake.eatFood();
}
}
public void startGame(){
new snakeDrive().start();
}
private class snakeDrive extends Thread{
@Override
public void run() {
while(true){
while(pause){
if(restart){
snake.init();
restart = !restart;
}
snake.move();
gamePanel.paintGamePanel();
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}