Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                

<!DOCTYPE html>


<head>
  <link rel="stylesheet" href="寶可夢對戰.css">
  </head>

<br>

<body>

  <script src="對戰成功檔1.js"></script>

   <div class= "wid" align= "center">




<br>

<div class="monster">
 <img id =" " class ="img" width="450" height="450"></a>

  <div class="blood"><span id="currentHP">?</span></div>
  <div class="monstermp" id="hempz">?</div>

</div>


<br>
<div class = "about" id ="hedata">
  <emhp>血量:?</emhp>
  <emmp>魔力:?</emmp>
  <ematk>攻擊:?</ematk>
  <emspd>速度:?</emspd>
  <emdef>防禦:?</emdef>

</div >



   </div>


<div class="data">
<br>
<div class="about" id ="usdata">
  <p id="ushp">血量:?</p>
  <p id="usmp">魔力:?</p>
  <p id="usatk">攻擊:?</P>
  <p id="usspd">速度:?</P>
  <p id="usdef">防禦:?</P>
</div>
      <div class="summary">
       
    
 <button class ="myhero"  onclick="start()"> 

  <img class = "img" src="" width="100" height="100"></button>

  <br><br><br>
 <button id="damageButton">直接攻擊</button>
 <button id="fireball">爆裂火球</button>
 <button id="damageButton">龍息爆氣</button>
 <button id="damageButton">龍之亂抓</button>
 <button id="damageButton">祭龍血咒</button>


      </div>

  <div class="HP"><span id="playerHP">?</span></div>

<div class="MP" id="usmpz">?</div>

  <div id="log" class="scrollen"></div>


</div>



</body>


</html>



              
            
!

CSS

              
                body {
    background-color:rgb(250, 251, 249);
    width: 100%;
    height:100%;
    display: flex;
   }

   .wid{
    width:  80%;
    height: 50%;
    background-color: rgb(255, 255, 254);
    border: 3px solid rgb(168, 160, 160);
    border-radius: 15px 15px 15px 15px;
margin-left: 1%;

   }

button{
    color:aliceblue;

  background-color:rgb(26, 57, 119);
  
  border:3px solid rgb(130, 128, 128);
  
  color:white;
  
  padding:10px 20px 10px 20px;
  
  border-radius:10px 10px 10px 10px;   

  margin: 3px;
 
}

button:active{

    color:aliceblue;

    background-color:rgb(108, 135, 189);
    
    border:3px solid rgb(130, 128, 128);
    
    color:white;
    
    padding:10px 20px 10px 20px;
    
    display: inline-flex;
    
    text-align:center;
  
    border-radius:10px 10px 10px 10px;


}

.summary{
    font-family: Arial, Helvetica, sans-serif;
    font-size: 15px;
    height: 150%;
    height: 150px;
    display: flexbox;
    margin-top: 30px;
}
    .HP{
        color: #f7f5f4; 
        border: 10px rgb(215, 205, 205) solid;
         border-radius: 30px;
         background-color: red;
         width: 20%;
         text-align: center;
         margin-left: 2%;
    }
    .MP{
        color: #fcfdfd;
        border: 10px rgb(215, 205, 205) solid;
        border-radius: 30px;
        background-color: rgb(59, 132, 227);
        width: 20%;
        margin-left: 2%;
    }


.monster{
    .blood{
        height: 20px;
        background-color: #FF0000;
        width: 70%;
        border-radius: 200px;
        border: rgb(192, 185, 176) 5px solid;
        color: white;
        
    }
}

.monstermp{
    height: 20px;
    background-color: #255e97;
    width: 70%;
    border-radius: 200px;
    border: rgb(192, 185, 176) 5px solid;
    color: white;
    

}

img
{
border: rebeccapurple 5px solid;
border-radius: 30px;
  
}

.uspic
{
    width: 90px;
    height:90px;
}

.myhero{
    float: left;
    margin-left: 3%;
}

.scrollen{
    overflow: scroll;
}

.data{
    width:  100%;
    height: 100%;
    height: 600px;
    background-color: rgb(255, 255, 254);
    border: 3px solid rgb(168, 160, 160);
    border-radius: 15px 15px 15px 15px;
    margin-left: 1%;
    margin-right: 3%;
  overflow: scroll;
  text-align: center;
}

flex{
    display: flex;
    flex-direction: column;
}

.about{
    background-color:gray;
    color: white;
}
              
            
!

JS

              
                

window.addEventListener('DOMContentLoaded', () => {




    console.log = (...e) => {

      for (var i = 0; i < e.length; i++) {
      log.innerHTML += (typeof(e[i]) == "object" ? JSON.stringify(e[i]) : e[i]) + "<br/>";
      
      }
      };



          // Check if both Pikachu and Charmander have HP greater than 0
          var hero = [
            pikachu = {
                hp: [190, 190], // HP(血量): 現在剩餘/總量
                mp: [200, 200], // MP(法力): 現在剩餘/總量
                def: 20, // 防禦力,決定被攻擊後會損多少血
                atk: 30,
                Moves: [ // 角色會的招式
                    {
                        MoveName: "直接攻擊",
                        Cost: 10 // 招式所消耗法力
                    },
                    {
                        MoveName: "爆裂火球",
                        Cost: 100
                    }
                ]
            },
            light = {
                hp: [300, 300],
                mp: [130, 130],
                def: 20,
                atk: 10,
                Moves: [
                    {
                        MoveName: "御劍術",
                        Cost: 10
                    }
                ]
            }
        ];
        
        
        

var mo_me =  0;

 var mo_he = 1;

 var playerHP = hero[mo_me].hp[1];

 var currentHP = hero[mo_he].hp[1];

 var theusmp =  hero[mo_me].mp[1];

 var thehemp = hero[mo_he].mp[1];



var ushper= document.getElementById("playerHP");

function ushpnow(){
    ushper.textContent = playerHP;
}

var currentHPElement = document.getElementById("currentHP");

function updateHPDisplay() {
currentHPElement.textContent = currentHP;

}


var usmper= document.getElementById("usmpz");

function usmpnow(){
    usmper.textContent = theusmp;
}

var hemper= document.getElementById("hempz");

function hempnow(){
    hemper.textContent = thehemp;
}

const damageButton = document.getElementById("damageButton");



var heelement = document.getElementById("hedata");

// 宣告多個非字串參數
var hehp = hero[mo_he].hp[0];
var hemp=  hero[mo_he].mp[0];
var hedef =  hero[mo_he].def;
var heatk =  hero[mo_he].atk;

// 使用拼接文本將多個值並排顯示在元素中
heelement.textContent = "血量:("+hehp + " )魔力:(" + hemp + ")防禦力:("+hedef +")攻擊力:(" + heatk+")";



var uselement = document.getElementById("usdata");

// 宣告多個非字串參數
var ushp = hero[mo_me].hp[0];
var usmp=  hero[mo_me].mp[0];
var usdef =  hero[mo_me].def;
var usatk =  hero[mo_me].atk;

// 使用拼接文本將多個值並排顯示在元素中
uselement.textContent = "血量:("+ushp + " )魔力:(" + usmp + ")防禦力:("+usdef +")攻擊力:(" + usatk+")";


document.getElementById("damageButton").addEventListener("click", function(){

takeDamage();
enemyAttack();
turn();


      function takeDamage() {

        if (currentHP > 0 && theusmp >= 100) {
            currentHP -= hero[mo_me].atk; 
            theusmp -= 10;
            console.log("<hr>敵方受到"+hero[mo_me].atk+"傷害,血量剩餘"+currentHP)
          
            if (theusmp < 10){
                damageButton.disabled=true;
                  }
            
            if (currentHP <= 0) {
                currentHP = 0;
                console.log("<hr>我方獲勝")
                damageButton.disabled = true; // 停用攻擊按鈕
            }
            updateHPDisplay();
            usmpnow();

        }
      }

      function enemyAttack() {
       if (playerHP > 0 && thehemp >= 10){
        playerHP -= hero[mo_he].atk;
        thehemp -= 10;
        console.log("我方受到"+hero[mo_he].atk+"傷害,血量剩餘"+playerHP)

        if (thehemp < 10){
            damageButton.disabled=true;
              }

        if (playerHP <= 0) {
            playerHP = 0;
         console.log("<hr>敵方獲勝")
         damageButton.disabled = true;
        }

        hempnow()
        ushpnow();
    }
}


function turn() {
    if (theusmp <= 0) {
        theusmp += 30;
    } else if (theusmp > hero[mo_me].mp[0]) {
        theusmp = hero[mo_me].mp[0];
    }

    if (thehemp <= 0) {
       thehemp += 30;
    } else if (thehemp > hero[mo_he].mp[0]) {
        thehemp = hero[mo_he].mp[0];
    }
    hempnow()
    usmpnow()
}


})});
              
            
!
999px

Console