AI智能
改变未来

java实现贪吃蛇小游戏(源码+注释)


一.工程文件

二.Main.java

package com.company;import javax.swing.*;public class Main {public static void main(String[] args) {//创建窗体对象JFrame frame = new JFrame();//创建窗体参数()frame.setBounds(10,10,900,720);//设置不允许更改大小//frame.setResizable(false);//设置关闭方式frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//添加主题逻辑框架frame.add(new Mpanel());//设置窗体可见frame.setVisible(true);}}

三.Mpanel.java

package com.company;import javax.imageio.ImageIO;import javax.sound.sampled.*;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.20000event.KeyListener;import java.io.BufferedInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Random;//基本套路,继承JPanel,实现KeyListener, ActionListener接口public class Mpanel extends JPanel implements KeyListener, ActionListener {//定义所需要的图片元素//标题ImageIcon title;//蛇的身体部位ImageIcon body;//蛇向上方向的头ImageIcon up;//蛇向下方向的头ImageIcon down;//蛇向左方向的头ImageIcon left;//蛇向右方向的头ImageIcon right;//食物元素ImageIcon food;//定义初始长度int len = 3;//定义初始分数int score = 0;//存放蛇位置坐标的数组int[] snakex = new int[750];int[] snakey = new int[750];//定义方向String fx = "R";//定义游戏是否开始Boolean isStart = false;//定义游戏是否失败Boolean isFailed = false;//定义游戏时钟,在此可以设置蛇移动速度,默认100msTimer timer = new Timer(100,this);//定义食物的坐标int foodx;int foody;//引入随机数,用于生成食物的随机位置Random random = new Random();//定义背景音乐Clip bgm;//构造方法public Mpanel(){//加载图片元素loadImages();//初始化数据initSnake();//设置聚焦this.setFocusable(true);//设置键盘监听this.addKeyListener(this);//开始计时timer.start();//加载背景音乐loadBGM();}//设置绘图元素public void paintComponent(Graphics g){//调用父类方法super.paintComponent(g);//设置背景颜色为白色this.setBackground(Color.white);//设置标题元素位置title.paintIcon(this,g,25,11);//设置白色背景填充位置g.fillRect(25,75,850,600);//设置画笔颜色为白色g.setColor(Color.white);//设置长度显示g.drawString("Len: " + len,750,35);//设置分数显示g.drawString("Score: " + score,750,50);//根据方向调用相应的蛇头元素if(fx == "R"){right.paintIcon(this,g,snakex[0],snakey[0]);}else if(fx == "L"){left.paintIcon(this,g,snakex[0],snakey[0]);}else if(fx == "U"){up.paintIcon(this,g,snakex[0],snakey[0]);}else if(fx == "D"){down.paintIcon(this,g,snakex[0],snakey[0]);}for(int i=1;i<len;i++){body.paintIcon(this,g,snakex[i],snakey[i]);}//调用食物元素food.paintIcon(this,g,foodx,foody);//游戏开始的处理if(isStart == false){//设置画笔颜色g.setColor(Color.white);//设置字体g.setFont(new Font("arial",Font.BOLD,40));//设置具体文字g.drawString("Press Space To Start",250,300);}//游戏失败的处理if(isFailed){//设置画笔颜色g.setColor(Color.red);//设置字体g.setFont(new Font("arial",Font.BOLD,40));//设置具体文字g.drawString("Failed: Press Space To Start",250,300);}}//初始化数据public void initSnake(){len = 3;snakex[0] = 100;snakey[0] = 100;snakex[1] = 75;snakey[1] = 100;snakex[2] = 50;snakey[2] = 100;//设置食物的初始随机生成位置foodx = 25 + 25 * random.nextInt(34);foody = 75 + 25 * random.nextInt(24);//设置初始值fx = "R";score = 0;}//接口要求的实现方法@Overridepublic void keyTyped(KeyEvent e) {}@Overridepublic void keyPressed(KeyEvent e) {//获取键盘监听int keyCode = e.getKeyCode();//空格if(keyCode == KeyEvent.VK_SPACE){//如果失败则重置信息if(isFailed){isFailed = !isFailed;//重新开始initSnake();}//设置开始else {isStart = !isStart;}//判断开始后循环播放背景音乐if(isStart){bgm.loop(Clip.LOOP_CONTINUOUSLY);}//未开始和暂停则不播放背景音乐else {bgm.stop();}//重新绘图repaint();}//右键处理并设置不能回头else if(keyCode == KeyEvent.VK_LEFT && fx != "R"){fx = "L";}//左键else if(keyCode == KeyEvent.VK_RIGHT && fx != "L"){fx = "R";}//上键else if(keyCode == KeyEvent.VK_UP && fx != "D"){fx = "U";}//下键else if(keyCode == KeyEvent.VK_DOWN && fx != "U"){fx = "D";}}//接口要求的实现方法@Overridepublic void keyReleased(KeyEvent e) {}//事件监听@Overridepublic void actionPerformed(ActionEvent e) {//开始且没有失败的情况if(isStart && !isFailed){//循环移动蛇身for(int i=len-1;i>0;i--){snakex[i]=snakex[i-1];snakey[i]=snakey[i-1];}//向右移动并判断是否撞墙,撞墙则失败if(fx == "R"){snakex[0]=snakex[0]+25;if(snakex[0]>825)isFailed=true;}//向左移动并判断是否撞墙,撞墙则失败else if(fx == "L"){snakex[0]=snakex[0]-25;if(snakex[0]<50)isFailed=true;}//向上移动并判断是否撞墙,撞墙则失败else if(fx == "U"){snakey[0]=snakey[0]-25;if(snakey[0]<100)isFailed=true;}//向下移动并判断是否撞墙,撞墙则失败else if(fx == "D"){snakey[0]=snakey[0]+25;if(snakey[0]>625)isFailed=true;}//吃到食物的判断if(snakex[0]==foodx&&snakey[0]==foody){//身体加长,分加多,并且随机生成新食物len++;score += 10;foodx = 25 + 25 * random.nextInt(34);foody = 75 + 25 * random.nextInt(24);}//撞到自身的处理for(int i=1;i<len;i++){if(snakex[i]==snakex[0] && snakey[i]==snakey[0]){isFailed = true;}}//重新绘图repaint();}//事件刷新timer.start();}//加载背景音乐public void loadBGM(){try {//从类所在文件引入bgm = AudioSystem.getClip();InputStream is = this.getClass().getClassLoader().getResourceAsStream("com/company/sound/bgm.wav");AudioInputStream ais = AudioSystem.getAudioInputStream(new BufferedInputStream(is));bgm.open(ais);//bgm.loop(Clip.LOOP_CONTINUOUSLY);}//相应的异常处理catch (LineUnavailableException e) {e.printStackTrace();}catch (UnsupportedAudioFileException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();}}//加载图片public void loadImages(){InputStream is;try {//依次添加响应元素is = getClass().getClassLoader().getResourceAsStream("com/company/img/title.jpg");title = new ImageIcon(ImageIO.read(is));is = getClass().getClassLoader().getResourceAsStream("com/company/img/body.png");body = new ImageIcon(ImageIO.read(is));is = getClass().getClassLoader().getResourceAsStream("com/company/img/up.png");up = new ImageIcon(ImageIO.read(is));is = getClass().getClassLoader().getResourceAsStream("com/company/img/down.png");down = new ImageIcon(ImageIO.read(is));is = getClass().getClassLoader().getResourceAsStream("com/company/img/left.png");left = new ImageIcon(ImageIO.read(is));is = getClass().getClassLoader().getResourceAsStream("com/company/img/right.png");right = new ImageIcon(ImageIO.read(is));is = getClass().getClassLoader().getResourceAsStream("com/company/img/food.png");food = new ImageIcon(ImageIO.read(is));} catch (IOException e) {e.printStackTrace();}}}

四.效果



赞(0) 打赏
未经允许不得转载:爱站程序员基地 » java实现贪吃蛇小游戏(源码+注释)