logo资料库

Java名字打架小游戏代码.doc

第1页 / 共11页
第2页 / 共11页
第3页 / 共11页
第4页 / 共11页
第5页 / 共11页
第6页 / 共11页
第7页 / 共11页
第8页 / 共11页
资料共11页,剩余部分请下载后查看
Java名字打架小游戏代码
一、截图如下:
1、初始界面:
2、游戏中界面:
3、游戏结束界面:
二、程序代码如下:
Java 名字打架小游戏代码 一、截图如下: 1、初始界面:
2、游戏中界面:
3、游戏结束界面: 二、程序代码如下: import java.awt.*; import java.awt.event.*; class Game extends Frame implements ActionListener { Label l1,l2,label0,label1,label2,label3,label4,label5,label6,label7; TextField text1,text2,text3,text4,text5,text6; Button button,button1,button2; Panel p1,p2,p3,p4;
TextArea area; Menu menu; MenuBar menubar; MenuItem item1,item2; int HP,gongji,fangyv; int count1=0,count2=0,count3=0,count4=0; Thread thread1; Game(String s) { super(s); thread1=new Thread(); label0=new Label("初始血量在2000-3000之间随机产生!"); l1=new Label("姓名1",Label.CENTER); l2=new Label("姓名2",Label.CENTER); label1=new Label("姓名1",Label.CENTER); label2=new Label("姓名2",Label.CENTER); label3=new Label("属性1",Label.CENTER); label4=new Label("属性2",Label.CENTER); label5=new Label("PK状态",Label.CENTER); label6=new Label("被治疗次数:"+count1+" 被攻击次数:"+count2); label7=new Label("被治疗次数:"+count3+" 被攻击次数:"+count4); p1=new Panel(); p2=new Panel(); p3=new Panel(); p4=new Panel(); area=new TextArea(10,30); text1=new TextField(5); text2=new TextField(5); text3=new TextField(34); text4=new TextField(34); text5=new TextField(27); text6=new TextField(27); button=new Button("确定"); button1=new Button("重新输入"); button2=new Button("退出"); thread1.start(); add(p1,BorderLayout.NORTH); add(p2,BorderLayout.CENTER); p1.add(label0); p2.add(label1); p2.add(text1); p2.add(button); p2.add(label2);
p2.add(text2); p2.add(label3); p2.add(text3); p2.add(label4); p2.add(text4); p2.add(label5); p2.add(p3); p2.add(label6); p2.add(p4); p2.add(label7); p2.add(area); p2.add(button1); p2.add(button2); p3.add(l1); p3.add(text5); p4.add(l2); p4.add(text6); l1.setLocation(0, 0); text5.setLocation(20,0); area.setEditable(false); text3.setEditable(false); text4.setEditable(false); text5.setEditable(false); text6.setEditable(false); button.setBackground(Color.LIGHT_GRAY); button1.setBackground(Color.LIGHT_GRAY); button2.setBackground(Color.LIGHT_GRAY); label0.setBackground(Color.orange); label3.setBackground(Color.cyan); label4.setBackground(Color.cyan); label5.setBackground(Color.cyan); button.addActionListener(this); button1.addActionListener(this); button2.addActionListener(this); menu=new Menu("菜单"); item1=new MenuItem("打开"); item2=new MenuItem("退出"); p2.setBackground(Color.pink); p1.setBackground(Color.orange); menubar=new MenuBar(); menu.add(item1); menu.addSeparator(); menu.add(item2); menubar.add(menu);
setMenuBar(menubar); item2.addActionListener(this); area.setBackground(Color.yellow); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setBounds(500,100,285,590); setResizable(false);//设置窗口不可调整大小 setVisible(true); validate(); } public void actionPerformed(ActionEvent e) { if(e.getSource()==item2 ||e.getSource()==button2) System.exit(0); else if(e.getSource()==button) { String name1=text1.getText(); String name2=text2.getText(); NPCC npc1=new NPCC(name1); NPCC npc2=new NPCC(name2); npc1.shuxing(); npc2.shuxing(); l1.setText(name1); l2.setText(name2); int h1,h2; h1=npc1.HP; h2=npc2.HP; text3.setText(name1+": HP:"+npc1.HP+" 攻击:"+npc1.gongji+" 防御:"+npc1.fangyv); text4.setText(name2+": HP:"+npc2.HP+" 攻击:"+npc2.gongji+" 防御:"+npc2.fangyv); int i=0; while(npc1.HP>0 && npc2.HP>0) { if (i%2==0) { String s1=npc1.damage(npc2.gongji); if(npc1.HPP!=0) {
count1++; } else { count2++; 数:"+count2); } label6.setText(" 被 治 疗 次 数 :"+count1+" 被 攻 击 次 String s=s1; npc1.HP=npc1.HP+npc1.HPP+npc1.fangyv-npc1.hurt; if(npc1.HP>h1) { area.append(name1+s+'\n'); text5.setText(name1+s); try { thread1.sleep(1000); } catch(InterruptedException ee){} npc1.HP=h1; s1="血量已达最大值!!"; } if(npc1.HP<0) { npc1.HP=0; } text3.setText(name1+": 击:"+npc1.gongji+" 防御:"+npc1.fangyv); HP:"+npc1.HP+" 攻 text5.setText(name1+s1); area.append(name1+s1+'\n'); area.append(name1+"剩余生命值:"+npc1.HP+'\n'); } else { String s2=npc2.damage(npc1.gongji); if(npc2.HPP!=0) { count3++; } else { count4++; } label7.setText(" 被 治 疗 次 数 :"+count3+" 被 攻 击 次
数:"+count4); String s=s2; npc2.HP=npc2.HP+npc2.HPP+npc2.fangyv-npc2.hurt; if(npc2.HP>h2) { text6.setText(name2+s); area.append(name2+s+'\n'); try { thread1.sleep(1000); } catch(InterruptedException ee){} npc2.HP=h2; s2="血量已达最大值!!"; } if(npc2.HP<0) { npc2.HP=0; } text4.setText(name2+": 击:"+npc2.gongji+" 防御:"+npc2.fangyv); HP:"+npc2.HP+" 攻 text6.setText(name2+s2); area.append(name2+s2+'\n'); area.append(name2+"剩余生命值:"+npc2.HP+'\n'); } try { thread1.sleep(1000); } catch(InterruptedException ee){} i++; } if(npc1.HP<=0) { text5.setText(name1+"挂了!!!\n"); text6.setText(name1+"挂了!!!\n"); area.append(name1+"挂了!!!\n"); } else if(npc2.HP<=0) { text5.setText(name2+"挂了!!!\n"); text6.setText(name2+"挂了!!!\n"); area.append(name2+"挂了!!!\n");
分享到:
收藏