最开始想学编程完全是因为游戏,第一次接触的游戏是扫雷。那还是初中在学校机房的时候。作为一个无限接近80后的小角色,那时看见电脑还是一个稀奇货(原谅我没见过世面,在我出生的时候世面就已经挂了....)虽然在那一通乱点,不知道怎么玩但还是被吸引住了。最后终于成了一个网瘾少年... 带着好奇心几经波折最后还是来学了编程。(主要是因为没找到工作(ノへ ̄、)捂脸)
来学的第一个星期还记得是java的awt组件,老师教我们做简易计算器。感到好神奇还认认真真的听了一个星期。以至于到后面工作才发现,尼玛!根本没有用的地方...
最近在看以前的资料发现了,刚学的时候写的一个扫雷小游戏。发现自己还是蛮用心的,还给它配上了动画,背景音乐效果
代码我就只贴一部分了。如果有兴趣的同学可以去下面的github地址下。
package lc.com;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Random;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**简易扫雷*/
public class SweepBySwing {
private static final String SRC_URL = "/Users/mac/IdeaProjects/mineClearance/src/main/resources/";
private static int[][] arr = new int[15][15];
private static int[][] book = new int[15][15];
private static int wipeCount = 0;//消灭的格子数量
private static JButton[][] buttons = new JButton[15][15];
private static JButton button;
private static URL mus;
/**生成扫雷的二维数组*/
/*
* 后续在更新鼠标的右键标记雷和鼠标双击事件
*
* */
public static void paintMoRen(){
for(int i=0;i<30;i++){
Random ran = new Random();
int lx = ran.nextInt(15);
int ly = ran.nextInt(15);
if(arr[lx][ly] == 0){//表示这个地方还没有布置雷
arr[lx][ly] = -1;
}else{
i--;
}
}
for(int i=0;i=0 && x = 0 && y+1 < arr.length && arr[x][y+1] == -1) res++;
if( x+1 >=0 && x+1 = 0 && y+1 < arr.length && arr[x+1][y+1] == -1) res++;
if( x+1 >=0 && x+1 = 0 && y < arr.length && arr[x+1][y] == -1 ) res++;
if( x+1 >=0 && x+1 = 0 && y-1 < arr.length && arr[x+1][y-1] == -1 ) res++;
if( x >=0 && x = 0 && y-1 < arr.length && arr[x][y-1] == -1 ) res++;
if( x-1 >=0 && x-1 = 0 && y-1 < arr.length && arr[x-1][y-1] == -1 ) res++;
if( x-1 >=0 && x-1 = 0 && y < arr.length && arr[x-1][y] == -1 ) res++;
if( x-1 >=0 && x-1 = 0 && y+1 =0 && tx < arr.length && ty >=0 && ty < arr.length && book[tx][ty] == 0){
book[tx][ty] = 1;
wipeCount++;
if(arr[tx][ty] == 0){
showRound(tx,ty);
}
}
}
}
// 建立一个点击某个按钮 根据不同的按钮显示不同的界面
public static void showViewByButton(int num,IndexJButton eventButton){
}
// 采用java的swing组件编写界面
public void produceView() throws Exception {
JFrame jf = new JFrame("简易扫雷");
JPanel jp = new JPanel();
GridLayout gl = new GridLayout(15,15);//java.lang.Integer
jp.setLayout(gl);
//JPanel jp = new JPanel(new GridLayout(20,20,0,0));
MouseListener ml = new MouseListener() {
public void mouseReleased(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
try {
mus = new File(SRC_URL+"7116.wav").toURI().toURL();
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
Applet.newAudioClip(mus).play();
if(e.getButton() == 1){
// 表示鼠标左击 打开
IndexJButton eventButton = (IndexJButton) e.getSource();
int x = eventButton.x;
int y = eventButton.y;
if(arr[x][y] == 0){
wipeCount++;
showRound(x, y);
if(wipeCount == 185){
// 表示成功完成任务
try {
mus = new File(SRC_URL+"8745.wav").toURI().toURL();
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
Applet.newAudioClip(mus).play();
JOptionPane.showMessageDialog
(null, "你胜利了"+wipeCount+"个格子", "你胜利了", JOptionPane.ERROR_MESSAGE);
}
for(int m=0;m 0){
wipeCount++;
if(wipeCount == 185){
// 表示成功完成任务
try {
mus = new File(SRC_URL+"8745.wav").toURI().toURL();
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
Applet.newAudioClip(mus).play();
JOptionPane.showMessageDialog
(null, "你胜利了"+wipeCount+"个格子", "你胜利了", JOptionPane.ERROR_MESSAGE);
}
try {
eventButton.setIcon(new ImageIcon(new File(SRC_URL+"11"+arr[x][y]+".jpg").toURI().toURL()));
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
book[x][y] = 1;
String str = arr[x][y]+"";
//eventButton.setContentAreaFilled(false);
//eventButton.setEnabled(false);
//eventButton.setText(str);
}else{
//表示踩到雷了
try {
mus = new File(SRC_URL+"4127.wav").toURI().toURL();
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
Applet.newAudioClip(mus).play();
JOptionPane.showMessageDialog
(null, "你踩到了雷,成功排了"+wipeCount+"个格子", "你失败了", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}else if(e.getButton() == 3){
// 表示鼠标右击 标记雷
IndexJButton eventButton = (IndexJButton) e.getSource();
int x = eventButton.x;
int y = eventButton.y;
if(book[x][y] != 1 && book[x][y] ==0){
// 表示这个格子还没有被打开
try {
eventButton.setIcon(new ImageIcon(new File(SRC_URL+"biaoji.jpg").toURI().toURL()));
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
book[x][y] = 3;//表示这个地方被标记成雷区
}else if(book[x][y] == 3){
try {
eventButton.setIcon(new ImageIcon(new File(SRC_URL+"wen.jpg").toURI().toURL()));
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
book[x][y] = 4;
}else if(book[x][y] == 4){
try {
eventButton.setIcon(new ImageIcon(new File(SRC_URL+"repare.jpg").toURI().toURL()));
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
book[x][y] = 0;
}
}
}
};
/*
ActionListener al = new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
IndexJButton eventButton = (IndexJButton) e.getSource();
int x = eventButton.x;
int y = eventButton.y;
if(arr[x][y] == 0){
showRound(x, y);
for(int m=0;m 0){
book[x][y] = 1;
String str = arr[x][y]+"";
eventButton.setContentAreaFilled(false);
eventButton.setEnabled(false);
eventButton.setText(str);
}else{
//表示踩到雷了
JOptionPane.showMessageDialog
(null, "你踩到了雷,成功排了"+wipeCount+"个格子", "你失败了", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
};
*/
for(int i=0;i
希望对初学的同学有帮助吧,毕竟学习还是要建立在兴趣的基础之上才会有动力坚持。
源码地址 https://github.com/laofuzi123/simpleMineClearance.git