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

 更新时间:2024-10-12

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

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

  ⑶连连看的小源码packageLianliankan;importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publilasslianliankanimplementsActionListener{JFramemainFrame;//主面板ContainerthisContainer;JPanelcenterPanel,southPanel,northPanel;//子面板JButtondiamondsButton=newJButton;//游戏按钮数组JButtonexitButton,resetButton,newlyButton;//退出,重列,重新开始按钮JLabelfractionLable=newJLabel(““);//分数标签JButtonfirstButton,secondButton;//分别记录两次被选中的按钮intgrid=newint;//储存游戏按钮位置staticbooleanpressInformation=false;//判断是否有按钮被选中intx=,y=,x=,y=,fristMsg=,secondMsg=,validateLV;//游戏按钮的位置坐标inti,j,k,n;//消除方法控制publicvoidinit(){mainFrame=newJFrame(“JKJ连连看“);thisContainer=mainFrame.getContentPane();thisContainer.setLayout(newBorderLayout());centerPanel=newJPanel();southPanel=newJPanel();northPanel=newJPanel();thisContainer.add(centerPanel,“Center“);thisContainer.add(southPanel,“South“);thisContainer.add(northPanel,“North“);centerPanel.setLayout(newGridLayout(,));for(intcols=;cols《;cols++){for(introws=;rows《;rows++){diamondsButton[cols][rows]=newJButton(String.valueOf(grid[cols+][rows+]));diamondsButton[cols][rows].addActionListener(this);centerPanel.add(diamondsButton[cols][rows]);}}exitButton=newJButton(“退出“);exitButton.addActionListener(this);resetButton=newJButton(“重列“);resetButton.addActionListener(this);newlyButton=newJButton(“再来一局“);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(,,,);mainFrame.setVisible(true);}publicvoidrandomBuild(){intrandoms,cols,rows;for(inttwins=;twins《=;twins++){randoms=(int)(Math.random()*+);for(intalike=;alike《=;alike++){cols=(int)(Math.random()*+);rows=(int)(Math.random()*+);while(grid[cols][rows]!=){cols=(int)(Math.random()*+);rows=(int)(Math.random()*+);}this.grid[cols][rows]=randoms;}}}publicvoidfraction(){fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+));}publicvoidreload(){intsave=newint;intn=,cols,rows;intgrid=newint;for(inti=;i《=;i++){for(intj=;j《=;j++){if(this.grid[i][j]!=){save[n]=this.grid[i][j];n++;}}}n=n-;this.grid=grid;while(n》=){cols=(int)(Math.random()*+);rows=(int)(Math.random()*+);while(grid[cols][rows]!=){cols=(int)(Math.random()*+);rows=(int)(Math.random()*+);}this.grid[cols][rows]=save[n];n--;}mainFrame.setVisible(false);pressInformation=false;//这里一定要将按钮点击信息归为初始init();for(inti=;i《;i++){for(intj=;j《;j++){if(grid[i+][j+]==)diamondsButton[i][j].setVisible(false);}}}publicvoidestimateEven(intplaceX,intplaceY,JButtonbz){if(pressInformation==false){x=placeX;y=placeY;secondMsg=grid[x][y];secondButton=bz;pressInformation=true;}else{x=x;y=y;fristMsg=secondMsg;firstButton=secondButton;x=placeX;y=placeY;secondMsg=grid[x][y];secondButton=bz;if(fristMsg==secondMsg&&secondButton!=firstButton){xiao();}}}publicvoidxiao(){//相同的情况下能不能消去。仔细分析,不一条条注释if((x==x&&(y==y+||y==y-))||((x==x+||x==x-)&&(y==y))){//判断是否相邻remove();}else{for(j=;j《;j++){if(grid[x][j]==){//判断第一个按钮同行哪个按钮为空if(y》j){//如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边for(i=y-;i》=j;i--){//判断第二按钮左侧直到第一按钮中间有没有按钮if(grid[x][i]!=){k=;break;}else{k=;}//K=说明通过了第一次验证}if(k==){linePassOne();}}if(y《j){//如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边for(i=y+;i《=j;i++){//判断第二按钮左侧直到第一按钮中间有没有按钮if(grid[x][i]!=){k=;break;}else{k=;}}if(k==){linePassOne();}}if(y==j){linePassOne();}}if(k==){if(x==x){remove();}if(x《x){for(n=x;n《=x-;n++){if(grid[n][j]!=){k=;break;}if(grid[n][j]==&&n==x-){remove();}}}if(x》x){for(n=x;n》=x+;n--){if(grid[n][j]!=){k=;break;}if(grid[n][j]==&&n==x+){remove();}}}}}for(i=;i《;i++){//列if(grid[i][y]==){if(x》i){for(j=x-;j》=i;j--){if(grid[j][y]!=){k=;break;}else{k=;}}if(k==){rowPassOne();}}if(x《i){for(j=x+;j《=i;j++){if(grid[j][y]!=){k=;break;}else{k=;}}if(k==){rowPassOne();}}if(x==i){rowPassOne();}}if(k==){if(y==y){remove();}if(y《y){for(n=y;n《=y-;n++){if(grid[i][n]!=){k=;break;}if(grid[i][n]==&&n==y-){remove();}}}if(y》y){for(n=y;n》=y+;n--){if(grid[i][n]!=){k=;break;}if(grid[i][n]==&&n==y+){remove();}}}}}}}publicvoidlinePassOne(){if(y》j){//第一按钮同行空按钮在左边for(i=y-;i》=j;i--){//判断第一按钮同左侧空按钮之间有没按钮if(grid[x][i]!=){k=;break;}else{k=;}//K=说明通过了第二次验证}}if(y《j){//第一按钮同行空按钮在与第二按钮之间for(i=y+;i《=j;i++){if(grid[x][i]!=){k=;break;}else{k=;}}}}publicvoidrowPassOne(){if(x》i){for(j=x-;j》=i;j--){if(grid[j][y]!=){k=;break;}else{k=;}}}if(x《i){for(j=x+;j《=i;j++){if(grid[j][y]!=){k=;break;}else{k=;}}}}publicvoidremove(){firstButton.setVisible(false);secondButton.setVisible(false);fraction();pressInformation=false;k=;grid[x][y]=;grid[x][y]=;}publicvoidactionPerformed(ActionEvente){if(e.getSource()==newlyButton){intgrid=newint;this.grid=grid;randomBuild();mainFrame.setVisible(false);pressInformation=false;init();}if(e.getSource()==exitButton)System.exit();if(e.getSource()==resetButton)reload();for(intcols=;cols《;cols++){for(introws=;rows《;rows++){if(e.getSource()==diamondsButton[cols][rows])estimateEven(cols+,rows+,diamondsButton[cols][rows]);}}}publicstaticvoidmain(Stringargs){lianliankanllk=newlianliankan();llk.randomBuild();llk.init();}}//oldlines//newlines

  ⑷java小游戏源代码

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

  ⑹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?jPanel?=?new?JPanel();

  ⒕JButton?jButton?=?new?JButton();

  ⒖JSlider?jSlider?=?new?JSlider();

  ⒗JLabel?jLabel?=?new?JLabel();

  ⒘JButton?jButton?=?new?JButton();

  ⒙JLabel?jLabel?=?new?JLabel();

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

  ⒛int?zhengque?=?,?cuowu?=?;

  int?rush?=?{??,?,?};?????//游戏每关的个数?可以自由添加.列?{??,?,?,,}

  int?rush_count?=?;??????//记录关数

  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’,?’’,?’’,?’’,?’’,?’’,?’’,?’’,?’’,?’’?};???????????//随机出现的数字?可以自由添加

  Vector?number?=?new?Vector();

  String?paiduan?=?“true“;

  AudioClip?Musci_anjian,?Music_shibai,?Music_chenggong;

  public?Frame()?{

  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());

  //---------------------------------------

  }?catch?(Exception?exception)?{

  exception.printStackTrace();

  *?ponent?initialization.

  *?throws?java.lang.Exception

  private?void?jbInit()?throws?Exception?{

  contentPane?=?(JPanel)?getContentPane();

  contentPane.setLayout(null);

  setSize(new?Dimension(,?));

  setTitle(“Frame?Title“);

  jPanel.setBorder(BorderFactory.createEtchedBorder());

  jPanel.setBounds(new?Rectangle(,?,?,?));

  jPanel.setLayout(null);

  jButton.setBounds(new?Rectangle(,?,?,?));

  jButton.setText(“开始“);

  jButton.addActionListener(new?Frame_jButton_actionAdapter(this));

  jSlider.setBounds(new?Rectangle(,?,?,?));

  jSlider.setMaximum();

  jSlider.setMinimum();

  jSlider.setValue();

  jLabel.setText(“速度“);

  jLabel.setBounds(new?Rectangle(,?,?,?));

  jButton.setBounds(new?Rectangle(,?,?,?));

  jButton.setText(“结束“);

  jButton.addActionListener(new?Frame_jButton_actionAdapter(this));

  jLabel.setText(“第一关:个“);

  jLabel.setBounds(new?Rectangle(,?,?,?));

  contentPane.add(jPanel);

  contentPane.add(jButton);

  contentPane.add(jButton);

  contentPane.add(jSlider);

  contentPane.add(jLabel);

  contentPane.add(jLabel);

  this.addKeyListener(new?MyListener());

  jButton.addKeyListener(new?MyListener());

  jSlider.addKeyListener(new?MyListener());

  jSlider.addChangeListener(new?ChangeListener()?{

  public?void?stateChanged(ChangeEvent?e)?{

  rapidity?=?jSlider.getValue();

  public?void?run()?{

  number.clear();

  zhengque?=?;

  paiduan?=?“true“;

  while?(count?《=?rush[rush_count])?{

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

  t.start();

  count?+=?;

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

  //?最快快.最慢秒

  }?catch?(InterruptedException?e)?{

  e.printStackTrace();

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

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

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

  zhengque?=?;

  if?(cuowu?==?)?{

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

  if?(zhengque?/?cuowu?》=?)?{

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

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

  if?(rush_count?《?rush.length)?{

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

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

  jSlider.setValue(rapidity);?//?选择位置

  Thread?t?=?new?Thread(this);

  t.start();

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

  rush_count?=?;

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

  rush_count?=?;

  rush_count?=?;

  public?void?jButton_actionPerformed(ActionEvent?e)?{

  Thread?t?=?new?Thread(this);

  t.start();

  public?void?jButton_actionPerformed(ActionEvent?e)?{

  count?=?rush[rush_count]?+?;

  paiduan?=?“flase“;

  class?Tthread?implements?Runnable?{

  public?void?run()?{

  boolean?fo?=?true;

  int?Y?=?,?X?=?;

  JLabel?show?=?new?JLabel();

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

  jPanel.add(show);

  X?=??+?(int)?(Math.random()?*?);

  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?+=?,?,?));

  Thread.sleep(rapidity);

  }?catch?(InterruptedException?e)?{

  e.printStackTrace();

  if?(Y?》=?)?{

  fo?=?false;

  for?(int?i?=?number.size()?-?;?i?》=?;?i--)?{

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

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

  cuowu?+=?;

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

  number.removeElementAt(i);

  Music_shibai.play();

  class?MyListener?extends?KeyAdapter?{

  public?void?keyPressed(KeyEvent?e)?{

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

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

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

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

  zhengque?+=?;

  number.removeElementAt(i);

  bean.getShow().setVisible(false);

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

  Music_chenggong.play();

  Musci_anjian.play();

  public?static?void?main(String?args)?{

  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)?/?,

  (screenSize.height?-?frameSize.height)?/?);

  frame.setVisible(true);

  class?Frame_jButton_actionAdapter?implements?ActionListener?{

  private?Frame?adaptee;

  Frame_jButton_actionAdapter(Frame?adaptee)?{

  this.adaptee?=?adaptee;

  public?void?actionPerformed(ActionEvent?e)?{

  adaptee.jButton_actionPerformed(e);

  class?Frame_jButton_actionAdapter?implements?ActionListener?{

  private?Frame?adaptee;

  Frame_jButton_actionAdapter(Frame?adaptee)?{

  this.adaptee?=?adaptee;

  public?void?actionPerformed(ActionEvent?e)?{

  adaptee.jButton_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小游戏的代码

  连连看的小源码packageLianliankan;importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publilasslianliankanimplementsActionListener{JFramemainFrame;//主面板ContainerthisContainer;JPanelcenterPanel,southPanel,northPanel;//子面板JButtondiamondsButton=newJButton;//游戏按钮数组JButtonexitButton,resetButton,newlyButton;//退出,重列,重新开始按钮JLabelfractionLable=newJLabel(““);//分数标签JButtonfirstButton,secondButton;//分别记录两次被选中的按钮intgrid=newint;//储存游戏按钮位置staticbooleanpressInformation=false;//判断是否有按钮被选中intx=,y=,x=,y=,fristMsg=,secondMsg=,validateLV;//游戏按钮的位置坐标inti,j,k,n;//消除方法控制publicvoidinit(){mainFrame=newJFrame(“JKJ连连看“);thisContainer=mainFrame.getContentPane();thisContainer.setLayout(newBorderLayout());centerPanel=newJPanel();southPanel=newJPanel();northPanel=newJPanel();thisContainer.add(centerPanel,“Center“);thisContainer.add(southPanel,“South“);thisContainer.add(northPanel,“North“);centerPanel.setLayout(newGridLayout(,));for(intcols=;cols《;cols++){for(introws=;rows《;rows++){diamondsButton[cols][rows]=newJButton(String.valueOf(grid[cols+][rows+]));diamondsButton[cols][rows].addActionListener(this);centerPanel.add(diamondsButton[cols][rows]);}}exitButton=newJButton(“退出“);exitButton.addActionListener(this);resetButton=newJButton(“重列“);resetButton.addActionListener(this);newlyButton=newJButton(“再来一局“);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(,,,);mainFrame.setVisible(true);}publicvoidrandomBuild(){intrandoms,cols,rows;for(inttwins=;twins《=;twins++){randoms=(int)(Math.random()*+);for(intalike=;alike《=;alike++){cols=(int)(Math.random()*+);rows=(int)(Math.random()*+);while(grid[cols][rows]!=){cols=(int)(Math.random()*+);rows=(int)(Math.random()*+);}this.grid[cols][rows]=randoms;}}}publicvoidfraction(){fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+));}publicvoidreload(){intsave=newint;intn=,cols,rows;intgrid=newint;for(inti=;i《=;i++){for(intj=;j《=;j++){if(this.grid[i][j]!=){save[n]=this.grid[i][j];n++;}}}n=n-;this.grid=grid;while(n》=){cols=(int)(Math.random()*+);rows=(int)(Math.random()*+);while(grid[cols][rows]!=){cols=(int)(Math.random()*+);rows=(int)(Math.random()*+);}this.grid[cols][rows]=save[n];n--;}mainFrame.setVisible(false);pressInformation=false;//这里一定要将按钮点击信息归为初始init();for(inti=;i《;i++){for(intj=;j《;j++){if(grid[i+][j+]==)diamondsButton[i][j].setVisible(false);}}}publicvoidestimateEven(intplaceX,intplaceY,JButtonbz){if(pressInformation==false){x=placeX;y=placeY;secondMsg=grid[x][y];secondButton=bz;pressInformation=true;}else{x=x;y=y;fristMsg=secondMsg;firstButton=secondButton;x=placeX;y=placeY;secondMsg=grid[x][y];secondButton=bz;if(fristMsg==secondMsg&&secondButton!=firstButton){xiao();}}}publicvoidxiao(){//相同的情况下能不能消去。仔细分析,不一条条注释if((x==x&&(y==y+||y==y-))||((x==x+||x==x-)&&(y==y))){//判断是否相邻remove();}else{for(j=;j《;j++){if(grid[x][j]==){//判断第一个按钮同行哪个按钮为空if(y》j){//如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边for(i=y-;i》=j;i--){//判断第二按钮左侧直到第一按钮中间有没有按钮if(grid[x][i]!=){k=;break;}else{k=;}//K=说明通过了第一次验证}if(k==){linePassOne();}}if(y《j){//如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边for(i=y+;i《=j;i++){//判断第二按钮左侧直到第一按钮中间有没有按钮if(grid[x][i]!=){k=;break;}else{k=;}}if(k==){linePassOne();}}if(y==j){linePassOne();}}if(k==){if(x==x){remove();}if(x《x){for(n=x;n《=x-;n++){if(grid[n][j]!=){k=;break;}if(grid[n][j]==&&n==x-){remove();}}}if(x》x){for(n=x;n》=x+;n--){if(grid[n][j]!=){k=;break;}if(grid[n][j]==&&n==x+){remove();}}}}}for(i=;i《;i++){//列if(grid[i][y]==){if(x》i){for(j=x-;j》=i;j--){if(grid[j][y]!=){k=;break;}else{k=;}}if(k==){rowPassOne();}}if(x《i){for(j=x+;j《=i;j++){if(grid[j][y]!=){k=;break;}else{k=;}}if(k==){rowPassOne();}}if(x==i){rowPassOne();}}if(k==){if(y==y){remove();}if(y《y){for(n=y;n《=y-;n++){if(grid[i][n]!=){k=;break;}if(grid[i][n]==&&n==y-){remove();}}}if(y》y){for(n=y;n》=y+;n--){if(grid[i][n]!=){k=;break;}if(grid[i][n]==&&n==y+){remove();}}}}}}}publicvoidlinePassOne(){if(y》j){//第一按钮同行空按钮在左边for(i=y-;i》=j;i--){//判断第一按钮同左侧空按钮之间有没按钮if(grid[x][i]!=){k=;break;}else{k=;}//K=说明通过了第二次验证}}if(y《j){//第一按钮同行空按钮在与第二按钮之间for(i=y+;i《=j;i++){if(grid[x][i]!=){k=;break;}else{k=;}}}}publicvoidrowPassOne(){if(x》i){for(j=x-;j》=i;j--){if(grid[j][y]!=){k=;break;}else{k=;}}}if(x《i){for(j=x+;j《=i;j++){if(grid[j][y]!=){k=;break;}else{k=;}}}}publicvoidremove(){firstButton.setVisible(false);secondButton.setVisible(false);fraction();pressInformation=false;k=;grid[x][y]=;grid[x][y]=;}publicvoidactionPerformed(ActionEvente){if(e.getSource()==newlyButton){intgrid=newint;this.grid=grid;randomBuild();mainFrame.setVisible(false);pressInformation=false;init();}if(e.getSource()==exitButton)System.exit();if(e.getSource()==resetButton)reload();for(intcols=;cols《;cols++){for(introws=;rows《;rows++){if(e.getSource()==diamondsButton[cols][rows])estimateEven(cols+,rows+,diamondsButton[cols][rows]);}}}publicstaticvoidmain(Stringargs){lianliankanllk=newlianliankan();llk.randomBuild();llk.init();}}//oldlines//newlines

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

  单人版五子棋,不用导入,直接新建一个mywindow类就行,然后把一下代码粘贴就Ok了。或者,直接用dos就可以了。。---------------------importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;classmypanelextendsPanelimplementsMouseListener{intchess=newint;booleanIs_Black_True;mypanel(){Is_Black_True=true;for(inti=;i《;i++){for(intj=;j《;j++){chess[i][j]=;}}addMouseListener(this);setBackground(Color.BLUE);setBounds(,,,);setVisible(true);}publicvoidmousePressed(MouseEvente){intx=e.getX();inty=e.getY();if(x《||x》+||y《||y》+){return;}if(chess[x/-][y/-]!=){return;}if(Is_Black_True==true){chess[x/-][y/-]=;Is_Black_True=false;repaint();Justisewiner();return;}if(Is_Black_True==false){chess[x/-][y/-]=;Is_Black_True=true;repaint();Justisewiner();return;}}voidDrawline(Graphicsg){for(inti=;i《=;i+=){for(intj=;j《=;j+=){g.setColor(Color.WHITE);g.drawLine(i,j,i,);}}for(intj=;j《=;j+=){g.setColor(Color.WHITE);g.drawLine(,j,,j);}}voidDrawchess(Graphicsg){for(inti=;i《;i++){for(intj=;j《;j++){if(chess[i][j]==){g.setColor(Color.BLACK);g.fillOval((i+)*-,(j+)*-,,);}if(chess[i][j]==){g.setColor(Color.WHITE);g.fillOval((i+)*-,(j+)*-,,);}}}}voidJustisewiner(){intblack_count=;intwhite_count=;inti=;for(i=;i《;i++)//横向判断{for(intj=;j《;j++){if(chess[i][j]==){black_count++;if(black_count==){JOptionPane.showMessageDialog(this,“黑棋胜利“);Clear_Chess();return;}}else{black_count=;}if(chess[i][j]==){white_count++;if(white_count==){JOptionPane.showMessageDialog(this,“白棋胜利“);Clear_Chess();return;}}else{white_count=;}}}for(i=;i《;i++)//竖向判断{for(intj=;j《;j++){if(chess[j][i]==){black_count++;if(black_count==){JOptionPane.showMessageDialog(this,“黑棋胜利“);Clear_Chess();return;}}else{black_count=;}if(chess[j][i]==){white_count++;if(white_count==){JOptionPane.showMessageDialog(this,“白棋胜利“);Clear_Chess();return;}}else{white_count=;}}}for(i=;i《;i++)//左向右斜判断{for(intj=;j《;j++){for(intk=;k《;k++){if(chess[i+k][j+k]==){black_count++;if(black_count==){JOptionPane.showMessageDialog(this,“黑棋胜利“);Clear_Chess();return;}}else{black_count=;}if(chess[i+k][j+k]==){white_count++;if(white_count==){JOptionPane.showMessageDialog(this,“白棋胜利“);Clear_Chess();return;}}else{white_count=;}}}}for(i=;i《;i++)//右向左斜判断{for(intj=;j》=;j--){for(intk=;k《;k++){if(chess[i-k][j+k]==){black_count++;if(black_count==){JOptionPane.showMessageDialog(this,“黑棋胜利“);Clear_Chess();return;}}else{black_count=;}if(chess[i-k][j+k]==){white_count++;if(white_count==){JOptionPane.showMessageDialog(this,“白棋胜利“);Clear_Chess();return;}}else{white_count=;}}}}}voidClear_Chess(){for(inti=;i《;i++){for(intj=;j《;j++){chess[i][j]=;}}repaint();}publicvoidpaint(Graphicsg){Drawline(g);Drawchess(g);}publicvoidmouseExited(MouseEvente){}publicvoidmouseEntered(MouseEvente){}publicvoidmouseReleased(MouseEvente){}publicvoidmouseClicked(MouseEvente){}}classmyframeextendsFrameimplementsWindowListener{mypanelpanel;myframe(){setLayout(null);panel=newmypanel();add(panel);panel.setBounds(,,,);setTitle(“单人版五子棋“);setBounds(,,,);setVisible(true);addWindowListener(this);}publicvoidwindowClosing(WindowEvente){System.exit();}publicvoidwindowDeactivated(WindowEvente){}publicvoidwindowActivated(WindowEvente){}publicvoidwindowOpened(WindowEvente){}publicvoidwindowClosed(WindowEvente){}publicvoidwindowIconified(WindowEvente){}publicvoidwindowDeiconified(WindowEvente){}}publilassmywindow{publicstaticvoidmain(Stringargc){myframef=newmyframe();}}

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

  贪吃蛇importjava.awt.*;importjava.awt.event.*;publilassGreedSnake//主类{/***paramargs*/publicstaticvoidmain(Stringargs){//TODOAuto-generatedmethodstubnewMyWindow();}}classMyPanelextendsPanelimplementsKeyListener,Runnable//自定义面板类,继承了键盘和线程接口{Buttonsnake;//定义蛇按钮intshu=;//蛇的节数intfood;//食物数组booleanresult=true;//判定结果是输还是赢Threadthread;//定义线程staticintweix,weiy;//食物位置booleant=true;//判定游戏是否结束intfangxiang=;//蛇移动方向intx=,y=;//蛇头位置MyPanel(){setLayout(null);snake=newButton;food=newint;thread=newThread(this);for(intj=;j《;j++){food[j]=(int)(Math.random()*);//定义个随机食物}weix=(int)(food*.)*;//十位*为横坐标weiy=(int)(food%)*;//个位*为纵坐标for(inti=;i《;i++){snake[i]=newButton();}add(snake);snake.setBackground(Color.black);snake.addKeyListener(this);//为蛇头添加键盘监视器snake.setBounds(,,,);setBackground(Color.cyan);}publicvoidrun()//接收线程{while(t){if(fangxiang==)//向右{try{x+=;snake.setLocation(x,y);//设置蛇头位置if(x==weix&&y==weiy)//吃到食物{shu++;weix=(int)(food[shu]*.)*;weiy=(int)(food[shu]%)*;repaint();//重绘下一个食物add(snake[shu]);//增加蛇节数和位置snake[shu].setBounds(snake[shu-].getBounds());}thread.sleep();//睡眠ms}catch(Exceptione){}}elseif(fangxiang==)//向左{try{x-=;snake.setLocation(x,y);if(x==weix&&y==weiy){shu++;weix=(int)(food[shu]*.)*;weiy=(int)(food[shu]%)*;repaint();add(snake[shu]);snake[shu].setBounds(snake[shu-].getBounds());}thread.sleep();}catch(Exceptione){}}elseif(fangxiang==)//向上{try{y-=;snake.setLocation(x,y);if(x==weix&&y==weiy){shu++;weix=(int)(food[shu]*.)*;weiy=(int)(food[shu]%)*;repaint();add(snake[shu]);snake[shu].setBounds(snake[shu-].getBounds());}thread.sleep();}catch(Exceptione){}}elseif(fangxiang==)//向下{try{y+=;snake.setLocation(x,y);if(x==weix&&y==weiy){shu++;weix=(int)(food[shu]*.)*;weiy=(int)(food[shu]%)*;repaint();add(snake[shu]);snake[shu].setBounds(snake[shu-].getBounds());}thread.sleep();}catch(Exceptione){}}intnum=shu;while(num》)//判断是否咬自己的尾巴{if(snake[num].getBounds().x==snake.getBounds().x&&snake[num].getBounds().y==snake.getBounds().y){t=false;result=false;repaint();}num--;}if(x《||x》=this.getWidth()||y《||y》=this.getHeight())//判断是否撞墙{t=false;result=false;repaint();}intnum=shu;while(num》)//设置蛇节位置{snake[num].setBounds(snake[num-].getBounds());num--;}if(shu==)//如果蛇节数等于则胜利{t=false;result=true;repaint();}}}publicvoidkeyPressed(KeyEvente)//按下键盘方向键{if(e.getKeyCode()==KeyEvent.VK_RIGHT)//右键{if(fangxiang!=)//如果先前方向不为左fangxiang=;}elseif(e.getKeyCode()==KeyEvent.VK_LEFT){if(fangxiang!=)fangxiang=;}elseif(e.getKeyCode()==KeyEvent.VK_UP){if(fangxiang!=)fangxiang=;}elseif(e.getKeyCode()==KeyEvent.VK_DOWN){if(fangxiang!=)fangxiang=;}}publicvoidkeyTyped(KeyEvente){}publicvoidkeyReleased(KeyEvente){}publicvoidpaint(Graphicsg)//在面板上绘图{intx=this.getWidth()-;inty=this.getHeight()-;g.setColor(Color.red);g.fillOval(weix,weiy,,);//食物g.drawRect(,,x,y);//墙if(t==false&&result==false)g.drawString(“GAMEOVER!“,,);//输出游戏失败elseif(t==false&&result==true)g.drawString(“YOUWIN!“,,);//输出游戏成功}}classMyWindowextendsFrameimplementsActionListener//自定义窗口类{MyPanelmy;Buttonbtn;Panelpanel;MyWindow(){super(“GreedSnake“);my=newMyPanel();btn=newButton(“begin“);panel=newPanel();btn.addActionListener(this);panel.add(newLabel(“begin后请按Tab键选定蛇“));panel.add(btn);panel.add(newLabel(“按上下左右键控制蛇行动“));add(panel,BorderLayout.NORTH);add(my,BorderLayout.CENTER);setBounds(,,,);setVisible(true);validate();addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit();}});}publicvoidactionPerformed(ActionEvente)//按下begin按钮{if(e.getSource()==btn){try{my.thread.start();//开始线程my.validate();}catch(Exceptionee){}}}}

  帮忙给个java小游戏源程序

  这是一个贪食蛇的程序,仅供参考(多给点分吧,代码注释非常详细,给分就很满足了:/***************************************************************************要点分析:*主要部分已经集成为一个对象SnakeModel,利用键盘控制实现操作。*************************************************************************/importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjava.util.*;//=============================================//MainClass//=============================================publilassGreedSnakeimplementsKeyListener{JFramemainFrame;CanvaspaintCanvas;JLabellabelScore;//计分牌SnakeModelsnakeModel=null;//蛇publicstaticfinalintcanvasWidth=;publicstaticfinalintcanvasHeight=;publicstaticfinalintnodeWidth=;publicstaticfinalintnodeHeight=;//----------------------------------------------------------------------//GreedSnake():初始化游戏界面//----------------------------------------------------------------------publicGreedSnake(){//设置界面元素mainFrame=newJFrame(“GreedSnake“);Containercp=mainFrame.getContentPane();labelScore=newJLabel(“Score:“);cp.add(labelScore,BorderLayout.NORTH);paintCanvas=newCanvas();paintCanvas.setSize(canvasWidth+,canvasHeight+);paintCanvas.addKeyListener(this);cp.add(paintCanvas,BorderLayout.CENTER);JPanelpanelButtom=newJPanel();panelButtom.setLayout(newBorderLayout());JLabellabelHelp;//帮助信息labelHelp=newJLabel(“PageUp,PageDownforspeed;“,JLabel.CENTER);panelButtom.add(labelHelp,BorderLayout.NORTH);labelHelp=newJLabel(“ENTERorRorSforstart;“,JLabel.CENTER);panelButtom.add(labelHelp,BorderLayout.CENTER);labelHelp=newJLabel(“SPACEorPforpause“,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():按键检测//----------------------------------------------------------------------publicvoidkeyPressed(KeyEvente){intkeyCode=e.getKeyCode();if(snakeModel.running)switch(keyCode){caseKeyEvent.VK_UP:snakeModel.changeDirection(SnakeModel.UP);break;caseKeyEvent.VK_DOWN:snakeModel.changeDirection(SnakeModel.DOWN);break;caseKeyEvent.VK_LEFT:snakeModel.changeDirection(SnakeModel.LEFT);break;caseKeyEvent.VK_RIGHT:snakeModel.changeDirection(SnakeModel.RIGHT);break;caseKeyEvent.VK_ADD:caseKeyEvent.VK_PAGE_UP:snakeModel.speedUp();//加速break;caseKeyEvent.VK_SUBTRACT:caseKeyEvent.VK_PAGE_DOWN:snakeModel.speedDown();//减速break;caseKeyEvent.VK_SPACE:caseKeyEvent.VK_P:snakeModel.changePauseState();//暂停或继续break;default:}//重新开始if(keyCode==KeyEvent.VK_R||keyCode==KeyEvent.VK_S||keyCode==KeyEvent.VK_ENTER){snakeModel.running=false;begin();}}//----------------------------------------------------------------------//keyReleased(:空函数//----------------------------------------------------------------------publicvoidkeyReleased(KeyEvente){}//----------------------------------------------------------------------//keyTyped(:空函数//----------------------------------------------------------------------publicvoidkeyTyped(KeyEvente){}//----------------------------------------------------------------------//repaint(:绘制游戏界面(包括蛇和食物//----------------------------------------------------------------------voidrepaint(){Graphicsg=paintCanvas.getGraphics();//drawbackgroundg.setColor(Color.WHITE);g.fillRect(,,canvasWidth,canvasHeight);//drawthesnakeg.setColor(Color.BLACK);LinkedListna=snakeModel.nodeArray;Iteratorit=na.iterator();while(it.hasNext()){Noden=(Node)it.next();drawNode(g,n);}//drawthefoodg.setColor(Color.RED);Noden=snakeModel.food;drawNode(g,n);updateScore();}//----------------------------------------------------------------------//drawNode(:绘画某一结点(蛇身或食物//----------------------------------------------------------------------privatevoiddrawNode(Graphicsg,Noden){g.fillRect(n.x*nodeWidth,n.y*nodeHeight,nodeWidth-,nodeHeight-);}//----------------------------------------------------------------------//updateScore(:改变计分牌//----------------------------------------------------------------------publicvoidupdateScore(){Strings=“Score:“+snakeModel.score;labelScore.setText(s);}//----------------------------------------------------------------------//begin(:游戏开始,放置贪吃蛇//----------------------------------------------------------------------voidbegin(){if(snakeModel==null||!snakeModel.running){snakeModel=newSnakeModel(this,canvasWidth/nodeWidth,this.canvasHeight/nodeHeight);(newThread(snakeModel)).start();}}//----------------------------------------------------------------------//main(:主函数//----------------------------------------------------------------------publicstaticvoidmain(Stringargs){GreedSnakegs=newGreedSnake();}}/***************************************************************************要点分析:*数据结构:matrix用来存储地图上面的信息,如果什么也没有设置为false,*如果有食物或蛇,设置为true;nodeArray,一个LinkedList,用来保存蛇的每*一节;food用来保存食物的位置;而Node类是保存每个位置的信息。*重要函数:*changeDirection(intnewDirection),用来改变蛇前进的方向,而且只是*保存头部的前进方向,因为其他的前进方向已经用位置来指明了。其中newDirection*必须和原来的direction不是相反方向,所以相反方向的值用了同样的奇偶性。在测试*的时候使用了direction%!=newDirection%进行判断。*moveOn(),用来更新蛇的位置,对于当前方向,把头部位置进行相应改变。如果越界,*结束;否则,检测是否遇到食物(加头部或身体(结束;如果什么都没有,加上头部,*去掉尾部。由于用了LinkedList数据结构,省去了相当多的麻烦。*************************************************************************///----------------------------------------------------------------------//Node:结点类//----------------------------------------------------------------------classNode{intx;inty;Node(intx,inty){this.x=x;this.y=y;}}//----------------------------------------------------------------------//SnakeModel:贪吃蛇模型//----------------------------------------------------------------------classSnakeModelimplementsRunnable{GreedSnakegs;booleanmatrix;//界面数据保存在数组里LinkedListnodeArray=newLinkedList();Nodefood;intmaxX;//最大宽度intmaxY;//最大长度intdirection=;//方向booleanrunning=false;inttimeInterval=;//间隔时间(速度doublespeedChangeRate=.;//速度改变程度booleanpaused=false;//游戏状态intscore=;intcountMove=;//UP和DOWN是偶数,RIGHT和LEFT是奇数publicstaticfinalintUP=;publicstaticfinalintDOWN=;publicstaticfinalintLEFT=;publicstaticfinalintRIGHT=;//----------------------------------------------------------------------//GreedModel():初始化界面//----------------------------------------------------------------------publicSnakeModel(GreedSnakegs,intmaxX,intmaxY){this.gs=gs;this.maxX=maxX;this.maxY=maxY;matrix=newboolean[maxX];for(inti=;i《maxX;++i){matrix[i]=newboolean[maxY];Arrays.fill(matrix[i],false);//没有蛇和食物的地区置false}//初始化贪吃蛇intinitArrayLength=maxX》?:maxX/;for(inti=;i《initArrayLength;++i){intx=maxX/+i;inty=maxY/;nodeArray.addLast(newNode(x,y));matrix[x][y]=true;//蛇身处置true}food=createFood();matrix[food.x][food.y]=true;//食物处置true}//----------------------------------------------------------------------//changeDirection():改变运动方向//----------------------------------------------------------------------publicvoidchangeDirection(intnewDirection){if(direction%!=newDirection%)//避免冲突{direction=newDirection;}}//----------------------------------------------------------------------//moveOn():贪吃蛇运动函数//----------------------------------------------------------------------publicbooleanmoveOn(){Noden=(Node)nodeArray.getFirst();intx=n.x;inty=n.y;switch(direction){caseUP:y--;break;caseDOWN:y++;break;caseLEFT:x--;break;caseRIGHT:x++;break;}if((《=x&&x《maxX)&&(《=y&&y《maxY)){if(matrix[x][y])//吃到食物或者撞到身体{if(x==food.x&&y==food.y)//吃到食物{nodeArray.addFirst(food);//在头部加上一结点//计分规则与移动长度和速度有关intscoreGet=(-*countMove)/timeInterval;score+=scoreGet》?scoreGet:;countMove=;food=createFood();matrix[food.x][food.y]=true;returntrue;}elsereturnfalse;//撞到身体}else//什么都没有碰到{nodeArray.addFirst(newNode(x,y));//加上头部matrix[x][y]=true;n=(Node)nodeArray.removeLast();//去掉尾部matrix[n.x][n.y]=false;countMove++;returntrue;}}returnfalse;//越界(撞到墙壁}//----------------------------------------------------------------------//run():贪吃蛇运动线程//----------------------------------------------------------------------publicvoidrun(){running=true;while(running){try{Thread.sleep(timeInterval);}catch(Exceptione){break;}if(!paused){if(moveOn())//未结束{gs.repaint();}else//游戏结束{JOptionPane.showMessageDialog(null,“GAMEOVER“,“GameOver“,JOptionPane.INFORMATION_MESSAGE);break;}}}running=false;}//----------------------------------------------------------------------//createFood():生成食物及放置地点//----------------------------------------------------------------------privateNodecreateFood(){intx=;inty=;do{Randomr=newRandom();x=r.nextInt(maxX);y=r.nextInt(maxY);}while(matrix[x][y]);returnnewNode(x,y);}//----------------------------------------------------------------------//speedUp():加快蛇运动速度//----------------------------------------------------------------------publicvoidspeedUp(){timeInterval*=speedChangeRate;}//----------------------------------------------------------------------//speedDown():放慢蛇运动速度//----------------------------------------------------------------------publicvoidspeedDown(){timeInterval/=speedChangeRate;}//----------------------------------------------------------------------//changePauseState():改变游戏状态(暂停或继续//----------------------------------------------------------------------publicvoidchangePauseState(){paused=!paused;}}

您可能感兴趣的文章:

相关文章