《最新java小游戏源代码.doc》由会员分享,可在线阅读,更多相关《最新java小游戏源代码.doc(25页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精品资料java小游戏源代码.java小游戏源代码Java小游戏第一个Java文件:import java.util.Scanner;public class GameA_B public static void main(String args) Scanner reader=new Scanner(System.in); int area; System.out.println(Game StartPlease enter the area:(1-9) + n+1,2,3 means easy+n+4,5,6 means middle+n+ 7,8,9 means hard+n+Pleas
2、e choose:); area=reader.nextInt(); switch(area-1)/3) case 0:System.out.println(You choose easy! );break; case 1:System.out.println(You choose middle! );break; case 2:System.out.println(You choose hard! );break; System.out.println(Good Luck!); GameProcess game1=new GameProcess(area); game1.process();
3、第二个Java文件:import java.util.Random;import java.util.Scanner;public class GameProcess int area,i,arrcount,right,midright,t;int base=new intarrcount,userNum=new intarea,sysNum=new intarea;Random random=new Random(); Scanner reader=new Scanner(System.in);GameProcess(int a)area=a;arrcount=10;right=0;midr
4、ight=0;t=0;base=new intarrcount;userNum=new intarea;sysNum=new intarea;for(int i=0;iarrcount;i+) basei=i;/System.out.println(basei); void process() rand(); while(right!=area) scanf(); compare(); print(); check(); void rand() for(i=0;iarea;i+) t=random.nextInt(arrcount); /System.out.println(t); sysNu
5、mi=baset; System.out.println(baset); delarr(t); void delarr(int t) for(int j=t;jarrcount-1;j+) basej=basej+1; arrcount-; void scanf() System.out.println(The system number has created!+n+Please enter +area+ Numbers); for(int i=0;iarea;i+) userNumi=reader.nextShort(); void check() if(right=area) Syste
6、m.out.println(You win!); boolean check(int i) return true; void compare() int i=0,j=0; right=midright=0; for(i=0;iarea;i+) for(j=0;jarea;j+) if(userNumi=sysNumj) if(i=j) right+; else midright+; void print() System.out.println( A +right+ B +midright); import java.awt.*;import java.awt.event.*;import
7、javax.swing.*;class TestGame public static void main(String args) App ap = new App(); /调用App()开始运行程序 ap.show(); class App extends JFrame MainPanel mp; public App() mp = new MainPanel(); this.getContentPane().add(mp); this.setSize(400, 450); this.setTitle(小游戏); /* * 主面板 * 显示格子 * author Administrator
8、* */class MainPanel extends JPanel ButtonPanel bp = new ButtonPanel(); CtrlPanel rp = new CtrlPanel(); public MainPanel() this.setLayout(new BorderLayout(); rp.btnstart.addActionListener(new StartListener(); this.add(bp, Center); this.add(rp, South); class StartListener implements ActionListener /*
9、* 重新开始按钮的事件 * 调用按钮面板里面的颜色初始化方法 */ public void actionPerformed(ActionEvent e) if (e.getActionCommand() = 重新开始) bp.ColorInit(); class ButtonPanel extends JPanel JButton b = new JButton55; /* * 按钮界面的构造器 * 设置布局方式为Grid布局,并生成5*5的格子, * 在每个格子生成一个按钮, * 为每个按钮添加一个监听事件 */ public ButtonPanel() this.setLayout(new
10、 GridLayout(5, 5); for (int i = 0; i 5; i+) for (int j = 0; j 5; j+) bij = new JButton(); bij.setActionCommand( + (i + 1) + (j + 1); bij.addActionListener(new MyButtonListener(); this.add(bij); this.ColorInit(); /* * 面板初始化时候给所有的格子都绘上深灰色 * i.j分别是行和列 */ public void ColorInit() for (int i = 0; i 5; i+)
11、 for (int j = 0; j 0) /行号大于0 this.btnChange(br - 1c); if (r 0)/列号大于0 this.btnChange(brc - 1); if (c num) text2.setText(输入的数大了,输小点的!); else if (shu num) text2.setText(输入的数小了,输大点的!); else if (shu = num) text2.setText(恭喜你,猜对了!); if (i = 2) text2.setText(你真是个天才!); else if (i = 6) text2.setText(还将就,你过关了!); else if (i = 8) text2.setText(但是你还真笨!); else text2.setText(你实在是太笨了!); break;