java小游戏源代码_第1页
java小游戏源代码_第2页
java小游戏源代码_第3页
java小游戏源代码_第4页
java小游戏源代码_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Java小游戏3 / 16第一个Java文件:import java.util.Scanner;public class GameA_B public static voidmain(String口 args) Scanner reader=new Scanner(System. in );-9)"+int area; System. out .println( "Game Start Pleaseenter the area:(1'n'+"1,2,3 means easy" +'n'+"4,5,6 means

2、middle"+'n“7,8,9 means hard" +'n' +"Please choose:" );area=reader.nextInt();switch (area-1)/3) case0:System.out.println("You choose easy!");break ;case1:System.out.println("You choose middle!");breakcase2:System.out.println("You choose hard!&q

3、uot;);break ;System. out .println( "Good Luck!" );GameProcess game1= new GameProcess(area); cess();第二个Java文件:import java.util.Random;import java.util.Scanner;newpublic class GameProcess | intarea , i , arrcount , right, midright , t ;intbase = new int arrcount , userNum = new int

4、area , sysNum 口int area ; Random random =new Random();Scanner reader =new Scanner(System. in ); GameProcess( int a) area =a; arrcount =10; right =0;midright =0;t =0;base =new int arrcount ;userNum =new int area ;sysNum =new int area ;for ( int i=0;i< arrcount ;i+) base i=i;/System.out.println(bas

5、ei);void process()r and();while ( right != area )scanf();compare();print();check();void rand()f or ( i =0; i <area ; i +)t =random .nextInt( arrcount ); /System.out.println(t);sysNum i = base t ;System. out .println( base t ); delarr( t );void delarr( int t)f or ( int j=t;j< arrcount -1;j+) ba

6、se j= base j+1;arrcount-;void scanf()+"n" +"PleaseSystem. out .println( "The system number has created! enter " +area +" Numbers" );for ( int i=0;i< area ;i+)userNum i= reader .nextShort();void check()if ( right = area )System. out .println( "You win !"

7、;);boolean check( int i)r eturn true ;void compare()i nt i=0,j=0;r ight =midright =0;f or (i=0;i< area ;i+)for (j=0;j< area ;j+)if ( userNum i= sysNum j)if (i=j)right +;elsemidright +;void print();System. out .println( " A " +right +" B " +midright4 / 16import java.awt.*;im

8、port java.awt.event.*;import 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("小游戏");/*主面

9、板*显示格子* author Administrator*/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");clas

10、s StartListener implements ActionListener /* 重新开始按钮的事件* 调用按钮面板里面的颜色初始化方法* /public void actionPerformed(ActionEvent e) if (e.getActionCommand()="重新开始")bp.ColorInit();class ButtonPanel extends JPanel JButton口口 b = new JButton55;* 按钮界面的构造器* 设置布局方式为 Grid布局,并生成5 param b5的格子,* 在每个格子生成一个按钮,* 为每个按

11、钮添加一个监听事件* /public ButtonPanel() this.setLayout(new 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();/*面板初始化时候给所有的格子都绘上深灰

12、色* i.j分别是行和列*/public void ColorInit() for (int i = 0; i < 5; i+) for (int j = 0; j < 5; j+) bij.setBackground(Color.DARK_GRAY);/* */this.add(btnstart);*按钮上监听的时事件,监听点击* author Administrator*/class MyButtonListener implements ActionListener int r, c;/*需要改变颜色的行和列* r row* c colunm* 调用change。来改变颜色*

13、/public void actionPerformed(ActionEvent e) int i = Integer.parseInt(e.getActionCommand(); r = i / 10 - 1;c = i % 10 - 1;this.changer();* 传一个按钮控件进去* 判断颜色,如果是深灰则变为粉红* 否则义相反if (b.getBackground() = Color.DARK_GRAY) b.setBackground(Color.pink); else b.setBackground(Color.DARK GRAY);* 这个方法是根据点击的按钮判断周围需要*

14、 不能超越数组的下标* /public void changer() this.btnChange(brc);if (r > 0) 行号大于0this.btnChange(br - 1c);if (r < 4)this.btnChange(br + 1c);if (c > 0)列号大于0this.btnChange(brc - 1);if (c < 4)/列好小余0this.btnChange(brc + 1);/*控制面板* author Administrator*下面的开始按钮*/ class CtrlPanel extends JPanel JButton bt

15、nstart;public CtrlPanel() btnstart = new JButton("重新开始");8 / 169 / 16import java.util.*;public class Cai enum ResSHITOU, JIANZI, BU;Res res;public static void main(String口 args) throws Exception / TODO Auto-generated method stubCai cai = new Cai();System.out.println("请输入你的选择:");S

16、ystem.out.println("0 表示石头,1表示剪子,2表示布"力 char yourResultOfChar =(char) System.in.read();int yourResultOfInt = yourResultOfChar - '0'int computerResult = pb();cai.getYourResult(yourResultOfInt);switch (computerResult)case 0:System.out.println("电脑选择石头");break;case 1:System.ou

17、t.println("电脑选择剪子");break;case 2:System.out.println("电脑选择布");break;cai.pa(computerResult);public void getYourResult(int count) Res result = Res.values();res = resultcount;void pa(int computer)Res口 result = Res.values();if(this.res = Res.SHITOU)System.out.println("我选择石头"

18、);switch(resultcomputer)case SHITOU:System.out.println("平局,再来!");break;case JIANZI:System.out.println("我赢了 !");break;case BU:System.out.println("我输了 !");break; else if(this.res = Res.JIANZI)System.out.println("我选择剪子");switch(resultcomputer)case JIANZI:System.o

19、ut.println("平局,再来!");break;case BU:System.out.println("我赢了 !");break;case SHITOU:System.out.println("我输了 !");break; else if(this.res = Res.BU)System.out.println("我选择布");switch(resultcomputer)case BU:System.out.println("平局,再来!");break;case SHITOU:Syst

20、em.out.println("我赢了 !");break;case JIANZI:System.out.println("我输了 !");break;static int pb()Random ran = new Random();int res = ran.nextInt(3);return res;输出0-2的整数,0表示石头,1表示剪子,2表示布,和enum Res中的顺序相对应 12 / 1613 / 16import java.util.*;/ 导入实用包 util下所有的类import javax.swing.*;import java.a

21、wt.*;import java.awt.event.*;public class CaiShu public static void main(String口 args) Win f = new Win();f.setVisible(true); class Win extends JFrame implements ActionListener JLabel labe;JButton butt;JButton button;Random a = new Random();private int i = 0;private int num;JTextField text1, text2;JP

22、anel p;public Win() super("猜数游戏");labe = new JLabel("我心里有个数,它是 1-100之间的,你能猜出来吗? ");butt = new JButton(" 确认");button = new JButton(" 重开");text1 = new JTextField(5);text2 = new JTextField(20);p = new JPanel();Container con = getContentPane();/ 调用 JFrame 的 getCon

23、tentPane 得到容器text2.setEditable(false);/使输出结果文本域不可编辑butt.addActionListener(this);/执行结果动作con.setLayout(new GridLayout(4, 1);/设置整个界面的长宽比p.add(textl);/添加输入数字文本域p.add(butt);p.add(button);button.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) text1.setText("");text2.setText("");i=0;);con.add(labe);/添加游戏标签con.add(p);con.add(text2);/添加输出结果信息文本域setSize(300, 30

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论