123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- // 玩家
- class Player {
- name: string;
- hp: number;
- power: number;
- defense: number;
- chargeCount: number;
- reboundUsed: boolean;
- constructor(name: string) {
- this.name = name;
- this.hp = 5;
- this.power = 1;
- this.defense = 0;
- this.chargeCount = 0;
- this.reboundUsed = false;
- }
- attack(target: Player) {
- target.receiveDamage(this.power);
- console.log(`${this.name}发动了攻击!`);
- }
- charge() {
- this.chargeCount++;
- console.log(`${this.name}开始蓄力!`);
- }
- defenseMove() {
- this.defense++;
- console.log(`${this.name}采取了防御姿态!`);
- }
- ultimateStrike(target: Player) {
- if (this.chargeCount === 2) {
- target.receiveDamage(999); // 直接秒杀
- this.chargeCount = 0;
- console.log(`${this.name}使用了终极大招!${target.name}被击败了!`);
- } else if (this.chargeCount === 1) {
- target.receiveDamage(this.power * 2); // 大招伤害为攻击的双倍
- this.chargeCount = 0;
- console.log(`${this.name}释放了大招!${target.name}受到了${this.power * 2}点伤害!`);
- } else {
- console.log(`${this.name}尚未蓄力足够,无法释放大招!`);
- }
- }
- reboundMove() {
- if (this.reboundUsed) {
- console.log(`${this.name}本局已经使用过反弹了!`);
- } else {
- this.reboundUsed = true;
- console.log(`${this.name}释放了反弹技能!`);
- }
- }
- receiveDamage(damage: number) {
- const actualDamage = Math.max(damage - this.defense, 0); // 考虑防御
- this.hp -= actualDamage;
- console.log(`${this.name}受到了${actualDamage}点伤害!`);
- if (this.hp <= 0) {
- console.log(`${this.name}被击败了!`);
- }
- }
- }
- // 游戏主逻辑
- function playGame() {
- const player1 = new Player('玩家1');
- const player2 = new Player('玩家2');
- while (player1.hp > 0 && player2.hp > 0) {
- // 玩家1的回合
- console.log('\n===== 玩家1的回合 =====');
- player1.attack(player2);
- // 玩家2的回合
- console.log('\n===== 玩家2的回合 =====');
- player2.charge();
- // 玩家1的回合
- console.log('\n===== 玩家1的回合 =====');
- player1.defenseMove();
- // 玩家2的回合
- console.log('\n===== 玩家2的回合 =====');
- player2.ultimateStrike(player1);
- // 重置防御
- player1.defense = 0;
- player2.defense = 0;
- // 判断玩家1是否被击败
- if (player1.hp <= 0) {
- break;
- }
- // 玩家1的回合
- console.log('\n===== 玩家1的回合 =====');
- player1.charge();
- // 玩家2的回合
- console.log('\n===== 玩家2的回合 =====');
- player2.attack(player1);
- // 玩家1的回合
- console.log('\n===== 玩家1的回合 =====');
- player1.reboundMove();
- // 判断玩家2是否被击败
- if (player2.hp <= 0) {
- break;
- }
- }
- }
- // 运行游戏
- playGame();
|