java小游戏源代码 java

求java rpg小游戏源代码 最好是文字rpg 不需要很复杂 只是交作业用

连连看的小源码
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;//游戏按钮数组
JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮
JLabel fractionLable=new JLabel(“0“); //分数标签
JButton firstButton,secondButton; //分别记录两次被选中的按钮
int grid = new int;//储存游戏按钮位置
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;
int n=0,cols,rows;
int grid= new int;
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;
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小游戏源代码

介绍这个给你把…我空间还有很多..

http://hi.baidu.com/282919088

import java.applet.Applet;

import java.applet.AudioClip;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.Toolkit;

import javax.sound.sampled.AudioFileFormat;

import javax.sound.sampled.AudioSystem;

import javax.swing.JFrame;

import javax.swing.JPanel;

import java.awt.Rectangle;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JOptionPane;

import javax.swing.JSlider;

import javax.swing.JLabel;

import javax.swing.SwingUtilities;

import javax.swing.UIManager;

import javax.swing.event.ChangeEvent;

import javax.swing.event.ChangeListener;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import java.io.File;

import java.util.Vector;

public class Frame extends JFrame implements Runnable {

JPanel contentPane;

JPanel jPanel1 = new JPanel();

JButton jButton1 = new JButton();

JSlider jSlider1 = new JSlider();

JLabel jLabel1 = new JLabel();

JButton jButton2 = new JButton();

JLabel jLabel2 = new JLabel();

int count = 1, rapidity = 80; // count 当前进行的个数, rapidity 游标的位置

int zhengque = 0, cuowu = 0;

int rush = { 10 ,20 ,30 };     //游戏每关的个数 可以自由添加.列 { 10 ,20 ,30 ,40,50}

int rush_count = 0;      //记录关数

char list = { ’A’, ’B’, ’C’, ’D’, ’E’, ’F’, ’G’, ’H’, ’I’, ’J’, ’K’, ’L’,

    ’M’, ’N’, ’O’, ’P’, ’Q’, ’R’, ’S’, ’T’, ’U’, ’V’, ’W’, ’X’, ’Y’,

    ’Z’, ’1’, ’2’, ’3’, ’4’, ’5’, ’6’, ’7’, ’8’, ’9’ };           //随机出现的数字 可以自由添加

Vector number = new Vector();

String paiduan = “true“;

AudioClip Musci_anjian, Music_shibai, Music_chenggong;

public Frame() {

   try {

    setDefaultCloseOperation(EXIT_ON_CLOSE);

   

    //—————–声音文件———————

    Musci_anjian = Applet.newAudioClip(new File(“sounds//anjian.wav“)

      .toURL());

    Music_shibai = Applet.newAudioClip(new File(“sounds//shibai.wav“)

      .toURL());

    Music_chenggong = Applet.newAudioClip(new File(

      “sounds//chenggong.wav“).toURL());

   

    //—————————————

    jbInit();

   } catch (Exception exception) {

    exception.printStackTrace();

   }

}

/**

* Component initialization.

* @throws java.lang.Exception

*/

private void jbInit() throws Exception {

   contentPane = (JPanel) getContentPane();

   contentPane.setLayout(null);

   setSize(new Dimension(588, 530));

   setTitle(“Frame Title“);

   jPanel1.setBorder(BorderFactory.createEtchedBorder());

   jPanel1.setBounds(new Rectangle(4, 4, 573, 419));

   jPanel1.setLayout(null);

   jButton1.setBounds(new Rectangle(277, 442, 89, 31));

   jButton1.setText(“开始“);

   jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));

   jSlider1.setBounds(new Rectangle(83, 448, 164, 21));

   jSlider1.setMaximum(100);

   jSlider1.setMinimum(1);

   jSlider1.setValue(50);

   jLabel1.setText(“速度“);

   jLabel1.setBounds(new Rectangle(35, 451, 39, 18));

   jButton2.setBounds(new Rectangle(408, 442, 89, 31));

   jButton2.setText(“结束“);

   jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));

   jLabel2.setText(“第一关:100个“);

   jLabel2.setBounds(new Rectangle(414, 473, 171, 21));

   contentPane.add(jPanel1);

   contentPane.add(jButton2);

   contentPane.add(jButton1);

   contentPane.add(jSlider1);

   contentPane.add(jLabel1);

   contentPane.add(jLabel2);

   this.addKeyListener(new MyListener());

   jButton1.addKeyListener(new MyListener());

   jSlider1.addKeyListener(new MyListener());

   jSlider1.addChangeListener(new ChangeListener() {

    public void stateChanged(ChangeEvent e) {

     rapidity = jSlider1.getValue();

    }

   });

}

public void run() {

   number.clear();

   zhengque = 0;

   cuowu = 0;

   paiduan = “true“;

   while (count 《= rush[rush_count]) { 

    try {

     Thread t = new Thread(new Tthread());

     t.start();

     count += 1;

     Thread.sleep(1000 + (int) (Math.random() * 2000)); // 生产下组停顿时间

     // 最快1快.最慢2秒

    } catch (InterruptedException e) {

     e.printStackTrace();

    }

   }

   while (true) { // 等待最后一个字符消失

    if (number.size() == 0) {

     break;

    }

   }

   if (zhengque == 0) { // 为了以后相除..如果全部正确或者错误就会出现错误. 所以..

    zhengque = 1;

   }

   if (cuowu == 0) {

    cuowu = 1;

   }

   if (paiduan.equals(“true“)) { // 判断是否是自然结束

    if (zhengque / cuowu 》= 2) {

     JOptionPane.showMessageDialog(null, “恭喜你过关了“);

     rush_count += 1; // 自动加1关

     if (rush_count 《 rush.length) {

      if (rapidity 》 10) { // 当速度大于10的时候在-5提加速度.怕速度太快

       rapidity -= 5; // 速度自动减10毫秒

       jSlider1.setValue(rapidity); // 选择位置

      }

      Thread t = new Thread(this);

      t.start();

     } else {

      JOptionPane.showMessageDialog(null, “牛B…你通关了..“);

      rush_count = 0;

      count = 0;

     }

    } else {

     JOptionPane.showMessageDialog(null, “请再接再励“);

     rush_count = 0;

     count = 0;

    }

   } else {

    rush_count = 0;

    count = 0;

   }

}

public void jButton1_actionPerformed(ActionEvent e) {

   Thread t = new Thread(this);

   t.start();

}

public void jButton2_actionPerformed(ActionEvent e) {

   count = rush[rush_count] + 1;

   paiduan = “flase“;

}

class Tthread implements Runnable {

   public void run() {

    boolean fo = true;

    int Y = 0, X = 0;

    JLabel show = new JLabel();

    show.setFont(new java.awt.Font(“宋体“, Font.PLAIN, 33));

    jPanel1.add(show);

    X = 10 + (int) (Math.random() * 400);

    String parameter = list[(int) (Math.random() * list.length)] + ““;

    Bean bean = new Bean();

    bean.setParameter(parameter);

    bean.setShow(show);

    number.add(bean);

    show.setText(parameter);

    while (fo) {

     // ———————数字下移——————–

     show.setBounds(new Rectangle(X, Y += 2, 33, 33));

     try {

      Thread.sleep(rapidity);

     } catch (InterruptedException e) {

      e.printStackTrace();

     }

     if (Y 》= 419) {

      fo = false;

      for (int i = number.size() – 1; i 》= 0; i–) {

       Bean bn = ((Bean) number.get(i));

       if (parameter.equalsIgnoreCase(bn.getParameter())) {

        cuowu += 1;

        jLabel2.setText(“正确:“ + zhengque + “个,错误:“ + cuowu

          + “个“);

        number.removeElementAt(i);

        Music_shibai.play();

        break;

       }

      }

     }

    }

   }

}

class MyListener extends KeyAdapter {

   public void keyPressed(KeyEvent e) {

    String uu = e.getKeyChar() + ““;

    for (int i = 0; i 《 number.size(); i++) {

     Bean bean = ((Bean) number.get(i));

     if (uu.equalsIgnoreCase(bean.getParameter())) {

      zhengque += 1;

      number.removeElementAt(i);

      bean.getShow().setVisible(false);

      jLabel2.setText(“正确:“ + zhengque + “个,错误:“ + cuowu + “个“);

      Music_chenggong.play();

      break;

     }

    }

    Musci_anjian.play();

   }

}

public static void main(String args) {

   try {

    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

   } catch (Exception exception) {

    exception.printStackTrace();

   }

   Frame frame = new Frame();

   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

   Dimension frameSize = frame.getSize();

   if (frameSize.height 》 screenSize.height) {

    frameSize.height = screenSize.height;

   }

   if (frameSize.width 》 screenSize.width) {

    frameSize.width = screenSize.width;

   }

   frame.setLocation((screenSize.width – frameSize.width) / 2,

     (screenSize.height – frameSize.height) / 2);

   frame.setVisible(true);

}

}

class Frame1_jButton2_actionAdapter implements ActionListener {

private Frame adaptee;

Frame1_jButton2_actionAdapter(Frame adaptee) {

   this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

   adaptee.jButton2_actionPerformed(e);

}

}

class Frame1_jButton1_actionAdapter implements ActionListener {

private Frame adaptee;

Frame1_jButton1_actionAdapter(Frame adaptee) {

   this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

   adaptee.jButton1_actionPerformed(e);

}

}

class Bean {

String parameter = null;

JLabel show = null;

public JLabel getShow() {

   return show;

}

public void setShow(JLabel show) {

   this.show = show;

}

public String getParameter() {

   return parameter;

}

public void setParameter(String parameter) {

   this.parameter = parameter;

}

}

求一个简单的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;//游戏按钮数组
JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮
JLabel fractionLable=new JLabel(“0“); //分数标签
JButton firstButton,secondButton; //分别记录两次被选中的按钮
int grid = new int;//储存游戏按钮位置
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;
int n=0,cols,rows;
int grid= new int;
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;
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

急需基于eclipse的JAVA小游戏源代码!!!

单人版五子棋,不用导入,直接新建一个mywindow类就行,然后把一下代码粘贴就Ok了。或者,直接用dos就可以了。。
———————
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class mypanel extends Panel implements MouseListener
{
int chess = new int;
boolean Is_Black_True;
mypanel()
{
Is_Black_True = true;
for(int i = 0;i 《 11;i++)
{
for(int j = 0;j 《 11;j++)
{
chess[i][j] = 0;
}
}
addMouseListener(this);
setBackground(Color.BLUE);
setBounds(0, 0, 360, 360);
setVisible(true);
}
public void mousePressed(MouseEvent e)
{
int x = e.getX();
int y = e.getY();
if(x 《 25 || x 》 330 + 25 ||y 《 25 || y 》 330+25)
{
return;
}
if(chess[x/30-1][y/30-1] != 0)
{
return;
}
if(Is_Black_True == true)
{
chess[x/30-1][y/30-1] = 1;
Is_Black_True = false;
repaint();
Justisewiner();
return;
}
if(Is_Black_True == false)
{
chess[x/30-1][y/30-1] = 2;
Is_Black_True = true;
repaint();
Justisewiner();
return;
}
}
void Drawline(Graphics g)
{
for(int i = 30;i 《= 330;i += 30)
{
for(int j = 30;j 《= 330; j+= 30)
{
g.setColor(Color.WHITE);
g.drawLine(i, j, i, 330);
}
}
for(int j = 30;j 《= 330;j += 30)
{
g.setColor(Color.WHITE);
g.drawLine(30, j, 330, j);
}

}
void Drawchess(Graphics g)
{
for(int i = 0;i 《 11;i++)
{
for(int j = 0;j 《 11;j++)
{
if(chess[i][j] == 1)
{
g.setColor(Color.BLACK);
g.fillOval((i + 1) * 30 – 8, (j + 1) * 30 – 8, 16, 16);
}
if(chess[i][j] == 2)
{
g.setColor(Color.WHITE);
g.fillOval((i + 1) * 30 – 8, (j + 1) * 30 – 8, 16, 16);
}
}
}
}
void Justisewiner()
{
int black_count = 0;
int white_count = 0;
int i = 0;

for(i = 0;i 《 11;i++)//横向判断
{
for(int j = 0;j 《 11;j++)
{
if(chess[i][j] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, “黑棋胜利“);
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i][j] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, “白棋胜利“);
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}

for(i = 0;i 《 11;i++)//竖向判断
{
for(int j = 0;j 《 11;j++)
{
if(chess[j][i] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, “黑棋胜利“);
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[j][i] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, “白棋胜利“);
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
for(i = 0;i 《 7;i++)//左向右斜判断
{
for(int j = 0;j 《 7;j++)
{
for(int k = 0;k 《 5;k++)
{
if(chess[i + k][j + k] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, “黑棋胜利“);
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i + k][j + k] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, “白棋胜利“);
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
}

for(i = 4;i 《 11;i++)//右向左斜判断
{
for(int j = 6;j 》= 0;j–)
{
for(int k = 0;k 《 5;k++)
{
if(chess[i – k][j + k] == 1)
{
black_count++;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, “黑棋胜利“);
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i – k][j + k] == 2)
{
white_count++;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, “白棋胜利“);
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
}

}
void Clear_Chess()
{
for(int i=0;i《11;i++)
{
for(int j=0;j《11;j++)
{
chess[i][j]=0;
}
}
repaint();
}
public void paint(Graphics g)
{
Drawline(g);
Drawchess(g);
}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}

}
class myframe extends Frame implements WindowListener
{
mypanel panel;
myframe()
{
setLayout(null);
panel = new mypanel();
add(panel);
panel.setBounds(0,23, 360, 360);
setTitle(“单人版五子棋“);
setBounds(200, 200, 360, 383);
setVisible(true);
addWindowListener(this);

}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowDeactivated(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
}
public class mywindow
{
public static void main(String argc )
{
myframe f = new myframe();
}
}

求“贪吃蛇“小游戏JAVA源代码一份

贪吃蛇
import java.awt.*;
import java.awt.event.*;
public class GreedSnake //主类
{
/**
* @param args
*/
public static void main(String args) {
// TODO Auto-generated method stub
new MyWindow();
}
}
class MyPanel extends Panel implements KeyListener,Runnable//自定义面板类,继承了键盘和线程接口
{
Button snake; //定义蛇按钮
int shu=0; //蛇的节数
int food; //食物数组
boolean result=true; //判定结果是输 还是赢
Thread thread; //定义线程
static int weix,weiy; //食物位置
boolean t=true; //判定游戏是否结束
int fangxiang=0; //蛇移动方向
int x=0,y=0; //蛇头位置
MyPanel()
{
setLayout(null);
snake=new Button;
food=new int ;
thread=new Thread(this);
for(int j=0;j《20;j++)
{
food[j]=(int)(Math.random()*99);//定义20个随机食物
}
weix=(int)(food*0.1)*60; //十位*60为横坐标
weiy=(int)(food%10)*40; //个位*40为纵坐标
for(int i=0;i《20;i++)
{
snake[i]=new Button();
}
add(snake);
snake.setBackground(Color.black);
snake.addKeyListener(this); //为蛇头添加键盘监视器
snake.setBounds(0,0,10,10);
setBackground(Color.cyan);
}
public void run() //接收线程
{
while(t)
{
if(fangxiang==0)//向右
{
try
{
x+=10;
snake.setLocation(x, y);//设置蛇头位置
if(x==weix&&y==weiy) //吃到食物
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint(); //重绘下一个食物
add(snake[shu]); //增加蛇节数和位置
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100); //睡眠100ms
}
catch(Exception e){}
}
else if(fangxiang==1)//向左
{
try
{
x-=10;
snake.setLocation(x, y);
if(x==weix&&y==weiy)
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100);
}
catch(Exception e){}
}
else if(fangxiang==2)//向上
{
try
{
y-=10;
snake.setLocation(x, y);
if(x==weix&&y==weiy)
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100);
}
catch(Exception e){}
}
else if(fangxiang==3)//向下
{
try
{
y+=10;
snake.setLocation(x, y);
if(x==weix&&y==weiy)
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100);
}
catch(Exception e){}
}
int num1=shu;
while(num1》1)//判断是否咬自己的尾巴
{
if(snake[num1].getBounds().x==snake.getBounds().x&&snake[num1].getBounds().y==snake.getBounds().y)
{
t=false;
result=false;
repaint();
}
num1–;
}
if(x《0||x》=this.getWidth()||y《0||y》=this.getHeight())//判断是否撞墙
{
t=false;
result=false;
repaint();
}
int num=shu;
while(num》0) //设置蛇节位置
{
snake[num].setBounds(snake[num-1].getBounds());
num–;
}
if(shu==15) //如果蛇节数等于15则胜利
{
t=false;
result=true;
repaint();
}
}
}
public void keyPressed(KeyEvent e) //按下键盘方向键
{
if(e.getKeyCode()==KeyEvent.VK_RIGHT)//右键
{
if(fangxiang!=1)//如果先前方向不为左
fangxiang=0;
}
else if(e.getKeyCode()==KeyEvent.VK_LEFT)
{ if(fangxiang!=0)
fangxiang=1;
}
else if(e.getKeyCode()==KeyEvent.VK_UP)
{ if(fangxiang!=3)
fangxiang=2;
}
else if(e.getKeyCode()==KeyEvent.VK_DOWN)
{ if(fangxiang!=2)
fangxiang=3;
}
}
public void keyTyped(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
public void paint(Graphics g) //在面板上绘图
{
int x1=this.getWidth()-1;
int y1=this.getHeight()-1;
g.setColor(Color.red);
g.fillOval(weix, weiy, 10, 10);//食物
g.drawRect(0, 0, x1, y1); //墙
if(t==false&&result==false)
g.drawString(“GAME OVER!“, 250, 200);//输出游戏失败
else if(t==false&&result==true)
g.drawString(“YOU WIN!“, 250, 200);//输出游戏成功
}
}
class MyWindow extends Frame implements ActionListener//自定义窗口类
{
MyPanel my;
Button btn;
Panel panel;
MyWindow()
{
super(“GreedSnake“);
my=new MyPanel();
btn=new Button(“begin“);
panel=new Panel();
btn.addActionListener(this);
panel.add(new Label(“begin后请按Tab键选定蛇“));
panel.add(btn);
panel.add(new Label(“按上下左右键控制蛇行动“));
add(panel,BorderLayout.NORTH);
add(my,BorderLayout.CENTER);
setBounds(100,100,610,500);
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)//按下begin按钮
{
if(e.getSource()==btn)
{
try
{
my.thread.start(); //开始线程
my.validate();
}
catch(Exception ee){}
}
}
}

帮忙给个java小游戏源程序

这是一个贪食蛇的程序,仅供参考(多给点分吧,代码注释非常详细,给100分就很满足了):
/**************************************************************************
*要点分析:
*1)主要部分已经集成为一个对象SnakeModel,利用键盘控制实现操作。
*************************************************************************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
//=============================================
//Main Class
//=============================================
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();
}
}
/**************************************************************************
*要点分析:
*1)数据结构:matrix用来存储地图上面的信息,如果什么也没有设置为false,
* 如果有食物或蛇,设置为true;nodeArray,一个LinkedList,用来保存蛇的每
* 一节;food用来保存食物的位置;而Node类是保存每个位置的信息。
*2)重要函数:
* changeDirection(int newDirection) ,用来改变蛇前进的方向,而且只是
* 保存头部的前进方向,因为其他的前进方向已经用位置来指明了。 其中newDirection
* 必须和原来的direction不是相反方向,所以相反方向的值用了同样的奇偶性。在测试
* 的时候使用了direction%2!=newDirection%2 进行判断。
* moveOn(),用来更新蛇的位置,对于当前方向,把头部位置进行相应改变。如果越界,
* 结束;否则,检测是否遇到食物(加头部)或身体(结束);如果什么都没有,加上头部,
* 去掉尾部。由于用了LinkedList数据结构,省去了相当多的麻烦。
*************************************************************************/
//———————————————————————-
//Node:结点类
//———————————————————————-
class Node
{
int x;
int y;
Node(int x,int y)
{
this.x=x;
this.y=y;
}
}
//———————————————————————-
//SnakeModel:贪吃蛇模型
//———————————————————————-
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;
}
}