java贪吃蛇游戏

本文介绍了一个基于Java的贪吃蛇游戏的实现过程,包括游戏界面、控制按钮、分数显示、蛇模型以及菜单系统的构建。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码如下

<span style="font-size:18px;">//贪吃蛇图形界面  
import java.awt.FlowLayout;  
import java.awt.GridLayout;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.util.Observable;  
import java.util.Observer;  
   
import javax.swing.ImageIcon;  
import javax.swing.JButton;  
import javax.swing.JFrame;  
import javax.swing.JLabel;  
import javax.swing.JMenu;  
import javax.swing.JMenuBar;  
import javax.swing.JMenuItem;  
import javax.swing.JOptionPane;  
import javax.swing.JPanel;  
import javax.swing.JTextField;  
import javax.swing.border.TitledBorder;  
import javax.swing.event.CaretEvent;  
import javax.swing.event.CaretListener;  
import javax.swing.event.ChangeEvent;  
import javax.swing.event.ChangeListener;  
   
public class GluttonousSnake extends JFrame implements ActionListener {  
    private JButton upButton, downButton, leftButton, rightButton;//控制方向按钮  
    private JTextField score;//分数  
    private SnakeCanvas snake;//蛇的模型  
   
    public GluttonousSnake() {  
        super("贪吃蛇");//设置标题  
        this.setSize(725, 515);//设置大小  
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);//设置退出按键  
        this.setLocationRelativeTo(null);//设置窗口居中  
        JPanel p = new JPanel();//控制按钮和显示分数面板  
        p.setBorder(new TitledBorder("控制和显示区"));//设置这个面板的标题  
        this.getContentPane().add(p, "East");//设置此面板的位置  
        p.setLayout(new GridLayout(4, 1));//设置此面板的布局方式,为网格布局方式  
        JPanel p2 = new JPanel();//在此面板中添加显示分数的面板  
        p2.setLayout(new FlowLayout());//设置为流布局方式  
        p2.add(new JLabel("得分:"));  
        score = new JTextField("0");  
        score.setEditable(false);  
        p2.add(score);  
        p.add(p2);  
        //添加按钮,有图片的并添加事件监听  
        upButton = new JButton("", new ImageIcon("up.png"));  
        upButton.setActionCommand("up");  
        upButton.addActionListener(this);  
        downButton = new JButton("", new ImageIcon("down.png"));  
        downButton.setActionCommand("down");  
        downButton.addActionListener(this);  
        leftButton = new JButton("", new ImageIcon("left.png"));  
        leftButton.setActionCommand("left");  
        leftButton.addActionListener(this);  
        rightButton = new JButton("", new ImageIcon("right.png"));  
        rightButton.setActionCommand("right");  
        rightButton.addActionListener(this);  
        p.add(upButton);  
        JPanel p1 = new JPanel();  
        p1.setLayout(new GridLayout(1, 2));  
        p1.add(leftButton);  
        p1.add(rightButton);  
        p.add(p1);  
        p.add(downButton);  
   
        addMenu();//添加菜单  
        start();  
        this.setResizable(false);  
        this.setVisible(true);  
   
    }  
   
    private void start() {  
        snake = new SnakeCanvas();  
        this.getContentPane().add(snake);  
    }  
   
    private void addMenu() {  
        JMenuBar menubar = new JMenuBar();  
        this.setJMenuBar(menubar);  
        JMenu game = new JMenu("游戏");  
        JMenu help = new JMenu("帮助");  
        JMenuItem jitemNew = new JMenuItem("新游戏");  
        jitemNew.setActionCommand("new");  
        jitemNew.addActionListener(this);  
        JMenuItem jitemPause = new JMenuItem("暂停");  
        jitemPause.setActionCommand("pause");  
        jitemPause.addActionListener(this);  
        JMenuItem jitemExit = new JMenuItem("退出");  
        jitemExit.setActionCommand("exit");  
        jitemExit.addActionListener(this);  
        game.add(jitemNew);  
        game.add(jitemPause);  
        game.addSeparator();//菜单里设置分隔线  
        game.add(jitemExit);  
        menubar.add(game);  
        menubar.add(help);  
    }  
   
    public static void main(String[] args) {  
        new GluttonousSnake();  
    }  
   
    public void actionPerformed(ActionEvent e) {  
        if (e.getActionCommand().equalsIgnoreCase("exit")) {  
            System.exit(EXIT_ON_CLOSE);  
        }  
        if (e.getSource() instanceof JButton) {  
            if (e.getActionCommand().equalsIgnoreCase("up")) {//响应向上按钮按下事件  
                snake.setDirect(1);//设置蛇头运动方向  
                snake.repaint();//对蛇模型重新画  
                snake.timer.start();//定时器开始  
                return;  
            }  
            if (e.getActionCommand().equalsIgnoreCase("down")) {  
                snake.setDirect(2);  
                snake.repaint();  
                snake.timer.start();  
                return;  
            }  
            if (e.getActionCommand().equalsIgnoreCase("left")) {  
                snake.setDirect(3);  
                snake.repaint();  
                snake.timer.start();  
                return;  
            }  
            if (e.getActionCommand().equalsIgnoreCase("right")) {  
                snake.setDirect(4);  
                snake.repaint();  
                snake.timer.start();  
                return;  
            }  
        }  
        if (e.getSource() instanceof JMenuItem) {  
            if (e.getActionCommand().equalsIgnoreCase("new")) {  
                //this.getContentPane().remove(snake);  
                snake.init();  
                snake.repaint();  
                snake.timer.start();  
            }  
            if (e.getActionCommand().equalsIgnoreCase("pause")) {  
                snake.timer.stop();  
                JOptionPane.showMessageDialog(this, "继续请按“确定”");  
                snake.timer.start();  
            }  
        }  
    }  
   
       
   
   
}</span>

<span style="font-size:18px;">import java.awt.Canvas;  
import java.awt.Color;  
import java.awt.Graphics;  
//蛇运行的地图  
public class MapCanvas extends Canvas{  
   
    public void paint(Graphics g) {  
        g.setColor(Color.red);  
        for(int i=30;i<=450;i+=30){  
            g.drawLine(0,i,450,i);  
        }  
        for(int i=30;i<=450;i+=30){  
            g.drawLine(i,0,i,450);  
        }  
        g.drawLine(0, 0, 450, 0);  
        g.drawLine(0, 450, 450, 450);  
        g.drawLine(0, 0, 0, 450);  
        g.drawLine(450, 0, 450, 450);  
    }  
   
}</span>
效果图


注:详细信息尽在java教程网

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值