css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <link type="text/html" rel="stylesheet" href="https://raw.githubusercontent.com/abstracted/lu-vue-battle/master/css/foundation.min.css">
<div id="app">
<!--     <div class="logo"><span class="logo-mod">B</span>attle <span class="logo-mod">R</span>oyale<span
            class="logo-mod">!</span></div> -->
    <section class="row">
        <div class="small-6 columns">
            <h1 class="text-center">{{ players[0].name }}</h1>
            <div class="statusbar">
                <div class="statusbar text-center" style="background-color: green; margin: 0; color: white;"
                     :style=" {width: healthPercent(players[0]) + '% !important'}"><p class="statustext">hp:
                    {{ players[0].healthPoints }}</p>
                </div>
            </div>
            <div class="statusbar">
                <div class="statusbar text-center" style="background-color: coral; margin: 0; color: white;"
                     :style=" {width: statPercent(players[0], 'special') + '% !important'}"><p
                        class="statustext">sp:
                    {{ players[0].special.counter }}</p>
                </div>
            </div>
            <div class="statusbar">
                <div class="statusbar text-center" style="background-color: rebeccapurple; margin: 0; color: white;"
                     :style=" {width: statPercent(players[0], 'magic') + '% !important'}"><p
                        class="statustext">mp:
                    {{ players[0].magic.counter }}</p>
                </div>
            </div>
        </div>
        <div class="small-6 columns">
            <h1 class="text-center">{{ players[1].name }}</h1>
            <div class="statusbar">
                <div class="statusbar text-center" style="background-color: green; margin: 0; color: white;"
                     :style=" {width: healthPercent(players[1]) + '% !important'}"><p class="statustext">hp:
                    {{ players[1].healthPoints }}</p>
                </div>
            </div>
            <div class="statusbar">
                <div class="statusbar text-center" style="background-color: coral; margin: 0; color: white;"
                     :style=" {width: statPercent(players[1], 'special') + '% !important'}"><p
                        class="statustext">sp:
                    {{ players[1].special.counter }}</p>
                </div>
            </div>
            <div class="statusbar">
                <div class="statusbar text-center" style="background-color: rebeccapurple; margin: 0; color: white;"
                     :style=" {width: statPercent(players[1], 'magic') + '% !important'}"><p
                        class="statustext">mp:
                    {{ players[1].magic.counter }}</p>
                </div>
            </div>
        </div>
    </section>
    <section class="row controls">
        <div class="small-12 columns">
            <button id="start-game" :disabled="gameRunning" @click="startGame">START NEW GAME</button>
        </div>
    </section>
    <section class="row controls" v-if="gameRunning">
        <div class="small-12 columns">
            <button id="attack"
                    :disabled="activePlayer !== 0"
                    @click="attack(false)">ATTACK
            </button>
            <button id="special-attack"
                    :class="{flashing: players[0].special.canSpecial}"
                    :disabled="activePlayer !== 0 || !players[0].special.canSpecial"
                    @click="attack(true)">SPECIAL ATTACK
            </button>
            <button id="heal"
                    :class="{flashing: players[0].magic.canMagic}"
                    :disabled="activePlayer !== 0 || !players[0].magic.canMagic"
                    @click="heal()">HEAL
            </button>
            <button :disabled="!gameRunning" id="give-up" @click="gameOver()">GIVE UP</button>
        </div>
    </section>
    <section class="row log" v-if="log.length > 0">
        <div class="small-12 columns">
            <ul>
                <li v-for="(entry, i) of log"
                    :class="{'player-turn': entry[0] === 0, 'monster-turn': entry[0] === 1}">
                    {{ entry[1] }}
                </li>
            </ul>
        </div>
    </section>
</div>
            
          
!
            
              .logo {
    font-family: 'Lobster', cursive;
    font-size: 300%;
    position: fixed;
    transform: rotate(-16deg);
    margin-top: 30px;
    color: #a291b3;
}

.logo-mod {
    font-size: 150%;
    transform: rotate(15deg);
    display: inline-block;
    margin-right: -10px;
}

.text-center {
    text-align: center;
}

.statusbar {
    width: 80%;
    height: 40px;
    background-color: #eee;
    margin: auto;
    transition: width 550ms cubic-bezier(0.175, 0.885, 0.32, 1.275);
    display: flex;
    align-items: center;
    margin-bottom: 8px;
    border: 1px solid #66339940;
    position: relative;}

.statustext {
    margin-left: 15px;
    font-weight: bolder;
    text-transform: uppercase;
    color: #101010;
    mix-blend-mode: difference;
    filter: brightness(500%);
    white-space: nowrap;
}

.controls, .log {
    margin-top: 30px;
    text-align: center;
    padding: 10px;
    border: 1px solid #ccc;
    box-shadow: 0px 3px 6px #ccc;
}

.turn {
    margin-top: 20px;
    margin-bottom: 20px;
    font-weight: bold;
    font-size: 22px;
}

.flashing {
    animation: flashing 700ms infinite ease-in-out;
}

@keyframes flashing {
    0% {
        filter: hue-rotate(0deg) opacity(100%) saturate(70%);
        color: initial;
    }
    100% {
        filter: hue-rotate(-100deg) opacity(90%) saturate(150%);
        color: white;
    }

}

.log ul {
    list-style: none;
    font-weight: bold;
    text-transform: uppercase;
}

.log ul li {
    margin: 5px;
}

.log ul .player-turn {
    color: blue;
    background-color: #e4e8ff;
}

.log ul .monster-turn {
    color: red;
    background-color: #ffc0c1;
}

button {
    font-size: 20px;
    background-color: #eee;
    padding: 12px !important;
    box-shadow: 0 1px 1px black;
    margin: 10px;
}

#start-game {
    background-color: #aaffb0;
}

#start-game:hover {
    background-color: #76ff7e;
}

#attack {
    background-color: #ff7367;
}

#attack:hover {
    background-color: #ff3f43;
}

#special-attack {
    background-color: #ffaf4f;
}

#special-attack:hover {
    background-color: #ff9a2b;
}

#heal {
    background-color: #aaffb0;
}

#heal:hover {
    background-color: #76ff7e;
}

#give-up {
    background-color: #ffffff;
}

#give-up:hover {
    background-color: #c7c7c7;
}
            
          
!
            
              class Player {
	constructor(name, hp, attack, special, heal, sMin, mMin, npcBool) {
		this.name = name;
		this.npc = npcBool;
		this.maxHP = hp;
		this.healthPoints = hp;
		this.attackMult = attack;
		this.specialMult = special;
		this.magicMult = heal;
		this.special = {
			canSpecial: false,
			counter: 0,
			minimum: sMin
		};
		this.magic = {
			canMagic: false,
			counter: 0,
			minimum: mMin
		};
	}
}

const vue = new Vue({
	el: '#app',
	data: {
		gameRunning: false,
		activePlayer: 0,
		otherPlayer: 1,
		winner: 'Nobody',
		sizes: [ 'small', 'large', 'large' ],
		players: [
			new Player('You', 10000, 900, 1600, 9000, 400, 375, false),
			new Player('Monster', 15000, 1000, 2000, 7000, 600, 550, true)
		],
		log: []
	},
	methods: {
		randomSize: function () {
			return this.sizes[ Math.ceil(Math.random() * this.sizes.length) ];
		},
		randomNumber: function (size) {
			let num = null;
			switch ( size ) {
				case 'small':
					num = 10;
					break;
				case 'medium':
					num = 100;
					break;
				case 'large':
					num = 1000;
					break;
				case 'jumbo':
					num = 3750;
					break;
				default:
					num = size;
			}
			return Math.ceil(Math.random() * num);
		},
		randomAdvanced: function (multiplier, bonus) {
			let processAmount = true;
			let amount = null;
			let initial = null;
			do {
				initial = this.randomNumber(multiplier);
				amount = initial + bonus;
				if ( amount > (multiplier - bonus) && amount < (multiplier + (bonus * 1.5)) ) {
					processAmount = false;
				}
			} while ( processAmount );
			return amount;
		},
		healthPercent: function (player) {
			return Math.ceil((player.healthPoints * 100) / player.maxHP);
		},
		statPercent: function (player, stat) {
			switch ( stat ) {
				case 'special':
					stat = player.special;
					break;
				case 'magic':
					stat = player.magic;
					break;
				default:
					window.alert('An invalid property for a status bar was defined in html.');
					break;
			}
			if ( stat.counter > stat.minimum ) {
				return 100;
			} else {
				return Math.ceil((stat.counter * 100) / stat.minimum);
			}
		},
		attack: function (special) {
			let player = this.players[ this.activePlayer ];
			let other = this.players[ this.otherPlayer ];
			let multiplier = null;
			let bonus = this.randomNumber('large');
			let specialMessage = '';
			if ( special ) {
				specialMessage = ' brutally';
				multiplier = player.specialMult;
				player.special.counter -= player.special.minimum;
			} else {
				multiplier = player.attackMult;
			}
			let attackAmount = this.randomAdvanced(multiplier, bonus);
			if ( (other.healthPoints - attackAmount) < 0 ) {
				other.healthPoints = 0;
			} else {
				other.healthPoints -= attackAmount;
			}
			let message = player.name + ' attacked ' + other.name + specialMessage + ' with ' + attackAmount + ' damage.';
			this.log.unshift([ this.activePlayer, message ]);
			if ( this.checkHealth() ) {
				this.nextRound();
			}
		},
		heal: function () {
			let player = this.players[ this.activePlayer ];
			let bonus = this.randomNumber('large');
			let healAmount = this.randomAdvanced(player.magicMult, bonus);
			if ( (healAmount + player.healthPoints) > player.maxHP ) {
				player.healthPoints = player.maxHP;
			} else {
				player.healthPoints += healAmount;
			}
			player.magic.counter -= player.magic.minimum;
			let message = player.name + ' healed +' + healAmount + ' HP. Health was restored to ' + player.healthPoints + '.';
			this.log.unshift([ this.activePlayer, message ]);
			this.nextRound();
		},
		ai: function () {
			let player = this.players[ this.activePlayer ];
			if ( this.healthPercent(player) < 50 && player.magic.canMagic === true ) {
				this.heal();
			} else {
				this.attack(player.special.canSpecial);
			}
		},
		checkHealth: function () {
			if ( this.players[ this.otherPlayer ].healthPoints <= 0 ) {
				this.winner = this.players[ this.activePlayer ].name;
				this.gameOver();
				return false;
			}
			return true;
		},
		gameOver: function () {
			this.gameRunning = false;
			if ( window.confirm(this.winner + ' won the game!\nWould you like to play again?') ) {
				this.startGame();
			}
		},
		nextRound: function () {
			if ( this.log.length > 2 ) {
				setTimeout(() => {
					this.log.pop();
				}, 2500);
			}
			this.updateAbilities();
			let temp = this.activePlayer;
			this.activePlayer = this.otherPlayer;
			this.otherPlayer = temp;
			if ( this.players[ this.activePlayer ].npc === true ) {
        this.ai();
			}
		},
		updateAbilities: function () {
			let player = this.players[ this.activePlayer ];
			player.magic.counter += this.randomNumber('medium');
			player.special.counter += this.randomNumber('medium');
			player.magic.canMagic = player.magic.counter >= player.magic.minimum;
			player.special.canSpecial = player.special.counter >= player.special.minimum;
		},
		startGame: function () {
			this.winner = 'Nobody';
			this.log = [];
			this.activePlayer = 0;
			this.otherPlayer = 1;
			this.gameRunning = true;
			for ( let player of this.players ) {
				player.special.counter = 0;
				player.special.canSpecial = false;
				player.magic.counter = 0;
				player.magic.canMagic = false;
				player.healthPoints = player.maxHP;
			}
		}
	}
});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console