首页 > 其他分享 >062 Finishing the Core Functionality

062 Finishing the Core Functionality

时间:2024-08-26 20:24:12浏览次数:6  
标签:box Core 062 monsterHealth color width playerHealth Finishing margin

示例

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vue Basics</title>
    <link
      href="https://fonts.googleapis.com/css2?family=Jost:wght@400;700&display=swap"
      rel="stylesheet"
    />
    <link rel="stylesheet" href="styles.css" />
    <script src="https://unpkg.com/vue@3/dist/vue.global.js" defer></script>
    <script src="app.js" defer></script>
  </head>
  <body>
    <header>
      <h1>Monster Slayer</h1>
    </header>
    <div id="game">
      <section id="monster" class="container">
        <h2>Monster Health</h2>
        <div class="healthbar">
          <div class="healthbar__value" :style="monsterHealthStyle"></div>
        </div>
      </section>
      <section id="player" class="container">
        <h2>Your Health</h2>
        <div class="healthbar">
          <div class="healthbar__value" :style="playerHealthStyle"></div>
        </div>
      </section>
      <section class="container" v-if="winner">
        <h2>Game Over!</h2>
        <h3 v-if="winner === 'monster'">You lost!</h3>
        <h3 v-else-if="winner === 'player'">You won!</h3>
        <h3 v-else>It's a draw!</h3>
        <button @click="startGame">START NEW GAME</button>
      </section>
      <section id="controls" v-if="!winner">
        <button @click="attackMonster">ATTACK</button>
        <button :disabled="mayUseSpecialAttack" @click="specialAttackMonster">SPECIAL ATTACK</button>
        <button @click="heal">HEAL</button>
        <button @click="surrender">SURRENDER</button>
      </section>
      <section id="log" class="container">
        <h2>Battle Log</h2>
        <ul></ul>
      </section>
    </div>
  </body>
</html>

app.js

function getRandomValue(min, max) {
    return Math.floor(Math.random() * (max - min)) + min;
}

const app = Vue.createApp({
    data() {
        return {
            playerHealth: 100,
            monsterHealth: 100,
            currentRound: 0,
            winner: null
        };
    },
    computed: {
        playerHealthStyle() {
            return { width: this.playerHealth + '%' };
        },
        monsterHealthStyle() {
            return { width: this.monsterHealth + '%' };
        },
        mayUseSpecialAttack() {
            return this.currentRound % 3 !== 0;
        }
    },

    methods: {
        attackMonster() {
            this.currentRound++;
            const attackValue = getRandomValue(5, 12);
            if (this.monsterHealth > 0) {
                this.monsterHealth -= attackValue;
                this.attackPlayer();
                if (this.monsterHealth < 0) {
                    this.monsterHealth = 0;
                }
            }
        },
        attackPlayer() {
            const attackValue = getRandomValue(8, 15);
            if (this.playerHealth > 0) {
                this.playerHealth -= attackValue;
                if (this.playerHealth < 0) {
                    this.playerHealth = 0;
                }
            }
        },
        specialAttackMonster() {
            this.currentRound++;
            const attackValue = getRandomValue(10, 25);
            if (this.monsterHealth > 0) {
                this.monsterHealth -= attackValue;
                this.attackPlayer();
                if (this.monsterHealth < 0) {
                    this.monsterHealth = 0;
                }
            }
        },
        heal() {
            const healValue = getRandomValue(8, 20);
            if (this.playerHealth + healValue > 100) {
                this.playerHealth = 100;
            } else {
                this.playerHealth += healValue;
            }
            this.attackPlayer();
        },
        startGame() {
            this.playerHealth = 100;
            this.monsterHealth = 100;
            this.currentRound = 0;
            this.winner = null;
        },
        surrender() {
            this.winner = 'monster';
        }
    },
    watch: {
        playerHealth(value) {
            if (value <= 0 && this.monsterHealth <= 0) {
                this.winner = 'draw';
            } else if (value <= 0) {
                this.winner = 'monster';
            }
        },
        monsterHealth(value) {
            if (value <= 0 && this.playerHealth <= 0) {
                this.winner = 'draw';
            } else if (value <= 0) {
                this.winner = 'player';
            }
        }
    },

});

app.mount('#game');

styles.css

* {
  box-sizing: border-box;
}

html {
  font-family: 'Jost', sans-serif;
}

body {
  margin: 0;
}

header {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.26);
  padding: 0.5rem;
  background-color: #880017;
  color: white;
  text-align: center;
  margin-bottom: 2rem;
}

section {
  width: 90%;
  max-width: 40rem;
  margin: auto;
}

.healthbar {
  width: 100%;
  height: 40px;
  border: 1px solid #575757;
  margin: 1rem 0;
  background: #fde5e5;
}

.healthbar__value {
  background-color: #00a876;
  width: 100%;
  height: 100%;
}

.container {
  text-align: center;
  padding: 0.5rem;
  margin: 1rem auto;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.26);
  border-radius: 12px;
}

#monster h2,
#player h2 {
  margin: 0.25rem;
}

#controls {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
}

button {
  font: inherit;
  border: 1px solid #88005b;
  background-color: #88005b;
  color: white;
  padding: 1rem 2rem;
  border-radius: 12px;
  margin: 1rem;
  width: 12rem;
  cursor: pointer;
  box-shadow: 1px 1px 4px rgba(0, 0, 0, 0.26);
}

button:focus {
  outline: none;
}

button:hover,
button:active {
  background-color: #af0a78;
  border-color: #af0a78;
  box-shadow: 1px 1px 8px rgba(0, 0, 0, 0.26);
}

button:disabled {
  background-color: #ccc;
  border-color: #ccc;
  box-shadow: none;
  color: #3f3f3f;
  cursor: not-allowed;
}

#log ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

#log li {
  margin: 0.5rem 0;
}

.log--player {
  color: #7700ff;
}

.log--monster {
  color: #da8d00;
}

.log--damage {
  color: red;
}

.log--heal {
  color: green;
}

标签:box,Core,062,monsterHealth,color,width,playerHealth,Finishing,margin
From: https://blog.csdn.net/KevinHuang2088/article/details/141572577

相关文章

  • 《欧洲卡车模拟2》游戏启动时闪退提示缺少Core_ets2mp.dll文件怎么解决?欧卡2游戏崩溃
    在玩《欧洲卡车模拟2》时,游戏启动时出现闪退,并提示缺少Core_ets2mp.dll文件,这着实令人困扰。玩家可以尝试重新安装游戏、更新驱动程序,或者从可靠来源获取该文件并正确放置,以解决这一问题。本篇将为大家带来《欧洲卡车模拟2》游戏启动时闪退提示缺少Core_ets2mp.dll文件怎么解决......
  • 使用try-convert将.NET Framework项目迁移到.NET Core
    try-convert 是一个命令行工具,它可以帮助开发者将.NETFramework项目迁移到.NETCore或.NET5/6/7(以及更高版本,取决于发布时的最新版本)。这是Microsoft官方提供的一个工具,旨在简化迁移过程,但请注意,它不会自动解决所有兼容性问题,因为它主要是帮助进行项目文件和配置文件......
  • C#/.NET/.NET Core技术前沿周刊 | 第 2 期(2024年8.19-8.25)
    前言C#/.NET/.NETCore技术前沿周刊,你的每周技术指南针!记录、追踪C#/.NET/.NETCore领域、生态的每周最新、最实用、最有价值的技术文章、社区动态、优质项目和学习资源等。让你时刻站在技术前沿,助力技术成长与视野拓宽。欢迎投稿,推荐或自荐优质文章/项目/学习资源等。每......
  • Swift 中的影像魔术:Core Video 的高级应用
    标题:Swift中的影像魔术:CoreVideo的高级应用在Swift开发中,CoreVideo是Apple提供的一个强大的框架,用于处理高质量的视频内容。从实时视频滤镜到高级图像处理,CoreVideo为开发者提供了丰富的API来实现各种视觉效果。本文将详细介绍如何在Swift中使用CoreVideo......
  • k8s中coredns访问连接拒绝问题解决
    问题现象1、节点访问coredns连接拒绝2、内部pod无法正常进行解析问题解决思路检查CoreDNSPod状态是否正常[root@k8s-master01~]#kubectlgetpods-nkube-system-lk8s-app=kube-dnsNAMEREADYSTATUSRESTARTSAGEcoredns-7b8d6fc5......
  • 062、Vue3+TypeScript基础,插槽中使用具名插槽
    01、main.js代码如下://引入createApp用于创建Vue实例import{createApp}from'vue'//引入App.vue根组件importAppfrom'./App.vue'//引入emitter用于全局事件总线//importemitterfrom'@/utils/emitter'constapp=createApp(App);//App.vue的根元素id为......
  • C# 扫描并读取图片中的文字(.NET Core)
    本文介绍如何通过C#程序来扫描并读取图片中的文字,这里以创建一个.NetCore程序为例。下面是具体步骤,供参考。程序测试环境:VisualStudio版本要求不低于2017图片扫描工具:Spire.OCRfor.NET图片格式:png(这里的图片格式支持JPG、PNG、GIF、BMP、TIFF等格式)扫描的图片文字:中文(......
  • Epicor ERP软件二次开发:EpicorERP二次开发之工作流集成与自动化
    EpicorERP软件二次开发:EpicorERP二次开发之工作流集成与自动化EpicorERP软件二次开发:工作流集成与自动化1.1EpicorERP软件概述EpicorERP是一款全面的企业资源规划软件,旨在帮助中大型企业优化其业务流程,提高运营效率。它集成了财务、供应链、制造、销售、人力资源等......
  • Citrix ADC Release 14.1 Build 29.63 (nCore, VPX, SDX, CPX, BLX) - 混合多云应用交
    CitrixADCRelease14.1Build29.63(nCore,VPX,SDX,CPX,BLX)-混合多云应用交付控制器CitrixADC-混合多云应用交付控制器请访问原文链接:https://sysin.org/blog/citrix-adc-14/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.org大规模应用程序交付可能很复......
  • C# .NET CORE 知识点总结【基础篇】
    心之所向,勇往直前!记录面试中的那些小事。面试题只是一道门,最好还是走进屋里看看。正文  结语本篇到此结束,如果有任何疑问或者指正,请发表在评论区。......