代码如下
SE.java文件
import java.io.Serializable;
/**
* SE类--蛇的关节元素对象
* @author Administrator
*
*/
public class SE implements Serializable {
private static final long serialVersionUID = -6058052882884632278L;
/**
* shift+alt+s 调出小菜单
* 自动生成:
* 1--无参数构造器
* 2--全部参数构造器
* 3--setter和getter方法
* 4--重写hashCode和equals方法
* 5--重写toString方法
* 6--该类需要实现一个可序列化接口 serializable 并 添加可序列化的标识常量
*/
private int se_w ;
private int se_h ;
private int se_x ;
private int se_y ;
public SE() {
super();
// TODO Auto-generated constructor stub
}
public SE(int se_w, int se_h, int se_x, int se_y) {
super();
this.se_w = se_w;
this.se_h = se_h;
this.se_x = se_x;
this.se_y = se_y;
}
public int getSe_w() {
return se_w;
}
public void setSe_w(int se_w) {
this.se_w = se_w;
}
public int getSe_h() {
return se_h;
}
public void setSe_h(int se_h) {
this.se_h = se_h;
}
public int getSe_x() {
return se_x;
}
public void setSe_x(int se_x) {
this.se_x = se_x;
}
public int getSe_y() {
return se_y;
}
public void setSe_y(int se_y) {
this.se_y = se_y;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + se_h;
result = prime * result + se_w;
result = prime * result + se_x;
result = prime * result + se_y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SE other = (SE) obj;
if (se_h != other.se_h)
return false;
if (se_w != other.se_w)
return false;
if (se_x != other.se_x)
return false;
if (se_y != other.se_y)
return false;
return true;
}
@Override
public String toString() {
return "SE [se_w=" + se_w + ", se_h=" + se_h + ", se_x=" + se_x
+ ", se_y=" + se_y + "]";
}
}
Bean.java文件
import java.io.Serializable;
/*
* 豆子类
*/
public class Bean implements Serializable{
private static final long serialVersionUID = -8929116071517599107L;
private int b_w ;
private int b_h ;
private int b_x ;
private int b_y ;
private int b_r ;
private int b_g ;
private int b_b ;
public Bean() {
super();
// TODO Auto-generated constructor stub
}
public Bean(int b_w, int b_h, int b_x, int b_y, int b_r, int b_g, int b_b) {
super();
this.b_w = b_w;
this.b_h = b_h;
this.b_x = b_x;
this.b_y = b_y;
this.b_r = b_r;
this.b_g = b_g;
this.b_b = b_b;
}
public int getB_w() {
return b_w;
}
public void setB_w(int b_w) {
this.b_w = b_w;
}
public int getB_h() {
return b_h;
}
public void setB_h(int b_h) {
this.b_h = b_h;
}
public int getB_x() {
return b_x;
}
public void setB_x(int b_x) {
this.b_x = b_x;
}
public int getB_y() {
return b_y;
}
public void setB_y(int b_y) {
this.b_y = b_y;
}
public int getB_r() {
return b_r;
}
public void setB_r(int b_r) {
this.b_r = b_r;
}
public int getB_g() {
return b_g;
}
public void setB_g(int b_g) {
this.b_g = b_g;
}
public int getB_b() {
return b_b;
}
public void setB_b(int b_b) {
this.b_b = b_b;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + b_b;
result = prime * result + b_g;
result = prime * result + b_h;
result = prime * result + b_r;
result = prime * result + b_w;
result = prime * result + b_x;
result = prime * result + b_y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Bean other = (Bean) obj;
if (b_b != other.b_b)
return false;
if (b_g != other.b_g)
return false;
if (b_h != other.b_h)
return false;
if (b_r != other.b_r)
return false;
if (b_w != other.b_w)
return false;
if (b_x != other.b_x)
return false;
if (b_y != other.b_y)
return false;
return true;
}
@Override
public String toString() {
return "Bean [b_w=" + b_w + ", b_h=" + b_h + ", b_x=" + b_x + ", b_y="
+ b_y + ", b_r=" + b_r + ", b_g=" + b_g + ", b_b=" + b_b + "]";
}
}
SnakeMainFrame.java文件:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class SnakeMainFrame extends JFrame{
private static final long serialVersionUID = 4162523224330812384L;
private static final String UP = "UP" ;
private final static String DOWN = "DOWN" ;
private static final String LEFT = "LEFT" ;
private static final String RIGHT = "RIGHT" ;
private static final String STOP = "STOP" ;
private int fw = 400 ;
private int fh = 400 ;
private String direction = "UP" ; //取值有四个方向 UP DOWN LEFT RIGHT
public void init(){ //初始化
//this.setAlwaysOnTop(true);
this.setSize(fw, fh);
this.setUndecorated(true);
this.getContentPane().setBackground(Color.BLACK);
this.setLocationRelativeTo(null);
this.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if(KeyEvent.VK_SPACE==e.getKeyCode()){
direction = STOP ;
}
if(KeyEvent.VK_LEFT==e.getKeyCode()&&direction!=RIGHT){ //说明你按下的键是 <--
direction = LEFT ;
}
if(KeyEvent.VK_RIGHT==e.getKeyCode()&&direction!=LEFT){
direction = RIGHT ;
}
if(KeyEvent.VK_UP==e.getKeyCode()&&direction!=DOWN){
direction = UP ;
}
if(KeyEvent.VK_DOWN==e.getKeyCode()&&direction!=UP){
direction = DOWN ;
}
}
});
this.initSE_m();//调用初始化蛇
this.initBean(); //调用初始化豆子
this.timer.schedule(timertask, 1000/*延时时间 如果不想延时 设为0毫秒*/, 100/*执行的频率 毫秒数*/);
this.setVisible(true);
}
/**
* 产生豆子
*/
private int b_w = 20 ;
private int b_h = 20 ;
private int b_x ;
private int b_y ;
private int b_r ;
private int b_g ;
private int b_b ;
private Random rand = new Random();
private Bean bean = null ;
public void initBean_data(){
b_x = rand.nextInt(fw/20)*20;
b_y = rand.nextInt(fh/20)*20;
b_r = rand.nextInt(256);
b_g = rand.nextInt(256);
b_b = rand.nextInt(256);
}
// public Bean(int b_w, int b_h, int b_x, int b_y, int b_r, int b_g, int b_b) {
public void initBean(){
this.initBean_data();
for(SE e:snake){
if(e.getSe_x()==b_x&&e.getSe_y()==b_y){
initBean_data();
}
}
bean = new Bean(b_w,b_h,b_x,b_y,b_r,b_g,b_b);
}
/*产生蛇*/
private int se_x = fw/2 ;
private int se_y = fh/2 ;
private int se_w = 20 ;
private int se_h = 20 ;
private SE se = null ;
private LinkedList<SE> snake = new LinkedList<SE>();
private Timer timer = new Timer();
public void initSE(){
//使用new关键字调用SE类的全部参数构造器创建一个SE实例(对象),
//赋值给一个自定义的引用变量(引用该对象在堆内存中的首地址),
//并指明该引用变量的类型
se = new SE(se_w,se_h , se_x , se_y); //注意参数顺序
se_y += se_h ;
snake.add(se);
}
public void initSE_m(){
snake.removeAll(snake);
for (int i = 0; i < 3; i++) {
initSE();
}
//System.out.println(snake);
}
@Override
public void paint(Graphics g) {
super.paint(g);
//画出豆子
g.setColor(new Color(bean.getB_r(),bean.getB_g(),bean.getB_b()));
g.fillRect(bean.getB_x(), bean.getB_y(), bean.getB_w(), bean.getB_h());
if(!check(snake.get(0))){
this.timer.cancel();
int option = JOptionPane.showConfirmDialog(null,
"得分为:"+(this.snake.size()-3), "是否继续?", JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE, null);
switch (option) {
case JOptionPane.YES_NO_OPTION: {
this.dispose();
new SnakeMainFrame();
break;
}
case JOptionPane.NO_OPTION:
System.exit(0);
}
};
//画出蛇
for (int i = 0; i < snake.size() ; i++) {
if(i==0){
g.setColor(Color.RED);
}else{
g.setColor(Color.GREEN);
}
g.fill3DRect(snake.get(i).getSe_x(), snake.get(i).getSe_y(), snake.get(i).getSe_w(), snake.get(i).getSe_h(), true);
}
}
public boolean check(SE se){
for(int i = 1 ; i < snake.size() ; i++){
SE e = snake.get(i);
if(snake.getFirst().getSe_x()==e.getSe_x()&&e.getSe_y()==snake.getFirst().getSe_y()){
return false;
}
}
if(se.getSe_x()>fw||se.getSe_y()>fh||se.getSe_x()<0||se.getSe_y()<0){
return false;
}else{
return true;
}
}
public boolean collision(){
if(snake.get(0).getSe_x()==bean.getB_x()&&snake.get(0).getSe_y()==bean.getB_y()){
initBean();
return true;
}else{
return false;
}
}
TimerTask timertask = new TimerTask() { //这是一个执行计划 就是准备做的事情 具体事情的逻辑代码在run方法中
@Override
public void run() {
//将蛇的最后一个关节移除 获取头部数据,在头部数据的基础上创建一个新关节元素并添加到容器中、
if(SnakeMainFrame.STOP.equalsIgnoreCase(direction)){
}
if(SnakeMainFrame.UP.equalsIgnoreCase(direction)){
SE e = new SE(
SnakeMainFrame.this.snake.get(0).getSe_w(),
SnakeMainFrame.this.snake.get(0).getSe_h(),
SnakeMainFrame.this.snake.get(0).getSe_x(),
SnakeMainFrame.this.snake.get(0).getSe_y() - SnakeMainFrame.this.snake.get(0).getSe_h()
);
if(!collision()){
SnakeMainFrame.this.snake.removeLast();
}
snake.addFirst(e);
repaint();//执行重画方法
}
if(SnakeMainFrame.DOWN.equalsIgnoreCase(direction)){
SE e = new SE(
SnakeMainFrame.this.snake.get(0).getSe_w(),
SnakeMainFrame.this.snake.get(0).getSe_h(),
SnakeMainFrame.this.snake.get(0).getSe_x(),
SnakeMainFrame.this.snake.get(0).getSe_y() + SnakeMainFrame.this.snake.get(0).getSe_h()
);
if(!collision()){
SnakeMainFrame.this.snake.removeLast();
}
snake.addFirst(e);
repaint();//执行重画方法
}
if(SnakeMainFrame.LEFT.equalsIgnoreCase(direction)){
SE e = new SE(
SnakeMainFrame.this.snake.get(0).getSe_w(),
SnakeMainFrame.this.snake.get(0).getSe_h(),
SnakeMainFrame.this.snake.get(0).getSe_x() -SnakeMainFrame.this.snake.get(0).getSe_w() ,
SnakeMainFrame.this.snake.get(0).getSe_y()
);
if(!collision()){
SnakeMainFrame.this.snake.removeLast();
}
snake.addFirst(e);
repaint();//执行重画方法
}
if(SnakeMainFrame.RIGHT.equalsIgnoreCase(direction)){
SE e = new SE(
SnakeMainFrame.this.snake.get(0).getSe_w(),
SnakeMainFrame.this.snake.get(0).getSe_h(),
SnakeMainFrame.this.snake.get(0).getSe_x() + SnakeMainFrame.this.snake.get(0).getSe_w() ,
SnakeMainFrame.this.snake.get(0).getSe_y()
);
if(!collision()){
SnakeMainFrame.this.snake.removeLast();
}
snake.addFirst(e);
repaint();//执行重画方法
}
}
};
public SnakeMainFrame() {
this.init();
}
public static void main(String[] args) {
new SnakeMainFrame();
}
}