import java.awt.Button;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JOptionPane;
public class Snake implements KeyListener,Runnable {
/**蛇最多只能有25个节点*/
SnakeNode snakenods[] = new SnakeNode[25];
/**蛇的每个节点都要有一个坐标*/
Point pSnakeNodePoint[] = new Point[15];
/**由于不知道怎样实现动态改变数组下标,只好用一个值来记录实际蛇的节点个数,即长度*/
int itSnakeLength;
/**特意的设置两个值来跟踪蛇的头和尾*/
int itHead,itTail;
/**蛇的运动方向*/
int itDirection;
/**只有一个线程*/
Thread tdSnake;
/**跟踪头尾点的坐标*/
Point pHead,pTail;
/**窗口必需为类成员*/
SnakeFrame mf;
/**蛇的目标--食物*/
SnakeNode snFood;
/**蛇运动的速度
snakenods[0] = new SnakeNode(0);
mf.add(snakenods[0]);
snakenods[0].setLocation(40,40);
snakenods[0].setBackground(Color.RED);
itHead = 0;
snakenods[1] = new SnakeNode(1);
snakenods[1].setBackground(Color.BLUE);
mf.add(snakenods[1]);
snakenods[1].setLocation(60,40);
snakenods[2] = new SnakeNode(2);
snakenods[2].setBackground(Color.CYAN);
mf.add(snakenods[2]);
snakenods[2].setLocation(80,40);
itTail = 2;
itDirection = 1;
itSnakeLength = 3;
snFood = new SnakeNode(90);
mf.add(snFood);
snFood.setLocation((int) Math.round(Math.random()*300),(int) Math.round(Math.random()*300));
tdSnake = new Thread(this);
tdSnake.start();
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_LEFT){
itDirection = KeyEvent.VK_LEFT;
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
itDirection = KeyEvent.VK_RIGHT;
}
if(e.getKeyCode()==KeyEvent.VK_DOWN){
itDirection = KeyEvent.VK_DOWN;
}
if(e.getKeyCode()==KeyEvent.VK_UP){
itDirection = KeyEvent.VK_UP;
}
if(e.getKeyCode()==KeyEvent.VK_SPACE){
EatFood();
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void run(){
while(true){
switch(itDirection){
case KeyEvent.VK_UP:{
snakenods[itTail].setLocation(snakenods[itHead].getBounds().x,snakenods[itHead].getBounds().y-20);
isSnakeDied();
break;
}
case KeyEvent.VK_DOWN:{
snakenods[itTail].setLocation(snakenods[itHead].getBounds().x,snakenods[itHead].getBounds().y+20);
isSnakeDied();
break;
}
case KeyEvent.VK_LEFT:{
snakenods[itTail].setLocation(snakenods[itHead].getBounds().x-20,snakenods[itHead].getBounds().y);
isSnakeDied();
break;
}
default:{
snakenods[itTail].setLocation(snakenods[itHead].getBounds().x+20,snakenods[itHead].getBounds().y);
isSnakeDied();
break;
}
}
EatFood();
itHead = itTail;
if(itTail == 0)itTail = itSnakeLength-1;
else {itTail = itTail-1;}
try {
tdSnake.sleep(itSnakeSpeed);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**画一堵墙*/
public void DrawWall(){
SnakeNode Wall[][] = new SnakeNode[2][18];
for(int i=0;i
mf.add(Wall[0]);
Wall[0].setLocation(i*20+3,350);
}
for(int i=0;i
mf.add(Wall[1]);
Wall[1].setLocation(350,i*20);
}
}
/**蛇吃到食物的时候,长一个节点*/
public void EatFood(){
if(Math.abs(snakenods[itHead].getBounds().x-snFood.getBounds().x)<20&&Math.abs(snakenods[itHead].getBounds().y-snFood.getBounds().y)<20){
snakenods[itSnakeLength] = new SnakeNode(itSnakeLength);
mf.add(snakenods[itSnakeLength]);
itSnakeLength ++;
snFood.setLocation((int) Math.round(Math.random()*300+20),(int) Math.round(Math.random()*300+20));
itGrades ++;
btShowGrades.setLabel("游戏分数"+itGrades);
if(itGrades%2==0){
itSnakeSpeed -= 100;
}
if(itGrades>=12){
JOptionPane.showMessageDialog(null, "恭喜你!游戏已经赢了!!!", "胜利!", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
}
/**判断蛇的行为是否是
public void isSnakeDied(){
int itSnakteHeadID = 0;
for(int i=0;i
itSnakteHeadID = i;
}
}
for(int i=0;i
if(snakenods[itHead].getBounds().x == snakenods.getBounds().x&&snakenods[itHead].getBounds().y == snakenods.getBounds().y){
JOptionPane.showMessageDialog(null, "蛇已死亡/n死亡原因:自残!", "蛇已死亡", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
}
if(snakenods[itHead].getBounds().x>300||snakenods[itHead].getBounds().y>300||snakenods[itHead].getBounds().x<0||snakenods[itHead].getBounds().y<0){
JOptionPane.showMessageDialog(null, "蛇已死亡/n死亡原因:撞墙!", "蛇已死亡", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
}
/**蛇的节点 就是画一个正方形*/
class SnakeNode extends Canvas{
/** 长方形的 高
/**
* 产生一个窗口
* */
class SnakeFrame extends Frame
{
public SnakeFrame(int xStart,int yStart,int width,int height)
{
setVisible(true);//设置窗体可见
validate();//窗体刷新
setBounds(xStart,yStart,width,height);//设定窗体在屏幕的初始位置和 横宽
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);//点击关闭,则关闭
}
});
}
}