资源描述
/*
* 贪吃蛇
*/
import java、awt、*; //包含文件
import javax、swing、*;
import java、awt、event、*;
public class GreedSnack extends JFrame{
int i,j;
ﻩWH_panel panel; //定义WH_panel得实例
ﻩJMenuBar wh_bar; //定义菜单实例
ﻩpublic GreedSnack() //构造函数
ﻩ{
ﻩ super("贪吃蛇——game-—"); //框架名称
ﻩContainer c=getContentPane(); //获得框架容器
ﻩsetBounds(200, 200, 620, 465); //设置frame得大小
c、setLayout(null); //设置框架布局
wh_bar=new JMenuBar(); //定义菜单实例
setJMenuBar(wh_bar); //设置菜单
ﻩ JMenu[]m={new JMenu("文件”),new JMenu("编辑")}; //主菜单
ﻩ JMenuItem[][]mi={ //下拉菜单项
ﻩﻩﻩ{new JMenuItem("开始”),new JMenuItem("退出”)}, //设计菜单得内容
ﻩ ﻩﻩ{new JMenuItem(”分数"),new JMenuItem("记录分")}
};
ﻩfor(i=0;i<m、length;i++) //添加菜单
{
ﻩ ﻩwh_bar、add(m[i]); //添加下拉菜单
ﻩﻩfor(j=0;j<mi[i]、length;j++) //小于菜单得长度
ﻩﻩﻩ{
ﻩﻩ m[i]、add(mi[i][j]); //添加
ﻩ ﻩ} //for
ﻩﻩ} //for
ﻩﻩmi[0][0]、addActionListener(new ActionListener() //设置菜单监听
{
ﻩ public void actionPerformed(ActionEvent e) //
ﻩ {
ﻩ try
ﻩﻩﻩ {
ﻩ ﻩﻩ panel、thread、start(); //开始线程
ﻩﻩﻩﻩﻩpanel、right(); //直接执行right函数
ﻩﻩ }
ﻩﻩﻩ catch(Exception ee){} //对线程进行捕获错误
}
ﻩﻩ});
addKeyListener(new KeyAdapter()
ﻩ{
ﻩﻩ public void keyPressed(KeyEvent e) //键盘监听
ﻩﻩﻩ {
ﻩﻩ if(e、getKeyCode()==KeyEvent、VK_LEFT) //监听左键
ﻩﻩ ﻩ panel、left(); //执行left函数
ﻩﻩﻩ if(e、getKeyCode()==KeyEvent、VK_RIGHT) //监听右键
ﻩﻩﻩﻩﻩ panel、right(); //执行right函数
ﻩ if(e、getKeyCode()==KeyEvent、VK_UP) //监听上键
panel、up(); //执行up函数
ﻩ if(e、getKeyCode()==KeyEvent、VK_DOWN) //监听下键
ﻩ ﻩﻩ panel、down(); //执行down函数
ﻩ } //键盘事件
ﻩﻩﻩ public void keyTyped(KeyEvent e) {}
ﻩﻩ public void keyReleased(KeyEvent e) {}
ﻩ });
ﻩpanel=new WH_panel();
panel、setLayout(null); //panel布局
c、add(panel); //添加panel
}
public static void main(String args[]) //主函数
ﻩ{
ﻩﻩGreedSnack app=new GreedSnack(); //设置frame得实例
ﻩ app、setDefaultCloseOperation(JFrame、EXIT_ON_CLOSE); //关闭窗口
ﻩﻩapp、setVisible(true); //设置成可见
} //main
} //greedsnack
class WH_panel extends JPanel implements Runnable //panel函数
{
ﻩThread thread; //定义线程
ﻩint x=0,y=0,wh_direct=0; //设置变量
ﻩint food_x=60,food_y=80; //初始食物得位置
ﻩint d_l,d_r,d_u,d_d; //定义行使方向
int i,j,wh_stop;
int food_date; //定义食物数
int [][]wh_array=new int[30][20]; //定义数组
ﻩpublic WH_panel() //构造函数
{
ﻩthis、setBounds(0, 0,600,400); //设置panel得大小
ﻩﻩthread=new Thread(this); //创建线程thread
for(i=0;i〈30;i++) //给数组付初值
ﻩ{
ﻩﻩfor(j=0;j<20;j++) //列标小于20
{
wh_array[i][j]=0; //将数组赋为0
ﻩ } //for
ﻩ } //for
ﻩ} //WH_panel()
ﻩpublic void left() //left函数
{
if(d_r!=3) //假设现在向右行进
ﻩ{
ﻩ wh_direct=1; //wh_direct为1
ﻩ d_l=1; //标记左不能运行
ﻩﻩﻩd_r=3;
ﻩﻩ d_u=0; //标记上可以运行
ﻩd_d=0; //标记下可以运行
ﻩﻩ}
ﻩﻩelse //假设现在不就是向右行进
ﻩ {
ﻩ d_l=0; //向左可以运行
ﻩﻩ}
ﻩ}
ﻩpublic void right() //右键函数
ﻩ{
ﻩif(d_l!=1) //假设现在向左行进
ﻩﻩ{
ﻩﻩwh_direct=2; //wh_direct为2
ﻩd_l=1;
ﻩ d_r=3; //向右不可以运行
ﻩﻩﻩd_u=0; //向上可以运行
ﻩ d_d=0; //向下可以运行
}
ﻩelse //假设没有向左行进
ﻩ{
ﻩﻩ d_r=0; //向右可以运行
ﻩ }
ﻩ}
public void up() //向上键函数
{
if(d_d!=7) //假设现在向下行进
{
ﻩﻩ wh_direct=3; //wh_direct为3
ﻩ d_u=5; //向上不可以运行
ﻩﻩﻩd_d=7;
ﻩﻩﻩd_l=0; //向左可以行进
ﻩ d_r=0; //向右可以运行
ﻩ }
ﻩ else //假设现在没有向下运行
ﻩ{
ﻩ d_u=0; //可以向上行进
}
ﻩ}
public void down() //向下键得函数
ﻩ{
ﻩﻩif(d_u!=5) //如果现在向上运行
ﻩ{
ﻩﻩ wh_direct=4; //wh_direct为4
ﻩ d_u=5;
ﻩ ﻩd_d=7; //现在不可向下行进
ﻩ d_r=0; //现在可向右行进
ﻩ d_l=0; //现在向左行进
ﻩ}
else //如果现在没有向上行进
ﻩ{
ﻩ d_d=0; //可以向下行进
ﻩﻩ}
ﻩ}
public void run() //线程函数
{
ﻩﻩwhile(true) //while循环
ﻩﻩ{
ﻩﻩ if(wh_direct==1) //向左方
ﻩﻩ {
ﻩ ﻩﻩif(x>=20&&y>=0&&x〈=580&&y<=380) //规定范围
ﻩﻩﻩif(wh_array[x/20-1][y/20]!=0) //当下一个有蛇身
ﻩﻩ {
ﻩ ﻩ wh_stop=1; //wh_stop=1
ﻩ }
ﻩﻩ x=x—20; //x坐标减小变化
ﻩﻩ wh_run();
ﻩ }
ﻩﻩﻩif(wh_direct==2) //向右方
ﻩﻩﻩ{
ﻩ if(x>=0&&y〉=0&&x〈=560&&y〈=380) //规定范围
ﻩﻩ if(wh_array[x/20+1][y/20]!=0) //当下一个有蛇身
ﻩﻩﻩ{
ﻩﻩ ﻩwh_stop=1; //wh_stop=1
ﻩ }
ﻩ x=x+20; //x坐标增大变化
ﻩﻩﻩ wh_run();
ﻩ ﻩ}
ﻩ if(wh_direct==3) //向上方
ﻩ{
ﻩﻩif(x〉=0&&y>=20&&x<=580&&y〈=380) //规定范围
ﻩﻩﻩ if(wh_array[x/20][y/20-1]!=0) //当下一个有蛇身
{
ﻩﻩ wh_stop=1; //wh_stop=1
ﻩﻩ}
ﻩﻩ y=y-20; //y坐标减小变化
ﻩ ﻩwh_run();
ﻩ ﻩ}
ﻩﻩﻩif(wh_direct==4) //向下方
ﻩ {
ﻩ ﻩﻩif(x〉=0&&y>=0&&x<=580&&y<=360) //规定范围
ﻩ ﻩ if(wh_array[x/20][y/20+1]!=0) //当下一个有蛇身
ﻩ {
ﻩﻩﻩﻩﻩwh_stop=1; //wh_stop=1
ﻩﻩﻩﻩ}
ﻩﻩﻩ y=y+20; //y坐标增大变化
ﻩﻩ wh_run();
ﻩﻩ}
ﻩﻩif(food_x==x&&food_y==y) //蛇头得坐标与食物相同
ﻩ {
ﻩﻩﻩfood_x=((int)(Math、random()*30))*20; //随机食物坐标X
ﻩ food_y=((int)(Math、random()*20))*20; //随机食物坐标Y
ﻩ repaint(); //刷新绘图
ﻩﻩ food_date=food_date+1; //食物数进行自加
ﻩ }
ﻩ ﻩif(x==600||y==400||x<0||y〈0||wh_stop==1) //到达边界跳出循环
ﻩﻩ{
ﻩ ﻩbreak;
ﻩ }
ﻩﻩ}
ﻩ}
public void wh_run() //蛇身行进函数
ﻩ{
ﻩ /*
ﻩ * 此函数为贪吃蛇得重点函数,实现蛇身得各种变化,主要思路就是:
ﻩﻩ * 将面板分割成20X30得数组,并将其初始值赋为0,每次将蛇头
ﻩﻩ * 值赋为1,然后对数组非0得数进行加1操作,若数组内得值大于
ﻩ * 蛇本身应有得长度,就将其值改为0,最后对数组不为0得进行绘
ﻩ * 图.
*/
ﻩif(x>=0&&y>=0&&x<=580&&y<=380) //当坐标满足数组范围时
ﻩ{
ﻩ wh_array[x/20][y/20]=1; //此时蛇头数组值为1
ﻩ}
ﻩ for(i=0;i<30;i++) //检测整个数组
{
ﻩ for(j=0;j<20;j++) //y小于20
ﻩ {
ﻩ ﻩ if(wh_array[i][j]!=0) //如果数组中含有不为0得值
ﻩﻩ {
ﻩ wh_array[i][j]=wh_array[i][j]+1; //对其加1操作
ﻩ ﻩ} //if
} //for
} //for
repaint(); //从绘窗口
wh_sleep(200); //睡眠
ﻩ for(i=0;i〈30;i++) //给数组付初值
{
ﻩfor(j=0;j<20;j++) //y小于20
ﻩ ﻩ{
ﻩ if(wh_array[i][j]>food_date+1) //若蛇身长度大于食物数加1
ﻩﻩ {
ﻩ ﻩﻩﻩwh_array[i][j]=0; //将值变为0
ﻩﻩ ﻩ} //if
} //for
} //for
} //wh_run()
protected void paintponent(Graphics g) //绘图函数
{
g、setColor(Color、getHSBColor(0, 0, 200)); //panel背景颜色
ﻩﻩg、fillRect(0, 0, this、getWidth(), this、getHeight());
ﻩfor(i=0;i<30;i++) //检测数组
{
ﻩﻩﻩfor(j=0;j<20;j++) //y坐标小于20
ﻩﻩ{
ﻩﻩ if(wh_array[i][j]!=0) //在wh_array不为0时
ﻩﻩ {
ﻩﻩﻩ g、setColor(Color、BLUE); //蛇身颜色
ﻩ ﻩ g、fillRect(20*i, 20*j, 20, 20); //画蛇身
ﻩ ﻩ g、setColor(Color、lightGray); //蛇身外框颜色
ﻩﻩ g、drawRect(20*i, 20*j, 20, 20); //蛇身外框
ﻩ ﻩﻩ} //if
ﻩ } //for
ﻩ } //for
ﻩ g、setColor(Color、RED); //食物颜色
g、fillOval(food_x,food_y,20,20); //画食物
ﻩif(x==600||y==400||x<0||y〈0||wh_stop==1) //当蛇撞墙时
{
ﻩﻩﻩg、setColor(Color、getHSBColor(0, 0, 200)); //panel背景颜色
ﻩg、fillRect(0, 0, this、getWidth(), this、getHeight()); //画矩形充当背景
ﻩ g、setColor(Color、BLACK); //设置字符串颜色
ﻩ g、drawString("game over”, 250, 150); //输出game over
ﻩ} //if
ﻩ} //paintponent
ﻩpublic static void wh_sleep(long millis) //自定义sleep函数
ﻩ{
ﻩtry
ﻩ{
Thread、sleep(millis); //调用线程休眠
ﻩﻩ} //try
catch(InterruptedException e) //捕获错误
{
ﻩﻩSystem、err、println("”+e);
ﻩ e、printStackTrace(); //输出错误
} //catch
ﻩ} //wh_sleep
}
展开阅读全文