/*
* 简单的小游戏:九宫格游戏,将蓝色方块全部变成绿色方块,最少25步能完成,你试试。。。
*/
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import java.awt.Panel;
import java.awt.Label;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;
public class Game {
private JFrame ga = null;
private MyJButton[] butt = new MyJButton[81];
private Panel pan = null;
private Panel pan1 = null;
private JButton but1 = new JButton("作弊器");
private JButton but2 = new JButton("重新开始");
private Label la = new Label("共走了:");
private static int count = 0;
private Label la2 = null;
public Game() {
inti();
}
public void inti() {
this.ga = new JFrame("我的游戏");
this.ga.setSize(500, 500);
this.ga.setLocation(300, 150);
this.pan = new Panel();
this.la2 = new Label("0步");
this.pan.add(la);
this.pan.add(la2);
this.pan.add(but2);
this.pan.add(but1);
this.but2.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
repet();
}
});
this.but1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
boss();
boolean b = flag();
if (b) {
int i =JOptionPane.showConfirmDialog(null, "恭喜您,您已通关,是否继续游戏", "温馨提示",
JOptionPane.YES_NO_OPTION);
if (i==0) {
repet();
} else {
System.exit(1);
}
}
}
});
this.pan1 = new Panel(new GridLayout(9, 9));
for (int i = 0; i < butt.length; i++) {
butt[i] = new MyJButton();
butt[i].setid(i);
final int index = butt[i].getid();
butt[i].setBackground(Color.BLUE);
this.pan1.add(butt[i]);
butt[i].addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
la2.setText(count+++1+"步");
changeColor(index);
if (index % 9 == 0) {
changeColor(index + 1);
changeColor(index +9);
changeColor(index - 9);
} else if ((index + 1) % 9== 0) {
changeColor(index -1);
changeColor(index +9);
changeColor(index -9);
} else {
changeColor(index - 1);
changeColor(index + 1);
changeColor(index +9);
changeColor(index - 9);
}
boolean b = flag();
if (b) {
int i =JOptionPane.showConfirmDialog(null, "恭喜您,您已通关,是否继续游戏", "温馨提示",
JOptionPane.YES_NO_OPTION);
if (i == 0) {
repet();
} else {
System.exit(1);
}
}
}
});
}
this.ga.getContentPane().add(pan, BorderLayout.NORTH);
this.ga.getContentPane().add(pan1);
this.ga.setVisible(true);
this.ga.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@SuppressWarnings("serial")
class MyJButton extends JButton {
private int id = 0;
public void setid(int id) {
this.id = id;
}
public int getid() {
return this.id;
}
}
public void changeColor(int index) {
if (index > 80 || index < 0) {
return;
} else {
if (butt[index].getBackground() == Color.BLUE) {
butt[index].setBackground(Color.GREEN);
} else {
butt[index].setBackground(Color.BLUE);
}
}
}
public void boss() {
la2.setText(count++ + 1 + "步");
for (int i = 0; i < butt.length; i++) {
butt[i].setBackground(Color.GREEN);
}
}
public void repet() {
count = 0;
la2.setText("0步");
for (int i = 0; i < butt.length; i++) {
butt[i].setBackground(Color.BLUE);
}
}
public final boolean flag (){
for (int j = 0; j < butt.length; j++) {
if (butt[j].getBackground() == Color.BLUE) {
return false;
}
}
return true;
}
public static void main(String[] args) {
@SuppressWarnings("unused")
Game g = new Game();
}
}
当然能用来写小游戏了,手机的游戏一般都是用java做的,单机的游戏用到的基本都是javase的知识!!你可以去网上查,java经典游戏代码,在百度文库里多了,有贪吃蛇的,坦克大战什么的。都是源代码,你直接编译了就能运行