java小游戏——走迷宫

main类

public class Main {
	public static void main(String[] args){
		new PreviousSynopsis();
	}
}

PreviousSynopsis类


//导入要用到的包中的类接口
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class PreviousSynopsis extends JFrame implements ActionListener//实现接口
{
	final int x=360,y=50,width=1200,height=855;//定义常量
	JButton start;
	ImageIcon  image1,image2;
	JLabel  backGound;
	public PreviousSynopsis(){
		this.setTitle("屋里灯还黑着");
		this.setLayout(null);
		image1=new ImageIcon("src/cover1_picture/封面.jpg");
		image2=new ImageIcon("src/cover1_picture/play.jpg");
		Image image3=(new ImageIcon("src/cover2_picture/小人儿.jpg")).getImage();
		this.setIconImage(image3);
		this.setLocation(x,y);
		this.setSize(width, height);
		start=new JButton(image2);
		start.setBounds(523,700,178,70);
		start.addActionListener(this);//把监听器向事件源注册
		backGound = new JLabel(image1);
		backGound.setBounds(0, 0, width,height);
		backGound.add(start);
		this.add(backGound);
		this.setResizable(false);//设置窗口大小为不可改变
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//用户单击窗口的关闭按钮时程序执行的操作
		this.setVisible(true);
	}


	public void actionPerformed(ActionEvent e){
		if((JButton)e.getSource()==start){
			new Choices();
			this.dispose();//关闭窗口,并释放资源
		}
	}
}

Choices类


import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Choices extends JFrame  implements ActionListener//实现接口
{
	final int X=463;int Y=420;//定义常量
	JButton[] btn=new  JButton[5]; 
	public Choices(){
		super("一个 两个");
		Image im=(new ImageIcon("src/cover2_picture/小人儿.jpg")).getImage();
		this.setIconImage(im);
		this.setLocation(360,38);
		this.setSize(1165, 855);
		JPanel pa  = new JPanel();
		pa.setLayout(null);
		JLabel  jab = new JLabel(new ImageIcon("src/cover2_picture/封面2.jpg"));
		jab.setBounds(0, 0, 1140, 810);
				btn[0]=new JButton(new ImageIcon("./src/cover2_picture/开始.JPG"));
		btn[0].setBounds(390,385,350,78);
		jab.add(btn[0]);


		for( int i = 1 ; i < 4; i ++ ){
			btn[ i ] = new JButton(new ImageIcon("src/cover2_picture/"+(i+1)+".JPG"));
			btn[ i ].setBounds(X, Y+i*80, 215, 60);
			jab.add(btn[i]);		    
		}
		btn[4]=new JButton(new ImageIcon("src/cover2_picture/5.JPG"));
		btn[4].setBounds(500, 730, 118, 58);
		jab.add(btn[4]);
		pa.add(jab);
		for(int i=0;i<5;i++)
			btn[i].addActionListener(this);
		this.add(pa);
		this.setResizable(false);//设置窗口的大小为不可改变
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//单击窗口的关闭按钮,结束程序
		this.setVisible(true);
		
	}
	public void actionPerformed(ActionEvent e) {
		if((JButton)e.getSource()==btn[0]){
			new Reality();
			this.dispose();//释放窗口
		}else if((JButton)e.getSource()==btn[1]) {
			new Strategy();
			this.dispose();
		}else if((JButton)e.getSource()==btn[4]) {
			int n=JOptionPane.showConfirmDialog(this, "是否要退出游戏?","退出",JOptionPane.YES_NO_OPTION);
			if(n==JOptionPane.YES_OPTION) {
				System.exit(0);
			}
		}
	}
}

Destiny类


public class Destiny {
	int row=19,line=32;
	public int[][] path = {
			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
			{1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0},
			{1,1,0,1,0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,1,0},
			{0,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0,0,1,0,0,1,1,1,0,0,0,0,1,0},
			{0,1,0,0,0,0,0,0,1,0,0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{
	public int x,y;
	public int width, height;
	public Diamond(int x, int y, int w, int h) {
		this.x=x;
		this.y=y;
		this.width=w;
		this.height=h;
	}
}

MyKey类


import java.awt.event.KeyEvent;

import javax.swing.JOptionPane;

import java.awt.event.KeyAdapter;;

//继承键盘事件适配器类
public class MyKey extends KeyAdapter {
	//声明对象
	Reality game;
	//调参
	MyKey(Reality g){
		//赋值给game
		this.game=g;
	}
	public void keyPressed(KeyEvent e) {
		//返回按下字符的键码值
		switch (e.getKeyCode()) {
		case KeyEvent.VK_RIGHT:
			game.xiaobing.right=true;
			break;
		case KeyEvent.VK_LEFT:
			game.xiaobing.left=true;
			break;
		case KeyEvent.VK_UP:
			game.xiaobing.up=true;
			break;
		case KeyEvent.VK_DOWN:
			game.xiaobing.down=true;
			break;
		case KeyEvent.VK_SPACE:
			game.operation=false;
			suspend();
			break;
		default:break;
		}
		if(game.xiaobing.up||game.xiaobing.down||game.xiaobing.left||game.xiaobing.right) {
			game.xiaobing.move(game.xiaobing.up,game.xiaobing.down,game.xiaobing.left,game.xiaobing.right);
		}
	}
	public void keyReleased(KeyEvent e) {
		int code=e.getKeyCode();//返回按下字符的键码值
		if (code==KeyEvent.VK_RIGHT) {
			game.xiaobing.right=false;
		} else if (code==KeyEvent.VK_LEFT) {
			game.xiaobing.left=false;
		} else if (code==KeyEvent.VK_UP) {
			game.xiaobing.up=false;
		} else if (code==KeyEvent.VK_DOWN) {
			game.xiaobing.down=false;

		}
	}
	public void suspend() {
		int n=JOptionPane.showConfirmDialog(game, "要走吗","离开",JOptionPane.YES_NO_OPTION);
		if(n==JOptionPane.YES_OPTION) {
			game.endThread=false;
			game.dispose();
			new Choices();
		}
		else if(n==JOptionPane.NO_OPTION) {
			game.operation=true; 
		}
	}
}

 

Obstacle类

public class Obstacle {
	public int x,y;
	public int width, height;
	public Obstacle(int x, int y, int w, int h) {
		this.x=x;
		this.y=y;
		this.width=w;
		this.height=h;
	}
}

Reality类


import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Reality extends JFrame{
	final int windowWidth=965,windowHight=700;
	JPanel panel;
	Destiny destiny=new Destiny();
	Obstacle obstacle;
	List<Obstacle> obstacleList=new ArrayList<Obstacle>();//创建容器
	Diamond diamond;
	List<Diamond> diamondList=new ArrayList<Diamond>();
	XiaoBing xiaobing=new XiaoBing(this);
	JLabel xiaobingbing,time,scoreLabel;
	int k=0;
	int x=xiaobing.x,y=xiaobing.y;
	int[][] path=destiny.getPath();
	JLabel[][] maze=new JLabel[destiny.row][destiny.line];
	Thread1 thread= new Thread1();
	ImageIcon way;
	public Boolean operation =true,endThread=true;
	Reality(){
		new Thread1().start();
		this.setTitle("回忆散落、紧握");
		this.setSize(windowWidth, windowHight);
		this.setLocationRelativeTo(null);//设置窗口显示居中
		Image image3=(new ImageIcon("./src/cover2_picture/小人儿.jpg")).getImage();
		this.setIconImage(image3);
		module();//调用方法
		this.setFocusable(true);//将控件设置成可获取焦点状态,防止键盘监听获取不到焦点
		this.addKeyListener(new MyKey(this));//匿名调用,把监听者向事件源注册
		this.setResizable(false);//设置窗口大小为不可改变
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	//声明方法
	private void module() {
		panel=new JPanel();
		panel.setLayout(null);//取消页面设置
		panel.setBackground(new Color(255,228,196));
		panel.setBounds(0, 0, windowWidth, windowWidth);
		way=new ImageIcon("./src/Map/路.jpg");
		ImageIcon wall=new ImageIcon("./src/Map/墙.jpg");
		ImageIcon diamondImage=new ImageIcon("./src/GameGrounds/冰.gif");
		ImageIcon image=null;//让照片为空
		this.xiaobingbing=xiaobing.xiaobingbing;
		this.xiaobingbing.setSize(xiaobing.width,xiaobing.height);
		this.xiaobingbing.setLocation(x,y);
		time = new JLabel();
		time.setFont(new Font("宋体", Font.PLAIN, 50));
		time.setBounds(50, 580, 300, 50);
		scoreLabel=new JLabel("获得的钻石数:0");
		scoreLabel.setFont(new Font("宋体", Font.PLAIN, 50));
		scoreLabel.setBounds(450, 580, 450, 50);
		panel.add(scoreLabel);
		panel.add(time);
		panel.add(xiaobingbing);
		path=destiny.getPath();
		while(k<10) {
			changceWay();
		}
		for(int j=0;j<destiny.line;j++) {
			for(int i=0;i<destiny.row;i++) {
				if(path[i][j]==0) {
					image=wall;
					obstacle=new Obstacle(j*30,i*30+5,30,30);//把墙封装
					obstacleList.add(obstacle);//把封装好的加到容器上
				}
				else if(path[i][j]==1) {
					image=way;
				}else if(path[i][j]==2) {
					image=diamondImage;
					diamond=new Diamond(j*30,i*30+5,30,30);
					diamondList.add(diamond);
				}
				maze[i][j]=new JLabel(image);
				maze[i][j].setBounds(j*30, i*30, 30, 30);
				panel.add(maze[i][j]);
			}
		}
		this.add(panel);
	}
	private void changceWay() {
		int diamond_y=(int)(Math.random()*destiny.line);
		int diamond_x=(int)(Math.random()*destiny.row);
		if(path[diamond_x][diamond_y]==1&&((diamond_x!=22&&diamond_y!=4)
				||(diamond_x!=21&&diamond_y!=8)||(diamond_x!=23&&diamond_y!=8))) {
			path[diamond_x][diamond_y]=2;
			k++;
		}
	}
	class Thread1 extends Thread{
		int minute=0,second=0;
		public void run() {
			while(endThread) {          //结束
				if(operation) {         //管暂停
					second++;
					if(second>=60) {
						minute++;
						second=0;
					}
					if(minute>=5) {
						xiaobing.ending(2);
					}
					try {
						Thread.sleep(1000);
					} catch (Exception e) {}
					time.setText(minute+"分"+second+"秒");
				}
			}
		}
	}
}

Strategy类


import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Strategy extends JFrame implements ActionListener{
	JButton jb;
	Strategy(){
		super("攻略");
		this.setBounds(750, 123, 400, 355);
		Image im=(new ImageIcon("./src/cover2_picture/小人儿.jpg")).getImage();
		this.setIconImage(im);//设置窗口显示图标
		ImageIcon im2=new ImageIcon("./src/help_picture/操作说明.jpg");
		JLabel jlab=new JLabel(im2);
		JPanel jp=new JPanel();
		jb=new JButton("准备好了");
		jb.setBounds(130,250,100,60);
		jb.addActionListener(this);
		jp.setBounds(0, 0, 400, 355);
		jlab.setBounds(0, 0, 400, 355);
		jlab.add(jb);
		jp.add(jlab);
		this.add(jp);
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);//单击窗口的关闭按钮,结束程序
		this.setVisible(true);//显示窗口
	}
	
	
	public void actionPerformed(ActionEvent e) {
		if((JButton)e.getSource()==jb) {
			new Choices();     //匿名调用
			this.dispose();
		}
		
	}

}	

XiaoBing类


import java.awt.Rectangle;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class XiaoBing {
	int x=6,y=36;
	int c,k;            //中间变量
	int Xspeed,Yspeed;
	int score=0;
	final int width=25,height=38;
	JLabel xiaobingbing;
	public boolean up=false,down=false,right=false,left=false;
	Reality game;//声明对象
	Rectangle people1;
	Diamond diamond;
	XiaoBing(Reality g){
		//赋值给game
		game=g;
		ImageIcon image=new ImageIcon("./src/Person/xiaobingbing.gif");
		xiaobingbing=new JLabel(image);
		xiaobingbing.setSize(width,height);
	}
	public void move(boolean up,boolean down,boolean left,boolean right) {
		Yspeed=5;
		if(up) {
			if(!collision("up")) {
				y-=Yspeed;
				xiaobingbing.setLocation(x,y);
			}
			Yspeed=5;
		}
		if(down) {
			if(!collision("down")) {
				y+=Yspeed;
				xiaobingbing.setLocation(x,y);
			}
			Yspeed=5;
		}
		if(left) {
			if(!collision("left")&&x>-9){
				x-=Xspeed;
				xiaobingbing.setLocation(x,y);
			}
			Xspeed=5;
		}
		if(right) {
			if(collision("right")) {
				Xspeed=0;
			}else if(x<941){
				if(x>926&&score>=10)ending(1);
				x+=Xspeed;
				xiaobingbing.setLocation(x,y);
			}
			Xspeed=5;
		}
		if(diamondCollision()) {
			game.maze[k/30][c/30].setVisible(false);
			
		}

	}
	//声明一个字符串引用变量
	public boolean collision(String s) {
		people1=new Rectangle(x+5,y+35,10,10);
		Rectangle blick=null;
		for(int i=0;i<game.obstacleList.size();i++) {
			Obstacle obstacle=game.obstacleList.get(i);
			if(s.equals("up")) {
				blick=new Rectangle(obstacle.x+1,obstacle.y+10,obstacle.width,obstacle.height);
			}else if(s.equals("down")) {
				blick = new Rectangle(obstacle.x+1,obstacle.y,obstacle.width,obstacle.height);
			}else if(s.equals("left")) {
				blick = new Rectangle(obstacle.x+5,obstacle.y+5,obstacle.width,obstacle.height);
			}else if(s.equals("right")) {
				blick = new Rectangle(obstacle.x,obstacle.y+5,obstacle.width,obstacle.height);
			}
			if(people1.intersects(blick)) {
				return true;
			}
		}
		return false;
	}
	public Boolean diamondCollision() {
		Rectangle rect=null;
		for(int i=0;i<game.diamondList.size();i++) {
			diamond=game.diamondList.get(i);
			rect=new Rectangle(diamond.x, diamond.y, diamond.width, diamond.height);
			if(people1.intersects(rect)) {
				c=diamond.x;k=diamond.y;
				diamond.x=1;diamond.y=1;
				score++;
				game.scoreLabel.setText("获得的钻石数:"+score);
				return true;
			}
		}
		return false;
	}
	public void ending(int t) {
		game.endThread=false;
		String s="";
		switch(t) {
		case 1:
			s="胜利";
		break;
		case 2:
			s="失败";
			break;
			default:break;
		}
		int n=JOptionPane.showConfirmDialog(game, "您已经"+s+",是否再来一局?",s,JOptionPane.YES_NO_OPTION);
		if(n==JOptionPane.YES_OPTION) {
			game.dispose();
			new Reality();
		}else if(n==JOptionPane.NO_OPTION) {
			game.endThread=false;
			game.dispose();
			new Choices(); 
		}
	}
}

 

  • 4
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值