今天用了一个多小时的时间,开发了一个简单的贪吃蛇游戏

 

源码下载:https://pan.baidu.com/s/1GrvTAHbo8s4pQu74jz2yyw

提取码:3m8y

 

成品展示图

  

 

其实用到的知识点并不多,下面展示一些自己设计

 

1、页面布局

  

 

2、自己画的几个图片

  

 

3、开始创建一个新的项目,并且按下面来写

  创建StartGame包,并创建Start.java

  目的的创建JFrame窗体

package StartGame;

import java.awt.Color;

import javax.swing.JFrame;

import GamePanel.GamePanel;



public class Start {

    public static void main(String[] args) {
       
        /*
         * 1、创建窗体,创建主体面板,
         * 2、画静态的蛇 
         * 3、让蛇动起来 (使用线程)
         * 4、进行键盘控制蛇的走动 (使用键盘监听)
         * 5、计算分数。
         */
        
        JFrame jframe = new JFrame();
        
        jframe.setTitle("简单贪吃蛇游戏");
        jframe.setBounds(300, 50, 650, 470);
        jframe.setResizable(false);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        jframe.add(new GamePanel());
        jframe.setVisible(true);

    }
}

 

 创建Resources包,创建ReadyImages.java 

 目的是用来存放一些静态的图片信息

package Resources;

import java.net.URL;

import javax.swing.ImageIcon;

public class ReadyImages {    //准备好图片
    
    public static URL headleft = ReadyImages.class.getResource("/Resources/snakeheadleft.gif");
    public static URL headright = ReadyImages.class.getResource("/Resources/snakeheadright.gif");
    public static URL headup = ReadyImages.class.getResource("/Resources/snakeheadup.gif");
    public static URL headdown = ReadyImages.class.getResource("/Resources/snakeheaddown.gif");
    public static URL body = ReadyImages.class.getResource("/Resources/snakebody.gif");
    public static URL food = ReadyImages.class.getResource("/Resources/snakefood.gif");
    
    public static ImageIcon headleftIcon = new ImageIcon(headleft);
    public static ImageIcon headrightIcon = new ImageIcon(headright);
    public static ImageIcon headupIcon = new ImageIcon(headup);
    public static ImageIcon headdownIcon = new ImageIcon(headdown);
    public static ImageIcon bodyIcon = new ImageIcon(body);
    public static ImageIcon foodIcon = new ImageIcon(food);
}

 

  创建Snake包,并创建Snake.java

  这个是蛇的实体类

package Snake;

import javax.swing.ImageIcon;

import Resources.ReadyImages;

public class Snake implements Runnable { // 创建一个蛇类

    public int snakeLength; // 蛇的长度
    public int[] snakeX = new int[600]; // 蛇的x坐标
    public int[] snakeY = new int[500]; // 蛇的y坐标
    public String action = "R";    //蛇的动作,默认是向右
    public ImageIcon headimages = ReadyImages.headrightIcon;//头部的图片

    public Snake() { // 初始化蛇的位置

        // 每一个格子都有(25高25宽)
        snakeLength = 3;
        snakeX[0] = 100;snakeY[0] = 100;
        snakeX[1] = 75;snakeY[1] = 100;
        snakeX[2] = 50;snakeY[2] = 100;
    }

    // 重写run方法,
    @Override
    public void run() {
        
        for (int i = snakeLength - 1; i > 0; i--) { // 除了头部其他的都向右移

            snakeX[i] = snakeX[i - 1];
            snakeY[i] = snakeY[i - 1];

        }
        
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    /** 吃的动作 */
    public boolean eatfood(int foodx, int foody) {

        // 吃到食物
        if (foodx == snakeX[0] && foody == snakeY[0]) {

            snakeLength++; // 身体加1
            return true;
        }

        return false;
    }
    
    /**吃到自己*/
    public boolean eatmyself() {
        
        for (int i = 1; i < snakeLength; i++) {

            if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {
                return true;
            }
        }
        
        return false;
    }

    /**蛇的边界问题*/
    public void border() {
        
        if (action == "L") { // 判断蛇的动作

            snakeX[0] = snakeX[0] - 25;
            if (snakeX[0] < 25) {
                snakeX[0] = 600;
            }
            
            headimages = ReadyImages.headleftIcon;

        } else if (action == "R") {

            snakeX[0] = snakeX[0] + 25;
            if (snakeX[0] > 600) {
                snakeX[0] = 25;
            }
            
            headimages = ReadyImages.headrightIcon;
            
        } else if (action == "U") {

            snakeY[0] = snakeY[0] - 25;
            if (snakeY[0] < 75) {
                snakeY[0] = 400;
            }
            headimages = ReadyImages.headupIcon;
            
        } else if (action == "D") {

            snakeY[0] = snakeY[0] + 25;
            if (snakeY[0] > 400) {
                snakeY[0] = 75;
            }
            headimages = ReadyImages.headdownIcon;
        }
    }
}

 

  创建GamePanel包,并创建GamePanel.java

  这个是游戏的主面板,

  

package GamePanel;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Random;

import javax.swing.JPanel;

import Controller.GameKeyListener;

import Resources.ReadyImages;
import Snake.Snake;

public class GamePanel extends JPanel{
    
    Snake snake;    //蛇的类别
    GameKeyListener keyListener;    //键盘操作
    int grade = 0;    //积分
    boolean start = false;    //是否开始游戏。默认不是
    boolean dead = false;    //蛇是否死亡
    int foodX;    //设置蛇出现的位置
    int foodY;
    Random random = new Random();
    
    
    public GamePanel(){    //初始化游戏界面
        
        initGame();    //初始化
    }
    
    /**初始化游戏*/
    public void initGame() {
        
        //把线程开起来
        snake = new Snake();
        keyListener = new GameKeyListener(snake.action,start);
        
                
        foodX = 25 + 25 * random.nextInt(24);    //随机生成食物
        foodY = 75 + 25 * random.nextInt(14);
                
        this.setFocusable(true); // 焦点可以在游戏上
        this.addKeyListener(keyListener);    //添加键盘事件
        
    }
    
    //游戏画板
    @Override
    protected void paintComponent(Graphics g) {
        
        super.paintComponent(g);
        
        //画一个游戏的面板
        g.setColor(Color.WHITE);
        g.fillRect(25, 75, 600, 350);    
        
        //积分显示块
        g.setColor(Color.BLACK);        
        g.setFont(new Font("微软雅黑", Font.BOLD, 18));
        g.drawString("积分" + grade, 500, 50);
        
        //画静态蛇
        snake.headimages.paintIcon(this, g, snake.snakeX[0], snake.snakeY[0]);
        
        for(int i = 1; i < snake.snakeLength; i++) {    //i需要从1开始,因为头部已经设置了
            
            ReadyImages.bodyIcon.paintIcon(this, g, snake.snakeX[i], snake.snakeY[i]);
            
        }
        
        
        //画食物
        ReadyImages.foodIcon.paintIcon(this, g, foodX, foodY);
        
        //游戏的判断
        snake.action = keyListener.snakeAction();
        start = keyListener.isStart();
        
        dead = snake.eatmyself();    //吃到自己
        
    
        //开始游戏
        if(start == true && dead == false) {
            
            snake.run();    //让蛇的线程跑起来
            snake.border();    //蛇的边界问题
            boolean eatting = snake.eatfood(foodX, foodY);//吃到食物
            
            if(eatting) {    //吃到食物
                
                foodX = 25 + 25 * random.nextInt(24);    //随机生成食物
                foodY = 75 + 25 * random.nextInt(14);
                grade++;
            }
            
        }
        
        //暂停游戏
        if (start == false && dead == false) {

            g.setColor(Color.BLACK);
            g.setFont(new Font("微软雅黑", Font.BOLD, 30));
            g.drawString("按下空格键开始游戏", 180, 250);
        }

        // 是否吃到自己游戏结束
        if (dead) {
            
            g.setColor(Color.BLACK);
            g.setFont(new Font("微软雅黑", Font.BOLD, 30));
            g.drawString("游戏结束,按下空格键开始", 180, 250);

            grade = 0;
            
        }
        
        //死亡后
        if(start == false && dead == true) {
            
            start = true;
            dead = false;
            initGame();    //重新游戏
            
        }
        
        //页面刷新(帧)
        repaint();
        
    }
}

 

  创建Controller包,并创建GameKeyListener.java

  这个是用来对键盘的一些监听操作

package Controller;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class GameKeyListener implements KeyListener{
    
    public String action; 
    public boolean isstart;
    
    public GameKeyListener(String action, boolean isstart) {
        super();
        this.action = action;
        this.isstart = isstart;
    }

    
    /**拿到蛇的操作*/
    public String snakeAction() {
        
        return action;
    }
    
    /**是否开始游戏*/
    public boolean isStart() {
        
        return isstart;
    }
    
    
    
    @Override
    public void keyPressed(KeyEvent e) {
        
        int keyCode = e.getKeyCode();
        
        if (keyCode == KeyEvent.VK_SPACE) { // 按下空格键
            
            isstart = !isstart;    //控制暂停
            
        } else if (keyCode == KeyEvent.VK_LEFT) {

            action = "L";
        } else if (keyCode == KeyEvent.VK_RIGHT) {

            action = "R";
        } else if (keyCode == KeyEvent.VK_UP) {

            action = "U";
        } else if (keyCode == KeyEvent.VK_DOWN) {
            action = "D";
        }
        
        
        
    }
    
    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub
        
    }

}

 

  完成上面的编写,就可以运行了。

  不过运行的时候,好像页面刷新这一块好像有些问题。希望大伙能帮老弟纠正一些!!!

 

 

  

版权声明:本文为huangcan1688原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/huangcan1688/p/11959311.html