标签:
一、容器类
package com.entity;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JPanel;
public class MyJPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
// 记录已有点坐标
private ArrayList<PointDect> arr = new ArrayList<PointDect>();
// 当前可吃点
private PointDect nowp = null;
private Random r = new Random();
// 初始化蛇头位置
public static int initx = 20, inity = 0;
// 初始化移动方向 0 1 2 3
private int driect = 2;
// 初始化3个组成蛇身
// 初始化一个可吃点
{
arr.add(new PointDect(initx, inity));
arr.add(new PointDect(initx - PointDect.WIDTH, inity));
arr.add(new PointDect(initx - 2 * PointDect.WIDTH, inity));
nowp = new PointDect(((r.nextInt(MyJFrame.JW - PointDect.WIDTH) + 9) / 10) * 10,
((r.nextInt(MyJFrame.JH - PointDect.HEIGHT) + 9) / 10) * 10);
}
public MyJPanel() {
}
// 添加点
public void addPoint() {
nowp = new PointDect(((r.nextInt(MyJFrame.JW - PointDect.WIDTH) + 9) / 10) * 10,
((r.nextInt(MyJFrame.JH - PointDect.HEIGHT) + 9) / 10) * 10);
}
// 判断蛇头和点相碰
public boolean judgePoint() {
if (arr.get(0).getX() == nowp.getX() && arr.get(0).getY() == nowp.getY()) {
arr.add(nowp);
nowp = null;
return true;
}
return false;
}
// 判断游戏是否结束
private boolean judgeGame() {
if ((arr.get(0).getX() >= MyJFrame.JW - PointDect.WIDTH || arr.get(0).getX() < 0)
|| (arr.get(0).getY() >= MyJFrame.JW - PointDect.HEIGHT || arr.get(0).getY() < 0)) {
return true;
} else {
for (int i = 1; i < arr.size(); i++) {
if ((arr.get(0).getX() == arr.get(i).getX() && arr.get(0).getY() == arr.get(i).getY())) {
return true;
}
}
}
return false;
}
// 移动蛇
public boolean snakeMove() {
int cx = 0;
int cy = 0;
int movex = 0, movey = 0;
switch (driect) {
case 0:
cx = -PointDect.M;
cy = 0;
break;
case 1:
cx = 0;
cy = -PointDect.M;
break;
case 2:
cx = PointDect.M;
cy = 0;
break;
case 3:
cx = 0;
cy = PointDect.M;
break;
}
movex = arr.get(0).getX() + cx;
movey = arr.get(0).getY() + cy;
arr.get(0).setX(movex);
arr.get(0).setY(movey);
boolean flag = judgeGame();
if (!flag) {
for (int i = arr.size() - 1; i > 0; i--) {
arr.get(i).setX(arr.get(i - 1).getX());
arr.get(i).setY(arr.get(i - 1).getY());
}
this.repaint();
}
return flag;
}
@Override
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i < arr.size(); i++) {
g.fillRect(arr.get(i).getX(), arr.get(i).getY(), PointDect.WIDTH, PointDect.HEIGHT);
}
if (nowp != null) {
g.fillRect(nowp.getX(), nowp.getY(), PointDect.WIDTH, PointDect.HEIGHT);
}
}
public int getDriect() {
return driect;
}
public void setDriect(int driect) {
this.driect = driect;
}
}
二、蛇组成类
package com.entity;
public class PointDect {
//蛇每个组成部分的宽高
public static final int WIDTH = 10, HEIGHT = 10;
//蛇每次的移动量
public static final int M=10;
//每个组成部分的x,y坐标
private int x;
private int y;
public PointDect() {
super();
}
public PointDect(int x, int y) {
super();
this.x = x;
this.y = 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;
}
}
三、运行类
package com.entity;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class MyJFrame extends JFrame {
public static final int JW = 500, JH = 500;
/**
*
*/
private static final long serialVersionUID = 1L;
private static MyJPanel jp = null;
public MyJFrame() {
this.setTitle("贪吃蛇");
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jp = new MyJPanel();
this.add(jp);
this.setVisible(true);
}
@SuppressWarnings("static-access")
public static void main(String[] args) throws InterruptedException {
MyJFrame my = new MyJFrame();
boolean flag=false;
my.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP&&jp.getDriect()!=3) {
jp.setDriect(1);
}
if (e.getKeyCode() == KeyEvent.VK_DOWN&&jp.getDriect()!=1) {
jp.setDriect(3);
}
if (e.getKeyCode() == KeyEvent.VK_LEFT&&jp.getDriect()!=2) {
jp.setDriect(0);
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT&&jp.getDriect()!=0) {
jp.setDriect(2);
}
}
});
while(true){
Thread.currentThread().sleep(100);
flag=jp.snakeMove();
if(flag){
break;
}
flag=jp.judgePoint();
if(flag){
jp.addPoint();
}
}
}
}
标签:
原文地址:http://www.cnblogs.com/begin-zero/p/5427089.html