jia-hao

java实现贪吃蛇

游戏的各个类存放的位置如图所示

 

 

备注:

  • header图片为窗口上部分图片,像素为850*64

  • body图片为小蛇的身体图片,像素为25*25

  • food图片为小蛇的食物图片,像素为25*25

  • up,down,right,lift图片均为小蛇的头部图片,因为小蛇要由键盘控制转向,所以up,down,right,lift图片分别代表了小蛇的头部朝向。

 

读者可以根据所提供的像素自己制作小蛇的相关图片。

 

 

游戏的启动程序

package EatSnake;

import javax.swing.*;
//开始游戏主程序
public class StartGame {
   public static void main(String[] args) {
       JFrame frame=new JFrame();

       frame.setResizable(false);
       frame.setBounds(10,10,900,720);
       frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

       frame.add(new GamePanel());
       frame.setVisible(true);
  }
}

游戏的图片资源

package EatSnake;
import javax.swing.*;
import java.net.URL;

//获得各个图片资源
public class Data {
   public static URL headerURl=Data.class.getResource("statics/header.png");
   public static ImageIcon header=new ImageIcon(headerURl);    //头部封面

   public static URL rightURl=Data.class.getResource("statics/right.png");
   public static ImageIcon right=new ImageIcon(rightURl);

   public static URL leftURL=Data.class.getResource("statics/lift.png");
   public static ImageIcon left=new ImageIcon(leftURL);

   public static URL upURL=Data.class.getResource("statics/up.png");
   public static ImageIcon up=new ImageIcon(upURL);

   public static URL downURL=Data.class.getResource("statics/down.png");
   public static ImageIcon down=new ImageIcon(downURL);

   public static URL bodyURL=Data.class.getResource("statics/body.png");
   public  static ImageIcon body=new ImageIcon(bodyURL);

   public static URL foodURL=Data.class.getResource("statics/food.png");
   public static ImageIcon food=new ImageIcon(foodURL);   //食物图标

}

游戏的绘制面板

package EatSnake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class GamePanel extends JPanel implements KeyListener,ActionListener {
   //定义蛇的数据结构
   int length;
   int[] snakeX=new int[600];
   int[] snakeY=new int[500];
   String fx;      //初始化蛇头方向
   //设置食物的相关属性
   int foodX,foodY;
   Random random=new Random();
   int score;      //定义在游戏中获得的积分.


   //设置游戏当前状态为不开始
   boolean isStart=false;      //设置游戏的初始状态是没开始
   boolean isFail=false; //判断游戏是否失败,默认没失败
   //设置定时器--->通过时间监听来完成
   //以毫秒为单位,一秒刷新十次
   Timer timer=new Timer(100,this);
   //构造器
   public GamePanel(){

       init();
       this.setFocusable(true);        //获取焦点事件
       this.addKeyListener(this);      //添加键盘监听事件
       timer.start();
  }
   public void init(){
       length=3;
       snakeX[0]=100;snakeY[0]=100; //脑袋的坐标
       snakeX[1]=75;snakeY[1]=100; //第一个身体的坐标
       snakeX[2]=50;snakeY[2]=100; //第二个身体的坐标
       fx="R";
       //把食物随机分布在界面上
       foodX=25+25* random.nextInt(34);    //random随机产生一个[0,34)的数字
       foodY=75+25*random.nextInt(24);     //random随机产生一个[0,24)的数字
       score=0;
  }
   @Override
   //绘制画板,游戏中的所有东西都通过这个画笔来画(实现的抽象方法)
   protected void paintComponent(Graphics g) {
       super.paintComponent(g);        //清屏
       this.setBackground(Color.WHITE);
       Data.header.paintIcon(this,g,25,11);//头部广告栏。
       g.fillRect(25,75,850,600);

       g.setColor(Color.black);
       g.setFont(new Font("微软雅黑",Font.BOLD,20));   //设置画笔的字体属性
       g.

分类:

技术点:

相关文章: