java魔塔小游戏源代码 java魔塔类游戏( 二 )


System.exit(0);
}
}
}
return p.fight;
}
}
class MK extends Person {
public MK() {
val = 700;
coldTime = 2.5;
fight = 50;
chanceDefense = 6;
chanceHit = 3;
waitTime = 0;
}
boolean hitFlag = false;
boolean defenseFlag = false;
Random rand = new Random();
public void hit(Person p) {
if (rand.nextInt(10)chanceHit) {
p.waitTime = 3;//使对方晕眩3s
hitFlag = true;
}
int hurt = p.defense(this);
p.val = p.val - hurt;
if (p.val = 0) {
System.out.println(this.getClass().getSimpleName() + "胜出!");
System.exit(0);
}
System.out.println(this.getClass().getSimpleName() + "攻击"
+ p.getClass().getSimpleName() + ","
+ this.getClass().getSimpleName()
+ (this.hitFlag ? "发动攻击技能" : "未发动攻击技能")
+ p.getClass().getSimpleName()
+ (this.defenseFlag ? "发动防御技能" : "未发动防御技能")
+ this.getClass().getSimpleName() + ":" + this.val + ","
+ p.getClass().getSimpleName() + ":" + p.val);
hitFlag = false;
defenseFlag = false;
}
public int defense(Person p) {
if (rand.nextInt(10)chanceDefense) {
defenseFlag = true;
return p.fight / 2;//防御技能发动,伤害减半
}
return p.fight;
}
}
package com.lxi;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Random;
//三个人物的基类
abstract class Person {
int val;//生命值
double coldTime;//冷却时间
int waitTime;//晕眩时间
int fight;//攻击力
int chanceHit;//发起主动技能的概率
int chanceDefense;//发起防御技能的概率
abstract void hit(Person p);//攻击技能
abstract int defense(Person p); //防御技能,返回被伤害点数
}
class DH extends Person {
public DH() {
val = 600;
coldTime = 1.0;
fight = 30;
chanceHit = 3;//表示30%的概率
chanceDefense = 3;
waitTime = 0;
}
Random rand = new Random();
boolean hitFlag = false;//主动技能发动的标识
boolean defenseFlag = false;//防御技能发动的标识
public void hit(Person p) {
if (rand.nextInt(10)chanceHit) {//发动主动技能
int hurt = p.defense(this);
p.val = p.val - hurt;
if (p.val = 0) {
System.out.println(this.getClass().getSimpleName() + "胜出!");
System.exit(0);
}
val = val + hurt;
if (val600)
val = 600;
hitFlag = true;//标记主动技能已经发动
} else {//进行普通攻击
int hurt = p.defense(this);
p.val = p.val - hurt;
if (p.val = 0) {
System.out.println(this.getClass().getSimpleName() + "胜出!");
System.exit(0);
}
}
System.out.println(this.getClass().getSimpleName() + "攻击"
+ p.getClass().getSimpleName() + ","
+ this.getClass().getSimpleName()
+ (this.hitFlag ? "发动攻击技能" : "未发动攻击技能")
+ p.getClass().getSimpleName()
+ (this.defenseFlag ? "发动防御技能" : "未发动防御技能")
+ this.getClass().getSimpleName() + ":" + this.val + ","
+ p.getClass().getSimpleName() + ":" + p.val);
hitFlag = false;//
defenseFlag = false; //重置标记,下次重用
}
public int defense(Person p) {
if (rand.nextInt(10)chanceDefense) {
defenseFlag = true;//标记防御技能已经发动
return 0;
} else {
return p.fight;
}
}
}
JAVA小游戏程序代码这个是比较有名的那个烟花,不知道你有没有用:
建个工程,以Fireworks为类即可
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
public class Fireworks extends Applet implements MouseListener,Runnable

推荐阅读