javaswing实现贪吃蛇

2021/9/24 14:41:11

本文主要是介绍javaswing实现贪吃蛇,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

游戏窗口类

package com.tcs;

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

public class GamePanel extends JPanel  {
    //绘制面板
    //定义蛇结构
    int length;//蛇的长度
    //蛇横坐标
    int [] snakex=new int[600];//蛇的横坐标
    //蛇的纵坐标
    int [] snakey=new int[600];//蛇的纵坐标
    String fx;//初始方向
    int foodx;//食物的横坐标
    int foody;//食物的纵坐标
    int source;//分数
    int lv; //等级
    int m;//初始速度
    Random random=null;
    //按下空格开始
    boolean isstart;
    //失败判定
    boolean isdel;
    Timer timer=null;

    public GamePanel() {

        //获得焦点事件
        this.setFocusable(true);
        timer=new Timer(500, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(isstart==true && isdel==false){
                    if(foodx==snakex[0]&&foody==snakey[0]){
                        length=length+1;
                        source+=10;
                        random=new Random();
                        foodx=25+25*random.nextInt(34);
                        foody=75+25*random.nextInt(24);
                    }
                    if (length>10){
                        lv=1;
                        m=800;
                    }else if(length>20){
                        lv=2;
                        m=600;
                    }
                    //前一节身体向后一节身体移动
                    for(int i=length-1;i>0;i--){
                        snakex[i]=snakex[i-1];
                        snakey[i]=snakey[i-1];
                        repaint();
                    }
                    //向右
                    if(fx.equals("right")){
                        snakex[0]=snakex[0]+25;
                        //边界判断
                        if(snakex[0]>850){snakex[0]=25;}
                        repaint();
                    }
                    else if(fx.equals("left")){
                        snakex[0]=snakex[0]-25;
                        //边界判断
                        if(snakex[0]<25){snakex[0]=850;}
                        repaint();
                    }
                    else if(fx.equals("up")){
                        snakey[0]=snakey[0]-25;
                        //边界判断
                        if(snakey[0]<75){snakey[0]=650;}
                        repaint();
                    }
                    else if(fx.equals("down")){
                        snakey[0]=snakey[0]+25;
                        //边界判断
                        if(snakey[0]>650){snakey[0]=75;}
                        repaint();
                    }

                    //重画
                    for (int i=1;i<length;i++){
                        if (snakex[0]==snakex[i] && snakey[0]==snakey[i]){
                            isdel=true;
                        }
                    }
                    repaint();
                }
                timer.start();
            }
        });
        init();
        //获得键盘监听事件
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                int i=e.getKeyChar();
                if(i==KeyEvent.VK_SPACE){
                   if(isdel){
                       isdel=false;
                       init();
                   }else {
                       isstart=!isstart;
                   }
                    repaint();
                }
                //上
                if (i==119){
                    fx="up";

                }
                //下
                if (i==115){
                    fx="down";

                }
                //左
                if (i==97){
                    fx="left";

                }
                //右
                if (i==100){
                    fx="right";
                }
            }
        }
        );


    }

    public void init(){
        length=3;
        snakex[0]=100;snakey[0]=100;
        snakex[1]=75; snakey[1]=100;
        snakex[2]=50; snakey[2]=100;
        fx="right";//定义蛇头的初始方向向右
        foodx=300;
        foody=300;
        isstart=false;
        //初始化食物,边界加上游戏界面内的随机一个坐标
        random=new Random();
        foodx=25+25*random.nextInt(34);
        foody=75+25*random.nextInt(24);
        timer.start();
        isdel=false;
        source=0;
        lv=0;
        m=1000;
    }
    @Override
    //游戏中所有的内容都由这个画笔绘画
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);//清屏
        g.setColor(Color.black);
        g.fillRect(25,75,850,650);//游戏界面
        GameDate.food.paintIcon(this,g,foodx,foody);
        g.setColor(Color.black);
        g.setFont(new Font("微软雅黑",Font.BOLD,40));
        g.drawString("成长记录",300,45);
        //分数
        g.setColor(Color.black);
        g.setFont(new Font("微软雅黑",Font.BOLD,18));
        g.drawString("长度:"+length,750,35);
        g.drawString("分数:"+source,750,50);
        g.drawString("等级:"+lv,750,75);
        if (fx.equals("right")){
            GameDate.imageIconKEY_RIGHT.paintIcon(this,g,snakex[0],snakey[0]);
        }else if(fx.equals("left")){
            GameDate.imageIconKEY_LEFT.paintIcon(this,g,snakex[0],snakey[0]);
        }else if(fx.equals("up")){
            GameDate.imageIconKEY_UP.paintIcon(this,g,snakex[0],snakey[0]);
        }else if(fx.equals("down")){
            GameDate.imageIconKEY_DOWN.paintIcon(this,g,snakex[0],snakey[0]);
        }

        for (int i=1;i<length;i++){
            //画蛇的身体
            GameDate.body.paintIcon(this,g,snakex[i],snakey[i]);
        }
        if (!isstart){
            g.setColor(Color.white);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("按下空格开始游戏",300,300);
        }
        if(isdel){
            g.setColor(Color.red);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("按下空格重新开始游戏",300,300);
        }
    }




}

获取图片的类

package com.tcs;

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

public class GameDate {


    public static final URL KEY_UP =GameDate.class.getResource("20210609002744177.png");
    public  static ImageIcon imageIconKEY_UP=new ImageIcon(KEY_UP);
    public static final URL KEY_DOWN =GameDate.class.getResource("20210609002803353.png");
    public  static ImageIcon imageIconKEY_DOWN=new ImageIcon(KEY_DOWN);
    public static final URL KEY_RIGHT =GameDate.class.getResource("20210609002818339.png");
    public static ImageIcon imageIconKEY_RIGHT=new ImageIcon(KEY_RIGHT);
    public static final URL KEY_LEFT =GameDate.class.getResource("20210609002812556.png");
    public static ImageIcon imageIconKEY_LEFT=new ImageIcon(KEY_LEFT);
    public  static final URL FOOD=GameDate.class.getResource("20210609002727267.png");
    public static ImageIcon food=new ImageIcon(FOOD);
    public static final URL SNAKE_BODY=GameDate.class.getResource("20210609002708755.png");
    static ImageIcon body=new ImageIcon(SNAKE_BODY);
}


这篇关于javaswing实现贪吃蛇的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程