Java 小游戏之华容道

本文介绍了使用Java编程实现经典益智游戏华容道的详细过程,包括游戏规则的解析、棋盘的设计、滑动块的逻辑处理以及用户交互功能的实现。通过实例代码讲解如何运用Java进行游戏开发,提升编程技巧。
摘要由CSDN通过智能技术生成

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


public class Hua_Rong_Road extends JFrame implements MouseListener,KeyListener,ActionListener {
private static final int ERROR_MESSAGE = 0;
private static final int WARNING_MESSAGE = 0;
int cnt=0;
Persion person[]=new Persion[10];//定义一个十个人的数组,

//定义按钮按钮
JButton left,right,above,below; 
JButton restart=new JButton("重新开始");
JButton about=new JButton("游戏背景");
JButton help=new JButton("游戏帮助");
JButton mouse=new JButton("鼠标操作");
JButton key=new JButton("键盘操作");
JButton message=new JButton("当前步数:"+cnt);
JButton begin=new JButton("开始游戏");
JButton star=new JButton();
//定义数组,用来放各种人物形象。
String name[]={"曹操","关羽","张飞","黄忠","马超","赵云","兵","兵","兵","兵"};


public Hua_Rong_Road(){
	//JOptionPane.showMessageDialog(this, "开始游戏前,请先阅读下方的游戏帮助,游戏背景可自行了解.");
	init();
	//定义窗体的各种参数如大小位置,可见与否等。
	//setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	setBounds(200,200,640,800);//设置窗体初始位置以及大小的一个函数
	setVisible(true);//窗口可见
	//person[9].requestFocus();//获取焦点需要卸载setVisible后面才行
	validate();//使用validate方法是容器再次布置其组件,确保布局有效
}

//初始化,在容器中加入按钮等工具
public void init(){
	
	setLayout(null);
	message.setBackground(Color.ORANGE);
	
	//定义重新开始按钮,大小,位置和监听器事件。
	add(restart);
	restart.setBounds(120, 640, 100, 50);
	//restart.setBackground(Color.RED);
	restart.addActionListener(this);
	
	//定义游戏帮助按钮,大小,位置和监听器事件。
	add(about);
	about.addActionListener(this);
	about.setBounds(250,640,100,50);
	
	//定义鼠标操作按钮,大小,位置和监听器事件。
	add(mouse);
	mouse.setBounds(280,40,100,50);
	mouse.addActionListener(this);
	
	//定义键盘操作按钮,大小,位置和监听器事件。
	add(key);
	key.setBounds(400, 40, 100, 50);
	key.addActionListener(this);
	
	//定义 游戏帮助按钮,大小,位置和监听器事件。
	add(help);
	help.setBounds(380, 640, 100, 50);
	help.addActionListener(this);
	
	//定义游戏背景,大小,位置和监听器事件。
	add(message);
	message.setBounds(110,40,160,50);
	ImageIcon starr=new ImageIcon("F:\\MYJAVA\\Myprogram\\HUARongd\\src\\MYHRD\\HRD.gif");//导入图片
	star.setIcon(starr);
	star.setBounds(108,208,400,4
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值