java小游戲
⑴ java小游戲
按照題目要求,人拿完火柴後計算機自動拿火柴,判斷勝利者。滑鼠點擊ok或者鍵盤按enter鍵即可提交人拿的火柴個數。圖形界面如下,
 
⑵ Java各種小游戲的編程思路
Java小游戲主要的是使用java swing,通過組件化示例一個模型,滑鼠監聽移動,刷新界面,進行交互。
⑶ 電腦java平台有什麼好玩的小游戲
《軍魂2》(守城類,怪物攻城)
《霸仼の
陸》(當偂葙當受歡迎。冇誃種版夲)。《聖吙外傳》(魘幻類の游戲,褦囀職乜褦自己挑戰自己)《神怪決》
⑷ 用JAVA編一個小游戲或者其他程序
貪吃蛇程序:
GreedSnake.java (也是程序入口):
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GreedSnake implements KeyListener {
 JFrame mainFrame;
 Canvas paintCanvas;
 JLabel labelScore;// 計分牌
 SnakeModel snakeModel = null;// 蛇
 public static final int canvasWidth = 200;
 public static final int canvasHeight = 300;
 public static final int nodeWidth = 10;
 public static final int nodeHeight = 10;
 // ----------------------------------------------------------------------
 // GreedSnake():初始化游戲界面
 // ----------------------------------------------------------------------
 public GreedSnake() {
  // 設置界面元素
  mainFrame = new JFrame("GreedSnake");
  Container cp = mainFrame.getContentPane();
  labelScore = new JLabel("Score:");
  cp.add(labelScore, BorderLayout.NORTH);
  paintCanvas = new Canvas();
  paintCanvas.setSize(canvasWidth + 1, canvasHeight + 1);
  paintCanvas.addKeyListener(this);
  cp.add(paintCanvas, BorderLayout.CENTER);
  JPanel panelButtom = new JPanel();
  panelButtom.setLayout(new BorderLayout());
  JLabel labelHelp;// 幫助信息
  labelHelp = new JLabel("PageUp, PageDown for speed;", JLabel.CENTER);
  panelButtom.add(labelHelp, BorderLayout.NORTH);
  labelHelp = new JLabel("ENTER or R or S for start;", JLabel.CENTER);
  panelButtom.add(labelHelp, BorderLayout.CENTER);
  labelHelp = new JLabel("SPACE or P for pause", JLabel.CENTER);
  panelButtom.add(labelHelp, BorderLayout.SOUTH);
  cp.add(panelButtom, BorderLayout.SOUTH);
  mainFrame.addKeyListener(this);
  mainFrame.pack();
  mainFrame.setResizable(false);
  mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  mainFrame.setVisible(true);
  begin();
 }
 // ----------------------------------------------------------------------
 // keyPressed():按鍵檢測
 // ----------------------------------------------------------------------
 public void keyPressed(KeyEvent e) {
  int keyCode = e.getKeyCode();
  if (snakeModel.running)
   switch (keyCode) {
   case KeyEvent.VK_UP:
    snakeModel.changeDirection(SnakeModel.UP);
    break;
   case KeyEvent.VK_DOWN:
    snakeModel.changeDirection(SnakeModel.DOWN);
    break;
   case KeyEvent.VK_LEFT:
    snakeModel.changeDirection(SnakeModel.LEFT);
    break;
   case KeyEvent.VK_RIGHT:
    snakeModel.changeDirection(SnakeModel.RIGHT);
    break;
   case KeyEvent.VK_ADD:
   case KeyEvent.VK_PAGE_UP:
    snakeModel.speedUp();// 加速
    break;
   case KeyEvent.VK_SUBTRACT:
   case KeyEvent.VK_PAGE_DOWN:
    snakeModel.speedDown();// 減速
    break;
   case KeyEvent.VK_SPACE:
   case KeyEvent.VK_P:
    snakeModel.changePauseState();// 暫停或繼續
    break;
   default:
   }
  // 重新開始
  if (keyCode == KeyEvent.VK_R || keyCode == KeyEvent.VK_S
    || keyCode == KeyEvent.VK_ENTER) {
   snakeModel.running = false;
   begin();
  }
 }
 // ----------------------------------------------------------------------
 // keyReleased():空函數
 // ----------------------------------------------------------------------
 public void keyReleased(KeyEvent e) {
 }
 // ----------------------------------------------------------------------
 // keyTyped():空函數
 // ----------------------------------------------------------------------
 public void keyTyped(KeyEvent e) {
 }
 // ----------------------------------------------------------------------
 // repaint():繪制游戲界面(包括蛇和食物)
 // ----------------------------------------------------------------------
 void repaint() {
  Graphics g = paintCanvas.getGraphics();
  // draw background
  g.setColor(Color.WHITE);
  g.fillRect(0, 0, canvasWidth, canvasHeight);
  // draw the snake
  g.setColor(Color.BLACK);
  LinkedList na = snakeModel.nodeArray;
  Iterator it = na.iterator();
  while (it.hasNext()) {
   Node n = (Node) it.next();
   drawNode(g, n);
  }
  // draw the food
  g.setColor(Color.RED);
  Node n = snakeModel.food;
  drawNode(g, n);
  updateScore();
 }
 // ----------------------------------------------------------------------
 // drawNode():繪畫某一結點(蛇身或食物)
 // ----------------------------------------------------------------------
 private void drawNode(Graphics g, Node n) {
  g.fillRect(n.x * nodeWidth, n.y * nodeHeight, nodeWidth - 1,
    nodeHeight - 1);
 }
 // ----------------------------------------------------------------------
 // updateScore():改變計分牌
 // ----------------------------------------------------------------------
 public void updateScore() {
  String s = "Score: " + snakeModel.score;
  labelScore.setText(s);
 }
 // ----------------------------------------------------------------------
 // begin():游戲開始,放置貪吃蛇
 // ----------------------------------------------------------------------
 void begin() {
  if (snakeModel == null || !snakeModel.running) {
   snakeModel = new SnakeModel(this, canvasWidth / nodeWidth,
     this.canvasHeight / nodeHeight);
   (new Thread(snakeModel)).start();
  }
 }
 // ----------------------------------------------------------------------
 // main():主函數
 // ----------------------------------------------------------------------
 public static void main(String[] args) {
  GreedSnake gs = new GreedSnake();
 }
}
Node.java:
public class Node {
 int x;
 int y;
 Node(int x, int y) {
  this.x = x;
  this.y = y;
 }
}
SnakeModel.java:
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Random;
import javax.swing.JOptionPane;
public class SnakeModel implements Runnable {
 GreedSnake gs;
 boolean[][] matrix;// 界面數據保存在數組里
 LinkedList nodeArray = new LinkedList();
 Node food;
 int maxX;// 最大寬度
 int maxY;// 最大長度
 int direction = 2;// 方向
 boolean running = false;
 int timeInterval = 200;// 間隔時間(速度)
 double speedChangeRate = 0.75;// 速度改變程度
 boolean paused = false;// 游戲狀態
 int score = 0;
 int countMove = 0;
 // UP和DOWN是偶數,RIGHT和LEFT是奇數
 public static final int UP = 2;
 public static final int DOWN = 4;
 public static final int LEFT = 1;
 public static final int RIGHT = 3;
 // ----------------------------------------------------------------------
 // GreedModel():初始化界面
 // ----------------------------------------------------------------------
 public SnakeModel(GreedSnake gs, int maxX, int maxY) {
  this.gs = gs;
  this.maxX = maxX;
  this.maxY = maxY;
  matrix = new boolean[maxX][];
  for (int i = 0; i < maxX; ++i) {
   matrix[i] = new boolean[maxY];
   Arrays.fill(matrix[i], false);// 沒有蛇和食物的地區置false
  }
  // 初始化貪吃蛇
  int initArrayLength = maxX > 20 ? 10 : maxX / 2;
  for (int i = 0; i < initArrayLength; ++i) {
   int x = maxX / 2 + i;
   int y = maxY / 2;
   nodeArray.addLast(new Node(x, y));
   matrix[x][y] = true;// 蛇身處置true
  }
  food = createFood();
  matrix[food.x][food.y] = true;// 食物處置true
 }
 // ----------------------------------------------------------------------
 // changeDirection():改變運動方向
 // ----------------------------------------------------------------------
 public void changeDirection(int newDirection) {
  if (direction % 2 != newDirection % 2)// 避免沖突
  {
   direction = newDirection;
  }
 }
 // ----------------------------------------------------------------------
 // moveOn():貪吃蛇運動函數
 // ----------------------------------------------------------------------
 public boolean moveOn() {
  Node n = (Node) nodeArray.getFirst();
  int x = n.x;
  int y = n.y;
  switch (direction) {
  case UP:
   y--;
   break;
  case DOWN:
   y++;
   break;
  case LEFT:
   x--;
   break;
  case RIGHT:
   x++;
   break;
  }
  if ((0 <= x && x < maxX) && (0 <= y && y < maxY)) {
   if (matrix[x][y])// 吃到食物或者撞到身體
   {
    if (x == food.x && y == food.y)// 吃到食物
    {
     nodeArray.addFirst(food);// 在頭部加上一結點
     // 計分規則與移動長度和速度有關
     int scoreGet = (10000 - 200 * countMove) / timeInterval;
     score += scoreGet > 0 ? scoreGet : 10;
     countMove = 0;
     food = createFood();
     matrix[food.x][food.y] = true;
     return true;
    } else
     return false;// 撞到身體
   } else// 什麼都沒有碰到
   {
    nodeArray.addFirst(new Node(x, y));// 加上頭部
    matrix[x][y] = true;
    n = (Node) nodeArray.removeLast();// 去掉尾部
    matrix[n.x][n.y] = false;
    countMove++;
    return true;
   }
  }
  return false;// 越界(撞到牆壁)
 }
 // ----------------------------------------------------------------------
 // run():貪吃蛇運動線程
 // ----------------------------------------------------------------------
 public void run() {
  running = true;
  while (running) {
   try {
    Thread.sleep(timeInterval);
   } catch (Exception e) {
    break;
   }
   if (!paused) {
    if (moveOn())// 未結束
    {
     gs.repaint();
    } else// 游戲結束
    {
     JOptionPane.showMessageDialog(null, "GAME OVER",
       "Game Over", JOptionPane.INFORMATION_MESSAGE);
     break;
    }
   }
  }
  running = false;
 }
 // ----------------------------------------------------------------------
 // createFood():生成食物及放置地點
 // ----------------------------------------------------------------------
 private Node createFood() {
  int x = 0;
  int y = 0;
  do {
   Random r = new Random();
   x = r.nextInt(maxX);
   y = r.nextInt(maxY);
  } while (matrix[x][y]);
  return new Node(x, y);
 }
 // ----------------------------------------------------------------------
 // speedUp():加快蛇運動速度
 // ----------------------------------------------------------------------
 public void speedUp() {
  timeInterval *= speedChangeRate;
 }
 // ----------------------------------------------------------------------
 // speedDown():放慢蛇運動速度
 // ----------------------------------------------------------------------
 public void speedDown() {
  timeInterval /= speedChangeRate;
 }
 // ----------------------------------------------------------------------
 // changePauseState(): 改變游戲狀態(暫停或繼續)
 // ----------------------------------------------------------------------
 public void changePauseState() {
  paused = !paused;
 }
}
⑸ java小游戲運行方法
jar包的小游戲都有游戲入口,一般就是main方法,安裝好JRE後直接右鍵運行即可了
⑹ Java實現的趣味游戲
連連看java源代碼 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
public class lianliankan implements ActionListener 
{ 
JFrame mainFrame; //主面板 
Container thisContainer; 
JPanel centerPanel,southPanel,northPanel; //子面板 
JButton diamondsButton[][] = new JButton[6][5];//游戲按鈕數組 
JButton exitButton,resetButton,newlyButton; //退出,重列,重新開始按鈕 
JLabel fractionLable=new JLabel("0"); //分數標簽 
JButton firstButton,secondButton; //分別記錄兩次被選中的按鈕 
int grid[][] = new int[8][7];//儲存游戲按鈕位置 
static boolean pressInformation=false; //判斷是否有按鈕被選中 
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戲按鈕的位置坐標 
int i,j,k,n;//消除方法控制 
public void init(){ 
mainFrame=new JFrame("JKJ連連看"); 
thisContainer = mainFrame.getContentPane(); 
thisContainer.setLayout(new BorderLayout()); 
centerPanel=new JPanel(); 
southPanel=new JPanel(); 
northPanel=new JPanel(); 
thisContainer.add(centerPanel,"Center"); 
thisContainer.add(southPanel,"South"); 
thisContainer.add(northPanel,"North"); 
centerPanel.setLayout(new GridLayout(6,5)); 
for(int cols = 0;cols < 6;cols++){ 
for(int rows = 0;rows < 5;rows++ ){ 
diamondsButton[cols][rows]=new JButton(String.valueOf(grid[cols+1][rows+1])); 
diamondsButton[cols][rows].addActionListener(this); 
centerPanel.add(diamondsButton[cols][rows]); 
} 
} 
exitButton=new JButton("退出"); 
exitButton.addActionListener(this); 
resetButton=new JButton("重列"); 
resetButton.addActionListener(this); 
newlyButton=new JButton("再來一局"); 
newlyButton.addActionListener(this); 
southPanel.add(exitButton); 
southPanel.add(resetButton); 
southPanel.add(newlyButton); 
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText()))); 
northPanel.add(fractionLable); 
mainFrame.setBounds(280,100,500,450); 
mainFrame.setVisible(true); 
} 
public void randomBuild() { 
int randoms,cols,rows; 
for(int twins=1;twins<=15;twins++) { 
randoms=(int)(Math.random()*25+1); 
for(int alike=1;alike<=2;alike++) { 
cols=(int)(Math.random()*6+1); 
rows=(int)(Math.random()*5+1); 
while(grid[cols][rows]!=0) { 
cols=(int)(Math.random()*6+1); 
rows=(int)(Math.random()*5+1); 
} 
this.grid[cols][rows]=randoms; 
} 
} 
} 
public void fraction(){ 
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+100)); 
} 
public void reload() { 
int save[] = new int[30]; 
int n=0,cols,rows; 
int grid[][]= new int[8][7]; 
for(int i=0;i<=6;i++) { 
for(int j=0;j<=5;j++) { 
if(this.grid[i][j]!=0) { 
save[n]=this.grid[i][j]; 
n++; 
} 
} 
} 
n=n-1; 
this.grid=grid; 
while(n>=0) { 
cols=(int)(Math.random()*6+1); 
rows=(int)(Math.random()*5+1); 
while(grid[cols][rows]!=0) { 
cols=(int)(Math.random()*6+1); 
rows=(int)(Math.random()*5+1); 
} 
this.grid[cols][rows]=save[n]; 
n--; 
} 
mainFrame.setVisible(false); 
pressInformation=false; //這里一定要將按鈕點擊信息歸為初始 
init(); 
for(int i = 0;i < 6;i++){ 
for(int j = 0;j < 5;j++ ){ 
if(grid[i+1][j+1]==0) 
diamondsButton[i][j].setVisible(false); 
} 
} 
} 
public void estimateEven(int placeX,int placeY,JButton bz) { 
if(pressInformation==false) { 
x=placeX; 
y=placeY; 
secondMsg=grid[x][y]; 
secondButton=bz; 
pressInformation=true; 
} 
else { 
x0=x; 
y0=y; 
fristMsg=secondMsg; 
firstButton=secondButton; 
x=placeX; 
y=placeY; 
secondMsg=grid[x][y]; 
secondButton=bz; 
if(fristMsg==secondMsg && secondButton!=firstButton){ 
xiao(); 
} 
} 
} 
public void xiao() { //相同的情況下能不能消去。仔細分析,不一條條注釋 
if((x0==x &&(y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)&&(y0==y))){ //判斷是否相鄰 
remove(); 
} 
else{ 
for (j=0;j<7;j++ ) { 
if (grid[x0][j]==0){ //判斷第一個按鈕同行哪個按鈕為空 
if (y>j) { //如果第二個按鈕的Y坐標大於空按鈕的Y坐標說明第一按鈕在第二按鈕左邊 
for (i=y-1;i>=j;i-- ){ //判斷第二按鈕左側直到第一按鈕中間有沒有按鈕 
if (grid[x][i]!=0) { 
k=0; 
break; 
} 
else{ k=1; } //K=1說明通過了第一次驗證 
} 
if (k==1) { 
linePassOne(); 
} 
} 
if (y<j){ //如果第二個按鈕的Y坐標小於空按鈕的Y坐標說明第一按鈕在第二按鈕右邊 
for (i=y+1;i<=j ;i++ ){ //判斷第二按鈕左側直到第一按鈕中間有沒有按鈕 
if (grid[x][i]!=0){ 
k=0; 
break; 
} 
else { k=1; } 
} 
if (k==1){ 
linePassOne(); 
} 
} 
if (y==j ) { 
linePassOne(); 
} 
} 
if (k==2) { 
if (x0==x) { 
remove(); 
} 
if (x0<x) { 
for (n=x0;n<=x-1;n++ ) { 
if (grid[n][j]!=0) { 
k=0; 
break; 
} 
if(grid[n][j]==0 && n==x-1) { 
remove(); 
} 
} 
} 
if (x0>x) { 
for (n=x0;n>=x+1 ;n-- ) { 
if (grid[n][j]!=0) { 
k=0; 
break; 
} 
if(grid[n][j]==0 && n==x+1) { 
remove(); 
} 
} 
} 
} 
} 
for (i=0;i<8;i++ ) { //列 
if (grid[i][y0]==0) { 
if (x>i) { 
for (j=x-1;j>=i ;j-- ) { 
if (grid[j][y]!=0) { 
k=0; 
break; 
} 
else { k=1; } 
} 
if (k==1) { 
rowPassOne(); 
} 
} 
if (x<i) { 
for (j=x+1;j<=i;j++ ) { 
if (grid[j][y]!=0) { 
k=0; 
break; 
} 
else { k=1; } 
} 
if (k==1) { 
rowPassOne(); 
} 
} 
if (x==i) { 
rowPassOne(); 
} 
} 
if (k==2){ 
if (y0==y) { 
remove(); 
} 
if (y0<y) { 
for (n=y0;n<=y-1 ;n++ ) { 
if (grid[i][n]!=0) { 
k=0; 
break; 
} 
if(grid[i][n]==0 && n==y-1) { 
remove(); 
} 
} 
} 
if (y0>y) { 
for (n=y0;n>=y+1 ;n--) { 
if (grid[i][n]!=0) { 
k=0; 
break; 
} 
if(grid[i][n]==0 && n==y+1) { 
remove(); 
} 
} 
} 
} 
} 
} 
} 
public void linePassOne(){ 
if (y0>j){ //第一按鈕同行空按鈕在左邊 
for (i=y0-1;i>=j ;i-- ){ //判斷第一按鈕同左側空按鈕之間有沒按鈕 
if (grid[x0][i]!=0) { 
k=0; 
break; 
} 
else { k=2; } //K=2說明通過了第二次驗證 
} 
} 
if (y0<j){ //第一按鈕同行空按鈕在與第二按鈕之間 
for (i=y0+1;i<=j ;i++){ 
if (grid[x0][i]!=0) { 
k=0; 
break; 
} 
else{ k=2; } 
} 
} 
} 
public void rowPassOne(){ 
if (x0>i) { 
for (j=x0-1;j>=i ;j-- ) { 
if (grid[j][y0]!=0) { 
k=0; 
break; 
} 
else { k=2; } 
} 
} 
if (x0<i) { 
for (j=x0+1;j<=i ;j++ ) { 
if (grid[j][y0]!=0) { 
k=0; 
break; 
} 
else { k=2; } 
} 
} 
} 
public void remove(){ 
firstButton.setVisible(false); 
secondButton.setVisible(false); 
fraction(); 
pressInformation=false; 
k=0; 
grid[x0][y0]=0; 
grid[x][y]=0; 
} 
public void actionPerformed(ActionEvent e) { 
if(e.getSource()==newlyButton){ 
int grid[][] = new int[8][7]; 
this.grid = grid; 
randomBuild(); 
mainFrame.setVisible(false); 
pressInformation=false; 
init(); 
} 
if(e.getSource()==exitButton) 
System.exit(0); 
if(e.getSource()==resetButton) 
reload(); 
for(int cols = 0;cols < 6;cols++){ 
for(int rows = 0;rows < 5;rows++ ){ 
if(e.getSource()==diamondsButton[cols][rows]) 
estimateEven(cols+1,rows+1,diamondsButton[cols][rows]); 
} 
} 
} 
public static void main(String[] args) { 
lianliankan llk = new lianliankan(); 
llk.randomBuild(); 
llk.init(); 
} 
} 
//old 998 lines 
//new 318 lines
請採納答案,支持我一下。
⑺ 用java做一個小游戲
import java.awt.*;
public class TowerPoint
{
   int x,y;                  
   boolean 有盤子;           
   Disk 盤子=null;           
   HannoiTower con=null;     
   public TowerPoint(int x,int y,boolean boo)
   {
      this.x=x;
      this.y=y;
      有盤子=boo;
   }
  public boolean 是否有盤子()
  {
    return 有盤子;
  }
  public void set有盤子(boolean boo)
  {
    有盤子=boo;
  }
 
  public int getX()
  {
    return x;
  }
  public int getY()
  {
    return y;
  }
  public void 放置盤子(Disk 盤子,HannoiTower con)
  {
     this.con=con;
     con.setLayout(null);
     this.盤子=盤子;
     con.add(盤子);
     int w=盤子.getBounds().width;
     int h=盤子.getBounds().height;
     盤子.setBounds(x-w/2,y-h/2,w,h);
     有盤子=true;
     con.validate(); 
  }
  public Disk 獲取盤子()
  {
     return 盤子;
  }
}
⑻ java 小游戲
import java.util.Random;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.*;
public class SmallGame extends JFrame {
 private Random r;
 private String[] box = { "剪刀", "石頭", "布" };
 private JComboBox choice;
 private JTextArea ta;
 private JLabel lb;
 private int win = 0;
 private int loss = 0;
 private int equal = 0;
 public SmallGame() {
  initial();//調用initial方法,就是下面定義的那個.該方法主要是初始界面.
  pack();
  setTitle("游戲主界面");
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setLocation(400, 300);
  setVisible(true);
 }
 public static void main(String[] args) {
  new SmallGame();
 }
 public void initial() {
  r = new Random(); // 生成隨機數
  choice = new JComboBox();//初始化choice這個下拉框.也就是你選擇出剪子還是石頭什麼的那個下拉框..
  for (int i = 0; i < box.length; i++) {//為那個下拉框賦值.用前面定義的private String[] box = { "剪刀", "石頭", "布" };附值.這樣下拉框就有三個選項了..
   choice.addItem(box[i]);
  }
  ta = new JTextArea(3, 15);//初始化那個文本域3行15列
  ta.setEditable(false);//讓用戶不能編輯那個文本域即不能在裡面寫東西
  JButton okBut = new JButton("出招");//新建一個出招的按鈕
  okBut.addActionListener(new ActionListener() {//給出招按鈕加個監聽.意思就是監聽著這個按鈕看用戶有沒有點擊它..如果點擊就執行下面這個方法
   public void actionPerformed(ActionEvent e) {//就是這個方法
    ta.setText(getResult());//給那個文本域賦值..就是顯示結果  賦值的是通過getResult()這個方法得到的返回值 getResult()這個方法下面會講
    lb.setText(getTotal());//給分數那個LABEL賦值..就是顯示分數..賦值的是通過getTotal()這個方法得到的返回值
   }
  });
  JButton clearBut = new JButton("清除分數");//新建一個清楚分數的按鈕
  clearBut.addActionListener(new ActionListener() {//同上給他加個監聽
   public void actionPerformed(ActionEvent e) {//如果用戶點擊了就執行這個方法
    ta.setText("");//給文本域賦值為""..其實就是清楚他的內容
    win = 0;//win賦值為0  
    loss = 0;//同上
    equal = 0;//同上
    lb.setText(getTotal());//給顯示分數那個賦值..因為前面已經都賦值為0了..所以這句就是讓顯示分數那都為0
   }
  });
  lb = new JLabel(getTotal());//初始化那個顯示分數的東西
  JPanel choicePanel = new JPanel();//定義一個面板..面板就相當於一個畫圖用的東西..可以在上面加按鈕啊文本域什麼的..
  choicePanel.add(choice);//把下拉框加到面板里
  choicePanel.add(okBut);//把出招按鈕加到面板里
  choicePanel.add(clearBut);//把清楚分數按鈕加到面板里
  JScrollPane resultPanel = new JScrollPane(ta);//把文本域加到一個可滾動的面板裡面..JScrollPane就是可滾動的面板..這樣如果那個文本域內容太多就會出現滾動條..而不是變大
  JPanel totalPanel = new JPanel();//再定義個面板..用來顯示分數的..
  totalPanel.add(lb);//把那個顯示分數的label加到裡面去
  Container contentPane = getContentPane();//下面就是布局了..
  contentPane.setLayout(new BorderLayout());
  contentPane.add(choicePanel, BorderLayout.NORTH);
  contentPane.add(resultPanel, BorderLayout.CENTER);
  contentPane.add(totalPanel, BorderLayout.SOUTH);
 }
 public String getResult() {//獲得結果的方法 返回值是一個String..這個返回值會用來顯示在文本域裡面
  String tmp = "";
  int boxPeop = choice.getSelectedIndex();//獲得你選擇的那個的索引..從0開始的..
  int boxComp = getBoxComp();//獲得電腦出的索引..就是隨機的0-2的數
  tmp += "你出:\t" + box[boxPeop];//下面你應該明白了..
  tmp += "\n電腦出:\t" + box[boxComp];
  tmp += "\n結果:\t" + check(boxPeop, boxComp);
  return tmp;
 }
 public int getBoxComp() {//就是產生一個0-2的隨機數..
  return r.nextInt(3);//Random的nextInt(int i)方法就是產生一個[0-i)的隨機整數 所以nextInt(3)就是[0-2]的隨機數
 }
 public String check(int boxPeop, int boxComp) {//這個就是判斷你選擇的和電腦選擇的比較結果..是輸是贏還是平..boxPeop就是你選擇的..boxComp就是電腦選擇的..
  String result = "";
  if (boxPeop == (boxComp + 1) % 3) {//(boxComp + 1) % 3  電腦選擇的加上1加除以3取余..也就是如果電腦選0這個就為1..這個判斷的意思就是如果電腦選0並且你選1..那麼就是電腦選了
   //private String[] box = { "剪刀", "石頭", "布" };這裡面下標為0的..你選了下標為1的..就是電腦選剪刀你選石頭..所以你贏了..如果電腦選1..(boxComp + 1) % 3就為2..意思就是
   //電腦選了石頭你選了布..所以你贏了..另外一種情況你明白了撒..只有三種情況你贏..所以這里都包含了..也只包含了那三種..
   result = "你贏了!";
   win++;//贏了就讓記你贏的次數的那個變數加1
  } else if (boxPeop == boxComp) {//相等當然平手了
   result = "平";
   equal++;//同上了
  } else {//除了贏和平當然就是輸了..
   result = "你輸了!";
   loss++;//同上
  }
  return result;
 }
 public int getPoint() {
  return (win - loss) * 10;
 }
 public String getTotal() {
  return "贏:" + win + "     平:" + equal + "     輸:" + loss + "     得分:"
    + getPoint();
 }
}
希望你能明白哈。。
⑼ 用JAVA編寫一個小游戲
前天寫的猜數字游戲,yongi控制猜測次數,有詳細解析,用黑窗口可以直接運行,
我試驗過了,沒問題
import javax.swing.Icon;
import javax.swing.JOptionPane;
public class CaiShuZi4JOptionPane {
/**
* @param args
*/
public static void main(String[] args) {
Icon icon = null;
boolean bl = false;
int put = 0;
int c = (int) (((Math.random())*100)+1); //獲取一個1-100的隨機數
System.out.println("你獲取的隨機數是:"+c); //列印你的隨機數字
String str1 = (String) JOptionPane.showInputDialog(null,"請輸入你的猜測數字(1-100): ","猜數字游戲",JOptionPane.PLAIN_MESSAGE,icon,null,"在這輸入"); //第一次輸入你的猜測數字
if(str1==null){
JOptionPane.showMessageDialog(null, "你已經取消了本次游戲"); //如果你點取消那麼本次游戲結束
}else{
bl = num(str1); //判斷是輸入的是不是數字或者是整數
if(true==bl){ //如果是數字的話進入與隨機數比較的程序
System.out.println("你輸入的數字是:"+str1); //列印你輸入的數字
put = Integer.valueOf(str1);
for(int i = 4;i > 0;i--){ //i是你可以猜測的次數
if(put==c){
JOptionPane.showMessageDialog(null, "恭喜你猜對了,正確答案是:"+c+"。"); //如果你猜對了就直接結束循環
break;
}else if(put>c){ //如果輸大了就讓你再次從新輸入
str1 = (String) JOptionPane.showInputDialog(null,"你的輸入過大。你還有"+i+"次機會,請重新輸入: ","猜數字游戲",JOptionPane.PLAIN_MESSAGE,icon,null,"在這輸入");
if(str1==null){
JOptionPane.showMessageDialog(null, "你已經取消了本次輸入");
break;
}else{
bl =num(str1);
if(true==bl){
put = Integer.valueOf(str1);
}else{
JOptionPane.showMessageDialog(null, "你的輸入不正確,請重新輸入");
}
}
}else if(put<c){ //如果你輸小了也讓你從新輸入
str1 = (String) JOptionPane.showInputDialog(null,"你的輸入過小。你還有"+i+"次機會,請重新輸入: ","猜數字游戲",JOptionPane.PLAIN_MESSAGE,icon,null,"在這輸入");
if(str1==null){
JOptionPane.showMessageDialog(null, "你已經取消了本次輸入");
break;
}else{
bl =num(str1);
if(true==bl){
put = Integer.valueOf(str1);
}else{
JOptionPane.showMessageDialog(null, "你的輸入不正確,請重新輸入");
}
}
}
}
}else if(bl==false){ //這個 是你第一次如果填寫的不是數字的話也會結束本次游戲
JOptionPane.showMessageDialog(null, "請您下次按要求填寫。本次游戲結束");
}
if(true==bl && c!=put){ //如果你i次都沒猜對,那麼就直接告訴你這個數十什麼
JOptionPane.showMessageDialog(null, "很遺憾你沒能猜對,這個數字是:"+c+".");
}
}
}
public static boolean num(String value){ //一個靜態方法,判斷你輸入的是不是數字
try {
Integer.parseInt(value);
return true;
} catch (Exception e) {
return false;
}
}
}
⑽ 求一個簡單又有趣的JAVA小游戲代碼
具體如下:
連連看的小源碼
package Lianliankan;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class lianliankan implements ActionListener
{
JFrame mainFrame; //主面板
Container thisContainer;
JPanel centerPanel,southPanel,northPanel; //子面板
JButton diamondsButton[][] = new JButton[6][5];//游戲按鈕數組
JButton exitButton,resetButton,newlyButton; //退出,重列,重新開始按鈕
JLabel fractionLable=new JLabel("0"); //分數標簽
JButton firstButton,secondButton; //
分別記錄兩次被選中的按鈕
int grid[][] = new int[8][7];//儲存游戲按鈕位置
static boolean pressInformation=false; //判斷是否有按鈕被選中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戲按鈕的位置坐標
int i,j,k,n;//消除方法控制

代碼(code)是程序員用開發工具所支持的語言寫出來的源文件,是一組由字元、符號或信號碼元以離散形式表示信息的明確的規則體系。
對於字元和Unicode數據的位模式的定義,此模式代表特定字母、數字或符號(例如 0x20 代表一個空格,而 0x74 代表字元「t」)。一些數據類型每個字元使用一個位元組;每個位元組可以具有 256 個不同的位模式中的一個模式。
在計算機中,字元由不同的位模式(ON 或 OFF)表示。每個位元組有 8 位,這 8 位可以有 256 種不同的 ON 和 OFF 組合模式。對於使用 1 個位元組存儲每個字元的程序,通過給每個位模式指派字元可表示最多 256 個不同的字元。2 個位元組有 16 位,這 16 位可以有 65,536 種唯一的 ON 和 OFF 組合模式。使用 2 個位元組表示每個字元的程序可表示最多 65,536 個字元。
單位元組代碼頁是字元定義,這些字元映射到每個位元組可能有的 256 種位模式中的每一種。代碼頁定義大小寫字元、數字、符號以及 !、@、#、% 等特殊字元的位模式。每種歐洲語言(如德語和西班牙語)都有各自的單位元組代碼頁。
雖然用於表示 A 到 Z 拉丁字母表字元的位模式在所有的代碼頁中都相同,但用於表示重音字元(如"é"和"á")的位模式在不同的代碼頁中卻不同。如果在運行不同代碼頁的計算機間交換數據,必須將所有字元數據由發送計算機的代碼頁轉換為接收計算機的代碼頁。如果源數據中的擴展字元在接收計算機的代碼頁中未定義,那麼數據將丟失。
如果某個資料庫為來自許多不同國家的客戶端提供服務,則很難為該資料庫選擇這樣一種代碼頁,使其包括所有客戶端計算機所需的全部擴展字元。而且,在代碼頁間不停地轉換需要花費大量的處理時間。
