JAVAJ2ME贪吃蛇游戏的设计

JAVAJ2ME贪吃蛇游戏的设计

1.包含源程序,数据库脚本。代码和数据库脚本都有详细注释。
2.课题设计仅供参考学习使用,可以在此基础上进行扩展完善

 代码已经上传github,下载地址https://github.com/21503882/snake
开发环境:
Eclipse ,MYSQL,JDK1.8,Tomcat 8.5
涉及技术点:
MVC模式、SpringBoot、Mybatis、Redis、HTML、log4j、druid、Bootstrap、
Semantic UI、Thymeleaf、JavaScript、CSS、JQUERY、Ajax等
适合学习J2EE的一段时间的熟手,代码思路清晰,注解详细,数据库用的是mysql5.1,服务器用的tomcat8.5,JDK版本1.8. 编程软件Eclispe J2EE版本。是典型MVC架构,并且前后台分离
主要功能:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.EventObject;
import javax.swing.*;
import java.io.*;
public class SnakeGame extends JApplet{
//**********************************//
	static JPanel oPnlLeft;			//
	private JPanel oPnlControl;		//
	private JPanel oPnlMsg;			//
	private JButton oBtnNewGame;	//
	private JButton oBtnPlay;		//
	private JButton oBtnAbout;		//
	private JLabel oLblMsg;			//
	private JLabel oLblMsg2;		//
	static JLabel oLblSnakeLen;
	static JLabel oLblSnakeLen2;
//	private Choice oChoice;
	private Container oCon;			//
	private Timer timer;
	private File file;
	private final int iX = 10;		//地图的开始坐标
    private final int iY = 10;		//
	private final int SWIDTH = 16;		//图标的宽度
    private final int iCells = 11;	//地图的列数
    private final int iRows = 18;		//地图的行数
    private final int iBoxW = SWIDTH*iCells;	//地图的宽度
    private final int iBoxH = SWIDTH*iRows;		//地图的高度
    private int iPlayState;//0表示:游戏暂停 ,1表示:游戏正在进行 ,2表示:游戏还没有开始
//**********************************//
	static SnakeList sNakes;
	public void init(){
		iPlayState = 2;
		oCon = getContentPane();
		oCon.setLayout(null);
		setFont(new Font("楷体",Font.BOLD,18));
		GameKeyEvent eKeyEvent = new GameKeyEvent();
		GameBtnEvent eBtnEvent = new GameBtnEvent();
		oPnlLeft = new JPanel();
		oPnlControl = new JPanel();
		oPnlMsg = new JPanel();
		oPnlControl.setLayout(new GridLayout(3, 1, 10, 10));
		oPnlLeft.setLayout(null);
		oPnlMsg.setLayout(new GridLayout(4,1));
		oCon.add(oPnlLeft);
		oCon.add(oPnlControl);
		oCon.add(oPnlMsg);
		oPnlMsg.setBackground(new Color(160, 168, 194));
		oPnlLeft.setBackground(new Color(160, 168, 194));
		oPnlMsg.setBounds(205, 10, 85, 120);
		oPnlControl.setBounds(205, 190, 85, 100);
		oPnlLeft.setBounds(10, 10, 176, 288);
		oBtnNewGame = new JButton("新游戏");
		oBtnPlay = new JButton("暂  停");
		oBtnAbout = new JButton("关  于");
		oLblMsg = new JLabel("小蛇当前长度:");
		oLblMsg2 = new JLabel("小蛇纪录长度:");
		file = new File("sankegame.db");
		oLblSnakeLen = new JLabel("3",JLabel.CENTER);
		oLblSnakeLen2 = new JLabel(readfile(),JLabel.CENTER);//change
		oBtnPlay.setEnabled(false);
        oBtnNewGame.addActionListener(eBtnEvent);
        oBtnPlay.addActionListener(eBtnEvent);
        oBtnAbout.addActionListener(eBtnEvent);
        oBtnNewGame.setFocusable(false);
        oBtnPlay.setFocusable(false);
        oBtnAbout.setFocusable(false);
		oPnlControl.add(oBtnNewGame);
		oPnlControl.add(oBtnPlay);
		oPnlControl.add(oBtnAbout);
		oPnlMsg.add(oLblMsg);
		oPnlMsg.add(oLblSnakeLen);
		oPnlMsg.add(oLblMsg2);
		oPnlMsg.add(oLblSnakeLen2);
		addKeyListener(eKeyEvent);
	}
    public class GameKeyEvent extends KeyAdapter{
        public void keyPressed(KeyEvent e){
			if(e.getKeyCode()>36&&e.getKeyCode()<41)
				if((e.getKeyCode()-sNakes.getHeading()!=2)&&(e.getKeyCode()-sNakes.getHeading()!=-2))
					sNakes.changeHeading(e.getKeyCode());
		}
	}
    private class GameBtnEvent implements ActionListener{
        public void actionPerformed(ActionEvent e){
            requestFocus();
            Object oSrc = e.getSource();
            if(oSrc == oBtnNewGame){
                oBtnPlay.setText("暂  停");
				oLblSnakeLen.setText("3");
				if(sNakes!=null){
					sNakes.hide();
				}
                if(timer == null){
                    timer = new Timer(500/2, new GameTimeEvent());
				}
                else
                if(timer.isRunning()){
                    timer.stop();
					sNakes.Food.hide();
					sNakes.Food = null;
					sNakes.hide();
				}
				Snake.setImTouch();
				sNakes = new SnakeList();
				sNakes.show();
				if(sNakes.Food==null){
					sNakes.Food = sNakes.createSnake();
				}
                timer.start();
                iPlayState = 0;
                oBtnPlay.setEnabled(true);
                return;
            }
            if(oSrc == oBtnPlay){
                if(iPlayState == 1){
                    iPlayState = 0;
                    oBtnPlay.setText("暂  停");
                    if(!timer.isRunning())
                        timer.start();
                } else
                if(iPlayState == 0){
                    iPlayState = 1;
                    oBtnPlay.setText("开  始");
                    if(timer.isRunning())
                        timer.stop();
                }
                return;
            }
            if(oSrc == oBtnAbout){
                JOptionPane.showMessageDialog(null,"制 作 :  黄军\nEmail: huangjun@163.com");
                return;
            } 
			else
                return;
        }
    }
	private class GameTimeEvent implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(Snake.getTouch()==true||sNakes.isHickSelf()==true){//撞到	墙、自己、鸟
				sNakes.Food.hide();
				sNakes.Food = null;
				timer.stop();
				timer = null;
				oBtnPlay.setEnabled(false);
				String strl = new String();
				strl = readfile();
				if(oLblSnakeLen.getText().length()==strl.length())
					if(oLblSnakeLen.getText().compareTo(readfile())<=0)
						JOptionPane.showMessageDialog(null, "死翘翘喽~~~~继续加油哦^_^");
					else
					{
						writefile();
						oLblSnakeLen2.setText(Integer.toString(sNakes.iLen));
						JOptionPane.showMessageDialog(null, "You are the record breaker !\n     please leave you name\n                     ^_^");
					}
				else if(oLblSnakeLen.getText().length()<strl.length())
						JOptionPane.showMessageDialog(null, "死翘翘喽~~~~继续加油哦^_^");
				else
				{
					writefile();
					oLblSnakeLen2.setText(Integer.toString(sNakes.iLen));
					JOptionPane.showMessageDialog(null, "You are the record breaker !\n     please leave you name\n                     ^_^");
				}
/*



				if(oLblSnakeLen.getText().length()>readfile().length)
				{
					writefile();
					oLblSnakeLen2.setText(Integer.toString(sNakes.iLen));
					JOptionPane.showMessageDialog(null, "You are the record breaker !\n     please leave you name\n                     ^_^");
			
				}
				else if(oLblSnakeLen.getText().compareTo(readfile())>0){
					writefile();
					oLblSnakeLen2.setText(Integer.toString(sNakes.iLen));
					JOptionPane.showMessageDialog(null, "You are the record breaker !\n     please leave you name\n                     ^_^");
				}
				else
					JOptionPane.showMessageDialog(null, "死翘翘喽~~~~继续加油哦^_^");

*/
				return;
			}
			else{
				sNakes.move();
			}
        }
    }
	public void stop(){
        if(timer != null){
            if(timer.isRepeats())
                timer.stop();
            timer = null;
        }
    }
	public void writefile(){
		String str = Integer.toString(sNakes.iLen);
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter("sankegame.db"));
			out.write(str);
			out.close();
		} catch (IOException e) {
		}
	}
	public String readfile(){//change it to int
		String str = new String();
		try {
			BufferedReader in = new BufferedReader(new FileReader("sankegame.db"));
			str = in.readLine();
			in.close();
			System.out.println(str);
		} catch (IOException e) {
		}
		return str;//``````````
	}


	public static void main(String[] args){
		SnakeGame oSnakeGame = new SnakeGame(); 
		JFrame frame = new JFrame("贪食蛇");
		frame.getContentPane().add(oSnakeGame);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		oSnakeGame.init();
		oSnakeGame.start();
		frame.setSize(310, 340);
		frame.setLocation(300,200);
		frame.setVisible(true);
		frame.setResizable(false);
	}
}

 代码已经上传github,下载地址https://github.com/21503882/snake

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值