连连看游戏地图界面 (MapUI) 类实现详解
该代码为连连看游戏的 MapUI 类,主要实现了游戏地图的显示、刷新、连连消除、炸弹消除、提示等功能。其中有一些方法如 paint()、validMap()、findNext()、earse()、bomb() 等实现了连连消除的核心逻辑。同时,该类还与计分动画、时钟动画、游戏界面等其他类进行了交互,实现了完整的游戏功能。
package lianliankan.map;
import lianliankan.ClockAnimate;
import lianliankan.Interface;
import lianliankan.ScoreAnimate;
import lianliankan.Setting;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import lianliankan.*;
public class MapUI
extends JPanel
implements ActionListener, Runnable {
private Map map;
private JButton[] dots;
private Point lastPoint = new Point(0, 0); // 上一个点的坐标
private boolean isSelected = false; // 是否已经选择了一个点
private int score = 0; // 记录用户的得分
private ClockAnimate clockAnimate; // 同步显示时钟
//AnimateDelete animate; // 动画
JButton goTop10;
private ScoreAnimate scoreAnimate;
int stepScore = 0; //
int limitTime = 0;
private boolean isPlaying = false; // 当前是否正在进行游戏
public MapUI(Map map, JButton[] dots) {
this.map = map;
this.dots = dots;
//animate = new AnimateDelete(dots);
GridLayout gridLayout = new GridLayout();
this.setLayout(gridLayout);
gridLayout.setRows(Setting.ROW);
gridLayout.setColumns(Setting.COLUMN);
gridLayout.setHgap(2);
gridLayout.setVgap(2);
this.setLayout(gridLayout);
this.setBackground(Interface.DarkColor);
for (int row = 0; row < Setting.ROW; row++) {
for (int col = 0; col < Setting.COLUMN; col++) {
int index = row * Setting.COLUMN + col;
dots[index].addActionListener(this);
this.add(dots[index]);
}
}
}
public void setMap(Map map) {
this.map = map;
}
/**
* 设置排行榜按钮
*
*/
public void setTop10Button(JButton goTop10) {
this.goTop10 = goTop10;
}
/**
* 绘制地图
*/
private void paint() {
for (int row = 0; row < Setting.ROW; row++) {
for (int col = 0; col < Setting.COLUMN; col++) {
int index = row * Setting.COLUMN + col;
/*message("row = " + row + ", col = " + col + ", index = " + index
+ ", map[][] = " + map.getMap()[row][col]);*/
if (map.getMap()[row][col] > 0) {
dots[index].setIcon(Interface.BlocksIcon[map.getMap()[row][col] - 1]);
dots[index].setEnabled(true);
}
else {
dots[index].setIcon(null);
dots[index].setEnabled(false);
}
}
}
}
public void repaint(Graphics g) {
paint();
}
/**
* 验证地图是否还有可以消除的点
* @param a
* @return
*/
private boolean validMap(Point a) {
if (map.getCount() == 0) {
return true;
}
Line line = new Line(0, new Point(), new Point());
map.setTest(true); // 仅测试
line = map.findNext(a);
int offset = 0;
if (line.direct == 1) { // 找到可以消除的点
return true;
}
else {
return false;
}
}
/**
* 显示得分变化
* @param l
* @param c
*/
private void showScore(int l, int c) {
if (scoreAnimate == null) {
return;
}
scoreAnimate.setScore(l, c);
}
/**
* 刷新地图
*/
public void refresh() {
if (!isPlaying) { // 游戏未开始,不刷新
return;
}
if (map.getCount() == 0) {
Interface.showHint("刷新结束");
}
if (Setting.Sound == 1) {
new Sound(Sound.REFRESH);
}
if (validMap(new Point( -1, -1))) {
score -= Setting.freshScore;
showScore(score - 1, score);
}
else {
showScore(score, score + Setting.freshScore);
score += Setting.freshScore;
}
score -= Setting.freshScore;
showScore(score - 1, score);
map.refresh();
paint();
}
/**
* 消除两个点
* @param a
* @param b
*/
void earse(Point a, Point b) {
//paint();
int offset;
offset = a.x * Setting.COLUMN + a.y;
dots[offset].setIcon(null);
dots[offset].setEnabled(false);
offset = b.x * Setting.COLUMN + b.y;
dots[offset].setIcon(null);
dots[offset].setEnabled(false);
// 检查是否游戏结束
if (map.getCount() == 0) {
int remainTime = clockAnimate.getUsedTime();
message("remainTime = " + remainTime);
if (remainTime > 0) {
showScore(score, score + remainTime * Setting.timeScore);
score += remainTime * Setting.timeScore;
}
isPlaying = false;
stop();
Pass frame=new Pass();
frame.setTitle("过关啦");
frame.setSize(620,393);
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = screenSize.width;
int screenHeight =screenSize.height;
int x=(screenWidth-frame.getWidth())/2;
int y=(screenHeight-frame.getHeight())/2;
frame.setLocation(x+78,y+21);
frame.setVisible(true);
Interface.showHint("时间剩余" + remainTime + "秒,想看看你的排名吗?");
goTop10.setEnabled(true);
}
else {
//test1(map.getDeleteArray());
}
}
/**
* 寻找下一个可以与当前点配对的点
* @param a
*/
public void findNext(Point a) {
if (!isPlaying) { // 游戏未开始,不寻找
return;
}
if (map.getCount() == 0) {
Interface.showHint("还找?没了!");
return;
}
Line line = new Line(0, new Point(), new Point());
map.setTest(true); // 仅测试
line = map.findNext(a);
int offset = 0;
if (line.direct == 1) { // 找到可以消除的点
if (Setting.Sound == 1) {
new Sound(Sound.HINT);
}
offset = line.a.x * Setting.COLUMN + line.a.y;
dots[offset].setBorder(Interface.Hint);
offset = line.b.x * Setting.COLUMN + line.b.y;
dots[offset].setBorder(Interface.Hint);
score -= Setting.hintScore;
showScore(score - 1, score);
}
else {
Interface.showHint("找不到,请刷新");
}
}
/**
* 自动消除附近的点
* @param a
* @param showMessage
* @return
*/
public boolean bomb(Point a, boolean showMessage) {
if (!isPlaying) { // 游戏未开始,不消除
return false;
}
if (map.getCount() == 0) {
Interface.showHint("你炸昏了头吧,没了!");
return false;
}
Line line = new Line(0, new Point(), new Point());
map.setTest(false);
line = map.findNext(a);
int offset = 0;
if (line.direct == 1) { // 找到可以消除的点
if (Setting.Sound == 1) {
new Sound(Sound.BOMB);
}
offset = line.a.x * Setting.COLUMN + line.a.y;
dots[offset].setBorder(Interface.unSelected);
offset = line.b.x * Setting.COLUMN + line.b.y;
dots[offset].setBorder(Interface.unSelected);
map.earse(line.a, line.b);
earse(line.a, line.b);
score -= Setting.bombScore;
showScore(score - 1, score);
return true;
}
else {
if (showMessage) {
Interface.showHint("炸弹用不了,请刷新");
}
return false;
}
}
private void message(String str) {
//System.out.println(str);
Interface.showHint(str);
}
/**
* 设置计分动画
* @param score
*/
public void setScore(ScoreAnimate score) {
this.scoreAnimate = score;
}
/**
* 设置时钟动画
* @param clock
*/
public void setClock(ClockAnimate clock) {
this.clockAnimate = clock;
}
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
int offset = Integer.parseInt(button.getActionCommand());
int row, col;
row = Math.round(offset / Setting.COLUMN);
col = offset - row * Setting.COLUMN;
if (map.getMap()[row][col] < 1) { // 点击的是没有图片的按钮
return;
}
if (Setting.Sound == 1) {
new Sound(Sound.SELECT);
}
if (isSelected) {
message("上次已经选择了一个点");
message("上次选择点的坐标为: " + lastPoint.x + ", " + lastPoint.y +
"值为:" + map.getMap()[lastPoint.x][lastPoint.y] +
" 位移为: " + (lastPoint.x * Setting.COLUMN + lastPoint.y));
if (lastPoint.x == row && lastPoint.y == col) { // 点击的是上次选择的点
message("这次选择的点和上次的是同一点,取消选择状态");
button.setBorder(Interface.unSelected);
isSelected = false;
}
else {
// 判断是否可以消除
message("这次选择的点和上次的点并不相同");
Point current = new Point(row, col);
message("这次选择的点的坐标为:" + row + ", " + col + " 值为:" +
map.getMap()[row][col] +
" 位移为:" + (row * Setting.COLUMN + col));
map.setTest(false);
if (map.test(lastPoint, current)) {
message("消除成功");
// 视觉效果
dots[row * Setting.COLUMN + col].setBorder(Interface.unSelected);
map.earse(current, lastPoint);
earse(current, lastPoint);
dots[lastPoint.x * Setting.COLUMN +
lastPoint.y].setBorder(Interface.unSelected);
lastPoint = new Point(0, 0);
isSelected = false;
showScore(score, score + Setting.correctScore + stepScore);
score += Setting.correctScore + stepScore;
if (Setting.Sound == 1) {
new Sound(Sound.EARSE);
}
}
else {
message("出错啦,重新再找和现在的点配对");
dots[lastPoint.x * Setting.COLUMN +
lastPoint.y].setBorder(Interface.unSelected);
button.setBorder(Interface.Selected);
lastPoint.x = row;
lastPoint.y = col;
isSelected = true;
score -= Setting.wrongScore;
showScore(score - 1, score);
}
}
}
else {
message("重新选择点");
message("当前点坐标为:" + row + ", " + col + "值为:" +
map.getMap()[row][col] +
"位移为:" + (row * Setting.COLUMN + col));
button.setBorder(Interface.Selected);
lastPoint.x = row;
lastPoint.y = col;
isSelected = true;
}
}
/**
* 编码当前游戏分数
* @param score
* @return
*/
public String encode() {
if (score < 0) {
Interface.showHint("负分啊,大哥!继续努力吧");
}
int level, f1, f2;
level = Setting.LevelIndex;
f1 = (score - 102) * (score - 102);
f2 = (1978 - score) * (1978 - score);
return "s=" + score + "&l=" + level + "&f1=" + f1 + "&f2=" + f2;
}
public void start() {
goTop10.setEnabled(false);
isPlaying = true;
limitTime = map.getCount() * Setting.limitScore;
message("欢迎使用JAVA连连看");
paint();
}
public void run() {
}
public void stop() {
clockAnimate.stop();
}
}
原文地址: https://www.cveoy.top/t/topic/n3vJ 著作权归作者所有。请勿转载和采集!