




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年淮南职业技术学院单招职业技能测试题库有答案解析
- 2025年黑龙江省五常市辅警招聘考试试题题库含答案详解(黄金题型)
- 2025年河北省定州市辅警招聘考试试题题库含答案详解(综合题)
- (福建高考卷自主命题6科)2021年福建省普通高中学业水平选择性考试高考物化生+政史地真题试卷及答案
- 政治●福建卷丨2024年福建省普通高中学业水平选择性考试政治试卷及答案
- 2024年消防条令纲要知识考试题库含答案(考试直接用)
- 人防工程施工要点
- Brand KPIs for online betting:Pin-Up in Mexiko-英文培训课件2025.5
- 2025届高考物理大一轮复习课件 第四章 第22课时 实验六:探究向心力大小与半径、角速度、质量的关系
- 西安市社区网格化治理现状与问题研究
- 建设土地买卖合同模板
- 儿科医生岗位招聘笔试题与参考答案(某世界500强集团)
- 《死亡诗社》电影赏析
- 种植二期手种植义齿II期手术护理配合流程
- 七年级信息技术教案下册(合集6篇)
- 2025年成人高考成考(高起专)英语试题与参考答案
- 延保服务合同模板
- 非遗文化走进数字展厅+大数据与互联网系创业计划书
- 【电商平台“二选一”行为的反垄断法律规制探究20000字(论文)】
- 第9课++友好相处++学会合作+第2课时 【中职专用】中职思想政治《心理健康与职业生涯》高效课堂 (高教版基础模块)
- 广东省广州市天河区2023-2024学年学年高一下学期期末考试数学试卷(解析版)
评论
0/150
提交评论