随机之魂
我做了一个最简单的游戏,很远古的那种,一开始只加了攻击和命中率。
public void actionPerformed(java.awt.event.ActionEvent e) {`
Label.setIcon(new ImageIcon("D:\\新建文件夹\\3.gif"));
Random rm =new Random();
mingzhong=rm.nextInt(100)+1;
if(mingzhong>50){
hexueliang-=yougongji;
text1.setText("你攻击了对方。\n对方血量-"+yougongji);
texthe.setText("敌人的血量:"+hexueliang+"\n敌人的攻击:"+hegongji);
if(hexueliang<=0){
JOptionPane.showMessageDialog(null, "你怼死了敌人", "获胜!", JOptionPane.ERROR_MESSAGE);
hexueliang=rm.nextInt(1000)+1;
hegongji=rm.nextInt(100)+1;
texthe.setText("敌人的血量:"+hexueliang+"\n敌人的攻击:"+hegongji);
text1.setText("");
direnshuliang+=1;
he.setText("敌人"+direnshuliang);
return;
}
}
else
text1.setText("你没有命中。");
mingzhong=rm.nextInt(100)+1;
if(mingzhong>50){
youxueliang-=hegongji;
text1.append("\n对方攻击了你。\n你的血量-"+hegongji);
textyou.setText("你的血量"+youxueliang+"\n你的攻击"+yougongji);
if(youxueliang<=0){
int res=JOptionPane.showConfirmDialog(null, "你已经死亡", "是否继续", JOptionPane.YES_NO_OPTION);
if(res==JOptionPane.YES_OPTION){
youxueliang=rm.nextInt(1000)+1;
yougongji=rm.nextInt(100)+1;
textyou.setText("你的血量"+youxueliang+"\n你的攻击"+yougongji); //点击“是”后执行这个代码块
hexueliang=rm.nextInt(1000)+1;
hegongji=rm.nextInt(100)+1;
texthe.setText("敌人的血量:"+hexueliang+"\n敌人的攻击:"+hegongji);
text1.setText(""); //点击“是”后执行这个代码块
}else{
System.exit(0);
}
}
else
text1.append("\n对方没有命中。");
}
然后自己和敌人的血量和攻击力做了个随机:
然后我们加上了可以攻击不同部位的按钮,打不同地方命中率是不一样的
(自杀就不用多说了)
一开始的时候敌人打死一个就完了,之后我们加上了敌人的数量显示,可以一直打,每打死一个数量加1。
然后我们加上了死亡和获胜时的弹出窗口。
因为不能回血,所以不能打很久,于是我们做了打过3个人就能重新刷新血量或攻击力的设定。
if(direnshuliang%3==0){
Object[] options = {“随机血量”,”随机攻击”,”取消随机”};
int response=JOptionPane.showOptionDialog(null, "你升级了","你怼死了3个敌人",JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if(response==0) {
youxueliang=rm.nextInt(1000)+1;
textyou.setText("你的血量"+youxueliang+"\n你的攻击"+yougongji);
} else if(response==1) { yougongji=rm.nextInt(100)+1;
} else if(response==2) {
JOptionPane.showMessageDialog(null,"取消随机","消息",JOptionPane.INFORMATION_MESSAGE);
}