




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java名字打架小游戏代码一、截图如下:1、初始界面:游戏中界面:游戏结束界面:程序代码如下:importjava.awt.*;importjava.awt.event.*;classGameextendsFrameimplementsActionListener{ Labell1,l2,label0,label1,label2,label3,label4,label5,label6,label7; TextFieldtext1,text2,text3,text4,text5,text6; Buttonbutton,button1,button2; Panelp1,p2,p3,p4; TextAreaarea; Menumenu; MenuBarmenubar; MenuItemitem1,item2; intHP,gongji,fangyv; intcount1=0,count2=0,count3=0,count4=0; Threadthread1; Game(Strings) { super(s); thread1=newThread(); label0=newLabel("初始血量在2000-3000之间随机产生!"); l1=newLabel("姓名1",Label.CENTER); l2=newLabel("姓名2",Label.CENTER); label1=newLabel("姓名1",Label.CENTER); label2=newLabel("姓名2",Label.CENTER); label3=newLabel("属性1",Label.CENTER); label4=newLabel("属性2",Label.CENTER); label5=newLabel("PK状态",Label.CENTER); label6=newLabel("被治疗次数:"+count1+"被攻击次数:"+count2); label7=newLabel("被治疗次数:"+count3+"被攻击次数:"+count4); p1=newPanel(); p2=newPanel(); p3=newPanel(); p4=newPanel(); area=newTextArea(10,30); text1=newTextField(5); text2=newTextField(5); text3=newTextField(34); text4=newTextField(34); text5=newTextField(27); text6=newTextField(27); button=newButton("确定"); button1=newButton("重新输入"); button2=newButton("退出"); 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=newMenu("菜单"); item1=newMenuItem("打开"); item2=newMenuItem("退出"); p2.setBackground(Color.pink); p1.setBackground(Color.orange); menubar=newMenuBar(); menu.add(item1); menu.addSeparator(); menu.add(item2); menubar.add(menu); setMenuBar(menubar); item2.addActionListener(this); area.setBackground(Color.yellow); addWindowListener(newWindowAdapter() { publicvoidwindowClosing(WindowEvente) { System.exit(0); } }); setBounds(500,100,285,590); setResizable(false);//设置窗口不可调整大小 setVisible(true); validate(); } publicvoidactionPerformed(ActionEvente) { if(e.getSource()==item2||e.getSource()==button2) System.exit(0); elseif(e.getSource()==button) { Stringname1=text1.getText(); Stringname2=text2.getText(); NPCCnpc1=newNPCC(name1); NPCCnpc2=newNPCC(name2); npc1.shuxing(); npc2.shuxing(); l1.setText(name1); l2.setText(name2); inth1,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); inti=0; while(npc1.HP>0&&npc2.HP>0) { if(i%2==0) { Strings1=npc1.damage(npc2.gongji); if(npc1.HPP!=0) { count1++; } else { count2++; } label6.setText("被治疗次数:"+count1+"被攻击次数:"+count2); Strings=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(InterruptedExceptionee){} npc1.HP=h1; s1="血量已达最大值!!"; } if(npc1.HP<0) { npc1.HP=0; } text3.setText(name1+":HP:"+npc1.HP+"攻击:"+npc1.gongji+"防御:"+npc1.fangyv); text5.setText(name1+s1); area.append(name1+s1+'\n'); area.append(name1+"剩余生命值:"+npc1.HP+'\n'); } else { Strings2=npc2.damage(npc1.gongji); if(npc2.HPP!=0) { count3++; } else { count4++; } label7.setText("被治疗次数:"+count3+"被攻击次数:"+count4); Strings=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(InterruptedExceptionee){} npc2.HP=h2; s2="血量已达最大值!!"; } if(npc2.HP<0) { npc2.HP=0; } text4.setText(name2+":HP:"+npc2.HP+"攻击:"+npc2.gongji+"防御:"+npc2.fangyv); text6.setText(name2+s2); area.append(name2+s2+'\n'); area.append(name2+"剩余生命值:"+npc2.HP+'\n'); } try { thread1.sleep(1000); } catch(InterruptedExceptionee){} i++; } if(npc1.HP<=0) { text5.setText(name1+"挂了!!!\n"); text6.setText(name1+"挂了!!!\n"); area.append(name1+"挂了!!!\n"); } elseif(npc2.HP<=0) { text5.setText(name2+"挂了!!!\n"); text6.setText(name2+"挂了!!!\n"); area.append(name2+"挂了!!!\n"); } } elseif(e.getSource()==button1) { text1.setText(null); text2.setText(null); text3.setText(null); text4.setText(null); text5.setText(null); text6.setText(null); area.setText(null); label6.setText("被治疗次数:"+0+"被攻击次数:"+0); label7.setText("被治疗次数:"+0+"被攻击次数:"+0); l1.setText("姓名1"); l2.setText("姓名2"); } }}classNPCC{ Stringname,s; intHP,gongji,fangyv; intHPP=0; inthurt=0; NPCC(Stringname) { =name; } publicvoidshuxing() { HP=(int)(Math.random()*1000)+2000; gongji=(int)(Math.random()*150)+80; fangyv=(int)(Math.random()*30)+50; } publicStringdamage(intgongji) { inta=(int)(Math.random()*10); switch(a) { case1: s="受到普通攻击"; hurt=gongji; HPP=0; break;
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 培训采购评审模板
- 预防医学视频课件
- 项目管理课件PMP
- 音乐课件教学课件
- 2025年棉花生产项目建议书
- 2025年气体检测设备项目合作计划书
- 水肌酸产品项目申请报告(参考模板)
- 城市污水管网建设工程资金申请报告(范文)
- 2025年抗血吸虫病药合作协议书
- 无人驾驶技术在物流中的应用
- 民法学全套精美课件
- 叉车安全驾驶技术(叉车基础知识、安全驾驶、动力装置)课件
- 国内高品质胶原蛋白行业发展白皮书
- 《庄子》寓言对后世的影响
- 质量过程报告记录汇总表-scr与ncr表格报检单
- 湖南省长沙市2022-2023学年新高一英语入学分班考试试卷【含答案】
- k-bus产品手册中文版ip interface使用手册
- 第九讲有机化学结构理论
- 工程化学复习要点及习题解答童志平版本PPT课件
- 论中心蝶阀、单、双、三、四偏心蝶阀
- 《中国语言文化》课程教学大纲
评论
0/150
提交评论