欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

GUI编程之贪吃蛇小游戏

程序员文章站 2022-07-10 08:21:58
本篇实现的是贪吃蛇小游戏,使用的是Java语言,GUI编程。//启动类import snake.GamePanel;import javax.swing.*;import java.awt.*;public class Start extends JFrame{ public Start() { this.setBounds(10, 10, 900, 720); setResizable(false);//窗口大小不可变 thi...

本篇实现的是贪吃蛇小游戏,使用的是Java语言,GUI编程。

//启动类

import snake.GamePanel;

import javax.swing.*;
import java.awt.*;

public class Start extends JFrame{

    public Start() {

        this.setBounds(10, 10, 900, 720);
        setResizable(false);//窗口大小不可变
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
        //正常游戏界面都应该在面上!
        Game panel = new Game();
        Container container = this.getContentPane();
        container.add(panel);
    }
//主类

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

//绘制游戏界面
//初始化、画笔JPanel、监听:键盘;事件监听(吃食物)
public class Game extends JPanel implements KeyListener, ActionListener {

    //属性
    String dir;//蛇头方向
    int[] snake_x =new int[600];//蛇位置
    int[] snake_y =new int[600];
    int food_x;//食物位置
    int food_y;
    Random random = new Random();
    int length ;
    int score;
    //游戏状态
    Boolean isstart =false;
    Boolean isfail =false;
    //定时器
    Timer timer = new Timer(100, this);//100ms执行一次定时器



    //无参构造
    public Game() {
        init();
        this.setFocusable(true);
        this.addKeyListener(this);
        timer.start();
    }

    //游戏开始和重新开始需要初始化,重新开始小蛇位置和分数等,还原到原始状态
    public void init() {

        //蛇头初始位置
        snake_x[0] = 100;snake_y[0] = 100;
        snake_x[1] = 75;snake_y[1] = 100;
        snake_x[2] = 50;snake_y[2] = 100;
        //初始方向
        dir = "R";
        //随机初始食物位置
        food_x=  25+25*random.nextInt(34);
        food_y= 75+25*random.nextInt(24);
        length=3;
        //绘制计分器
        score=0;

//        timer.start();

    }

    //绘制面板 重写JComponet方法
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        setBackground(Color.WHITE);
        Data.header.paintIcon(this,g,25,11);
        g.fillRect(25,75,850,600);
        //画蛇头
        switch (dir) {
            case"R":
                Data.right.paintIcon(this,g,snake_x[0],snake_y[0]);
                break;
            case"L":
                Data.left.paintIcon(this,g,snake_x[0],snake_y[0]);
                break;
            case"U":
                Data.up.paintIcon(this,g,snake_x[0],snake_y[0]);
                break;
            case"D":
                Data.down.paintIcon(this,g,snake_x[0],snake_y[0]);
                break;
        }
        //画食物
        Data.food.paintIcon(this,g,food_x,food_y);
        //画蛇身
        for (int i = 1; i <length ; i++) {
            Data.body.paintIcon(this,g,snake_x[i],snake_y[i]);
        }
        //绘制计分器
        g.setColor(Color.WHITE);
        g.setFont(new Font("微软雅黑",Font.BOLD,18));
        g.drawString("分数"+(length-3)*10,750,50);

        //绘制开始提示问题
        if (isstart == false) {
            g.setColor(Color.WHITE);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("按下空格开始游戏",300,300);
        }
        if (isfail) {
            g.setColor(Color.RED);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("游戏失败,按下空格重新开始",300,300);
        }



    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (isstart == true && isfail == false) {
            //吃食物
            if (snake_x[0] == food_x && snake_y[0]==food_y){
                length++;
                //再次随机生成食物
                food_x=  25+25*random.nextInt(34);
                food_y= 75+25*random.nextInt(24);
            }
            //小蛇移动
            for (int i = length-1; i >0 ; i--) {
                snake_x[i]=snake_x[i-1];
                snake_y[i]=snake_y[i-1];
            }
            switch (dir) {
                case "U":
                    snake_y[0]-=25;
                    if(snake_y[0]<75){
                        snake_y[0]=650;
                    }
                    break;
                case "D":
                    snake_y[0]+=25;
                    if(snake_y[0]>650){
                        snake_y[0]=75;
                    }
                    break;
                case "L":
                    snake_x[0]-=25;
                    if(snake_x[0]<25){
                        snake_x[0]=850;
                    }
                    break;
                case "R":
                    snake_x[0]+=25;
                    if(snake_x[0]>850){
                        snake_x[0]=25;
                    }
                    break;
            }
            repaint();

        }
        //小蛇移动
        if(isstart)
        //判断失败
        for (int i = 1; i <length ; i++) {
            if(snake_x[0] == snake_x[i] && snake_y[0] ==snake_y[i]){
                isfail=true;
            }

        }
        timer.start();



    }



    @Override
    public void keyPressed(KeyEvent e) {
        //获得按键码
        int keyCode = e.getKeyCode();
        switch (keyCode){
            case KeyEvent.VK_SPACE:
                if(isfail){
                    isfail=false;
                    init();
                } else {
                    isstart =! isstart;
                }
                repaint();//重新画图
                break;
            case KeyEvent.VK_UP:
                dir = "U";
                break;
            case KeyEvent.VK_DOWN:
                dir = "D";
                break;
            case KeyEvent.VK_LEFT:
                dir = "L";
                break;
            case KeyEvent.VK_RIGHT:
                dir = "R";
                break;
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
    @Override
    public void keyTyped(KeyEvent e) {

    }
}
//数据类

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

/**
 * 功能:加载图片资源,在Game类中加载到画板上
 * */
public class Data {

    //头部图片
    public static URL headerURL = Data.class.getResource("statics/header.png");
    public static ImageIcon header= new ImageIcon(headerURL);
    //蛇头上下左右
    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 leftURL = Data.class.getResource("statics/left.png");
    public static ImageIcon left= new ImageIcon(leftURL);
    public static URL rightURL = Data.class.getResource("statics/right.png");
    public static ImageIcon right= new ImageIcon(rightURL);
    //蛇身
    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);



}

运行结果:

GUI编程之贪吃蛇小游戏

 

注:程序中所有用到图片素材在主页上传的资源中,粉丝即可下载,无需积分! 

 

 

 

本文地址:https://blog.csdn.net/qq_38723455/article/details/112007463

相关标签: java 游戏 gui