偶学习java也已经有半年的时间了,但是总觉得没学到什么东西,有没有遇到某位高人指点一下(没那个命).感觉都有一点学不下去了.最近开始试着写一些小的游戏,想增加一下自己的学习java的兴趣....偶的第一个小游戏--扫雷...写的还不是那么好,有一些功能还没有用上.希望那位大虾看到后能指点一下小弟........顺便教偶一下java的学习之道..........
//ExtendButton.java
import java.awt.*;
public class ExtendButton extends Button
{
private int Button_Position;
private boolean Button_Status;
private boolean Button_Visit;
public void setPosition(int newPos)
{
Button_Position = newPos;
}
public int getPosition()
{
return Button_Position;
}
public void setStatus(boolean newStatus)
{
Button_Status = newStatus;
}
public boolean getStatus()
{
return Button_Status;
}
public void setVisit(boolean newVisit)
{
Button_Visit = newVisit;
}
public boolean getVisit()
{
return Button_Visit;
}
}
//Game.java
/**
* AWT Sample application
*
* @author
* @version 1.00 06/02/27
*/
import java.awt.*;
import java.awt.event.*;
//import java.util.Random;
import javax.swing.JOptionPane;
public class Game implements ActionListener
{
private Frame frame;
private MenuBar mb;
private int initRow;
private int initColumn;
ExtendButton[] myButton;
public Game()
{
frame = new Frame("扫雷 @ G.L");
mb = new MenuBar();
frame.setLayout(new GridLayout(10,10));
setMenu(frame,mb);
frame.pack();
frame.setSize(200,200);
frame.setVisible(true);
}
public void setMenu(Frame frame,MenuBar menuBar)
{
Menu m_Edit = new Menu("游戏");
MenuItem mi_chuji = new MenuItem("初级");
MenuItem mi_zhongji = new MenuItem("中级");
MenuItem mi_gaoji = new MenuItem("高级");
MenuItem mi_Exit = new MenuItem("退出",new MenuShortcut(KeyEvent.VK_X));
m_Edit.add(mi_chuji);
m_Edit.add(mi_zhongji);
m_Edit.add(mi_gaoji);
m_Edit.addSeparator();
m_Edit.add(mi_Exit);
for(int i = 0;i < m_Edit.getItemCount();i++)
{
m_Edit.getItem(i).addActionListener(this);
}
mb.add(m_Edit);
frame.setMenuBar(mb);
}
public void initGame(int bombCount,int row,int column,int width,int height)
{
initRow = row;
initColumn = column;
frame.setLayout(new GridLayout(row,column));
frame.setSize(width,height);
frame.setVisible(true);
myButton = new ExtendButton[row*column];
for(int i = 0;i < row*column;i++)
{
myButton[i] = new ExtendButton();
myButton[i].setPosition(i);
myButton[i].setStatus(false);
myButton[i].setVisit(false);
myButton[i].addActionListener(this);
frame.add(myButton[i]);
}
System.gc();
setBomb(bombCount);
}
public void setBomb(int bombCount)
{
int count = 0;
while(count != bombCount)
{
int temp = (int)(Math.random()*initRow*initColumn);
//Random cnd = new Random();
//int temp = cnd.nextInt(initRow*initColumn);
if(!myButton[temp].getStatus())
{
myButton[temp].setStatus(true);
count++;
}
}
}
public void showBomb()
{
for(int i = 0;i < initRow*initColumn;i++)
{
if(myButton[i].getStatus())
myButton[i].setBackground(Color.BLACK);
}
}
public void checkButton(ExtendButton tempButton)
{
int temp = 0;
int[] circleNum = new int[8];
if(!tempButton.getStatus())
{
circleNum[0] = tempButton.getPosition()-initColumn;
circleNum[0] = (circleNum[0]<0) ? -1:circleNum[0];
circleNum[1] = tempButton.getPosition()-initColumn+1;
circleNum[1] = (circleNum[1]<0||circleNum[1]%initColumn==0) ? -1:circleNum[1];
circleNum[2] = tempButton.getPosition()+1;
circleNum[2] = (circleNum[2]>(initRow*initColumn-1)||circleNum[2]%initColumn==0) ? -1:circleNum[2];
circleNum[3] = tempButton.getPosition()+initColumn+1;
circleNum[3] = (circleNum[3]>(initRow*initColumn-1)||circleNum[3]%initColumn==0) ? -1:circleNum[3];
circleNum[4] = tempButton.getPosition()+initColumn;
circleNum[4] = (circleNum[4]>(initRow*initColumn-1)) ? -1:circleNum[4];
circleNum[5] = tempButton.getPosition()+initColumn-1;
circleNum[5] = (circleNum[5]>(initRow*initColumn-1)||tempButton.getPosition()%initColumn==0) ? -1:circleNum[5];
circleNum[6] = tempButton.getPosition()-1;
circleNum[6] = (circleNum[6]<0||tempButton.getPosition()%initColumn==0) ? -1:circleNum[6];
circleNum[7] = tempButton.getPosition()-initColumn-1;
circleNum[7] = (circleNum[7]<0||tempButton.getPosition()%initColumn==0) ? -1:circleNum[7];
}
else if(tempButton.getStatus())
{
JOptionPane.showMessageDialog( null, "You Failed!", "Dialog",JOptionPane.INFORMATION_MESSAGE );
this.showBomb();
return;
}
if(!tempButton.getStatus() && !tempButton.getVisit())
{
for(int i = 0;i<8;i++)
{
if((circleNum[i]!=-1) && myButton[circleNum[i]].getStatus())
temp++;
}
if(temp>0)
{
tempButton.setVisit(true);
tempButton.setLabel(String.valueOf(temp));
}
else if(temp==0)
{
tempButton.setVisit(true);
tempButton.setBackground(Color.GREEN);
for(int j = 0;j<8;j++)
{
if(circleNum[j]!=-1 && !myButton[circleNum[j]].getStatus())
checkButton(myButton[circleNum[j]]);
}
}
}
}
public void checkStop()
{
boolean bn=true;
for(int i = 0;i < initRow*initColumn;i++)
{
if(!myButton[i].getVisit())
{
if(!myButton[i].getStatus())
bn=false;
}
}
if(bn)
{
JOptionPane.showMessageDialog( null, "You Winning!", "Dialog",JOptionPane.INFORMATION_MESSAGE );
return;
}
}
public void actionPerformed(ActionEvent ae)
{
ExtendButton extendButton;
if(ae.getSource() instanceof ExtendButton)
{
extendButton = (ExtendButton)ae.getSource();
checkButton(extendButton);
checkStop();
}
else if(ae.getSource() instanceof MenuItem)
{
if(ae.getActionCommand()=="初级")
{
initGame(10,9,9,250,270);
}
if(ae.getActionCommand()=="中级")
{
initGame(40,16,16,480,500);
}
else if(ae.getActionCommand()=="高级")
{
initGame(99,16,30,700,500);
}
else if(ae.getActionCommand()=="退出")
{
frame.dispose();
}
}
}
public static void main(String[] args)
{
Game game = new Game();
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
};
}
}
posted on 2006-03-01 10:21
Kira-2006 阅读(151)
评论(0) 编辑 收藏