效果预览图
视频预览
贪吃蛇小游戏
代码部分
package demo01;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;public class Snake008 extends JFrame implements KeyListener, ActionListener, MouseListener {// 定义一个内部类用来创建坐标class Point{public int x;public int y;public Point (int x,int y){this.x=x;this.y=y;}}
// 窗口的宽int width=800;
// 窗口的高int height=900;// 设置分数int fenshu=0;// 0表示上 1表示下 2表示左 3表示右int fangxiang=3;// 游戏状态 0表示正常 1表示暂停 2表示游戏结束int zhuangtai=0;// 设置图片private Image image=null;Graphics gf=null;// 画蛇(蛇的初始顶点坐标)Point points[]= {new Point(6,3),new Point(5,3),new Point(4,3),new Point(3,3)};// 食物Point food=getfood();// 设置计时器Timer timer=new Timer(500,this);public void Tian(){//设置宽为800,高为900this.setSize(width,height);//设置窗口能看见this.setVisible(true);//定时器的开关timer.start();//设置键盘监听事件(也就是当键盘有什么反应都会调用此函数)this.addKeyListener(this);}// 绘制图形public void paint(Graphics g){//初始化临时图片//解决闪烁问题if(image==null){image=this.createImage(width,height);}if(this.gf ==null){this.gf =image.getGraphics();}// 擦除背景gf.setColor(Color.white);//绘制白色背景gf.fill3DRect(0,0,width,height,true);// 绘制棋盘网格
// 网格颜色gf.setColor(Color.darkGray);// 画竖线for (int j = 0; j <=width ; j+=50) {gf.drawLine(j,0,j,height);}
// 画横线for (int i = 0; i <=height ; i+=50) {gf.drawLine(0,i,width,i);}// 画蛇gf.setColor(Color.green);for (int i = 0; i < points.length; i++) {gf.fill3DRect(points[i].x*50,points[i].y*50,50,50,true);}// 食物gf.setColor(Color.yellow);gf.fill3DRect(food.x*50,food.y*50,50,50,true);
// 重画蛇头gf.setColor(Color.RED);gf.fill3DRect(points[0].x*50,points[0].y*50,50,50,true);// 分数if (zhuangtai==0||zhuangtai==1){gf.setColor(Color.GRAY);gf.fill3DRect(0,0,width,height-800,true);gf.setColor(Color.black);gf.setFont(new Font("微软雅黑", 10, 25)); //设置字体gf.drawString("得分:"+fenshu+"分",300,75);}else if(zhuangtai==2){gf.setColor(Color.GRAY);gf.fill3DRect(0,0,width,height-800,true);gf.setColor(Color.black);gf.setFont(new Font("微软雅黑", 10, 25)); //设置字体gf.drawString("游戏结束 最终得分:"+fenshu+"分",250,75);}g.drawImage(image,0,0,null);}
// 按钮单击方法public void actionPerformed(ActionEvent e) {
// 保留蛇的最后一步Point end=new Point(points[points.length-1].x,points[points.length-1].y);// 向上if(fangxiang==0){for (int i = 0,j=points.length-1; i < points.length; i++) {if(j-i>0){points[j-i].x=points[j-i-1].x;points[j-i].y=points[j-i-1].y;}else if(j-i==0){points[j-i].x=points[j-i].x;points[j-i].y=--points[j-i].y;}repaint();}// 向下}else if(fangxiang==1){for (int i = 0,j=points.length-1; i < points.length; i++) {if(j-i>0){points[j-i].x=points[j-i-1].x;points[j-i].y=points[j-i-1].y;}else if(j-i==0){points[j-i].x=points[j-i].x;points[j-i].y=++points[j-i].y;}repaint();}// 向左}else if(fangxiang==2){for (int i = 0,j=points.length-1; i < points.length; i++) {if(j-i>0){points[j-i].x=points[j-i-1].x;points[j-i].y=points[j-i-1].y;}else if(j-i==0){points[j-i].x=--points[j-i].x;points[j-i].y=points[j-i].y;}repaint();}// 向右}else if(fangxiang==3){for (int i = 0,j=points.length-1; i < points.length; i++) {if(j-i>0){points[j-i].x=points[j-i-1].x;points[j-i].y=points[j-i-1].y;}else if(j-i==0){points[j-i].x=++points[j-i].x;points[j-i].y=points[j-i].y;}repaint();}}//蛇吃食物的处理if(food.x==points[0].x&&food.y==points[0].y){Point newpoints[]=new Point[points.length+1];for (int i = 0; i < points.length; i++) {newpoints[i]=points[i];}newpoints[newpoints.length-1]=end;points=newpoints;
// 当食物被蛇吃后重新生成新的食物food=getfood();fenshu=points.length-4;}// 检测撞墙if(points[0].x<0||points[0].x>15){zhuangtai=2;timer.stop();}if(points[0].y<2||points[0].y>17){zhuangtai=2;timer.stop();}
// 检测是否撞到自己for (int i = 1; i <points.length ; i++) {if(points[0].x==points[i].x&&points[0].y==points[i].y){zhuangtai=2;timer.stop();}}}public void keyTyped(KeyEvent e) {}// 按下键盘--控制游戏的开始以及蛇的移动方向public void keyPressed(KeyEvent e) {// 获取从键盘输入的键int key = e.getKeyCode();if(key == KeyEvent.VK_SPACE) {//空格键// 0表示正常 1表示暂停 2表示结束if(zhuangtai==0){
// 从正常状态变为暂停状态zhuangtai=1;timer.stop();}else if(zhuangtai==1){
// 从暂停状态变为正常状态zhuangtai=0;timer.start();}else if(zhuangtai==2){zhuangtai=0;
// 启动定时器timer.start();
// 重置分数fenshu=0;
// 重置蛇points=new Point[]{new Point(6,3),new Point(5,3),new Point(4,3),new Point(3,3)};
// 重置食物food=getfood();
// 重置方向fangxiang=3;}}
//判断状态if(zhuangtai==0){// 上if (key == KeyEvent.VK_UP&&fangxiang!=1) {fangxiang=0;
// 下}else if (key == KeyEvent.VK_DOWN&&fangxiang!=0) {fangxiang=1;
// 左}else if(key == KeyEvent.VK_LEFT&&fangxiang!=3){fangxiang=2;
// 右}else if (key == KeyEvent.VK_RIGHT&&fangxiang!=2) {fangxiang=3;}}
// 强制重绘页面repaint();}// 产生随机食物点Point getfood(){Random rd=new Random();Point newfood=new Point(rd.nextInt(16),rd.nextInt(16)+2);for (int i = 0; i < points.length; i++) {
// 产生一个合法的食物随机点if(newfood.x==points[i].x&&newfood.y==points[i].y){newfood=new Point(rd.nextInt(16),rd.nextInt(16)+2);i=0;}}return newfood;}public void keyReleased(KeyEvent e) {}public void mouseClicked(MouseEvent e) {}public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) {}public void mouseEntered(MouseEvent e) {}public void mouseExited(MouseEvent e) {}public static void main(String[] args) {Snake008 snake=new Snake008();snake.Tian();}
}
没有过多的测试,如有bug欢迎友友下方留言交流。