Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

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

              
                  <div id="current_win"></div>
  <img src="https://cryptocannabis.app/wp-content/uploads/2019/06/CCG-DApp-Sun.png"/>
  <div id="game_container">
    <div class="slot_column">
      <img class="slot"><img class="slot"><img class="slot"><img class="slot"><img class="slot"><img class="slot">
    </div>
    <div class="slot_column">
      <img class="slot"><img class="slot"><img class="slot"><img class="slot"><img class="slot"><img class="slot">
    </div>
    <div class="slot_column">
      <img class="slot"><img class="slot"><img class="slot"><img class="slot"><img class="slot"><img class="slot">
    </div>
    <div class="slot_column">
      <img class="slot"><img class="slot"><img class="slot"><img class="slot"><img class="slot"><img class="slot">
    </div>
    <div class="slot_column">
      <img class="slot"><img class="slot"><img class="slot"><img class="slot"><img class="slot"><img class="slot">
    </div>
  </div>
  <footer>
    <div class="table">
      <div class="cell">
        <button id="button_bet_deg" onclick="changeBet(this);">-</button>
        <button id="current_bet" disabled></button>
        <img src="https://cryptocannabis.app/wp-content/uploads/2019/06/TRC10-CCGX.png" />
        <button id="button_bet_inc" onclick="changeBet(this)">+</button>
      </div>
      <div class="cell">
        <button id="button_roll" onclick="roll()">Roll</button>
      </div>
      <div class="cell">
        <button id="current_money" disabled></button>
        <img src="https://cryptocannabis.app/wp-content/uploads/2019/06/TRC10-CCGX.png" />
        <button id="button_reset" onclick="resetGame()">Reset</button>
      </div>
    </div>
  </footer>
<footer id="info">
  Programming and game design by Mikko-Pentti Einari Eronen<br />
  Graphics by: www.pixabay.com and KinToshi<br />
  Music and FX: www.freesound.org and www.bendsound.com
</footer>
</div>
              
            
!

CSS

              
                html {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}

* {
  font-family: 'Caveat Brush', cursive;
}

body {
  margin: 0px;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  display: table;
  text-align: center;
  font-family: 'Anton', sans-serif;
  color: white;
  background: radial-gradient(rgb(255, 200, 0), rgb(200, 255, 0));
  background-size: 100% 100%;
}

#logo {
  font-size: 6em;
  color: rgb(200, 255, 0);
  text-shadow: -3px 0 rgb(255, 0, 0),
                0 3px rgb(255, 0, 0),
                3px 0 rgb(255, 0, 0),
                0 -3px rgb(255, 0, 0);
}

#game_container {
  width: 640px;
  height: 384px;
  margin: auto;
  overflow: hidden;

  border-width: 6px;
  border-style: solid;
  border-color: white;
  border-image-source: linear-gradient(0deg,
                                       rgb(32, 128, 23) 0%,
                                       rgb(32, 255, 23) 100%);
  border-image-slice: 1;
  border-bottom: 0px;
  z-index: 0;
  background-color: rgb(64, 32, 0);
  background-image: url("https://www.lieksa.info/demot/codepen/slotgame/column_shadow.png");
}

/* SLOTS */
.slot_column {
  width: 128px;
  position: relative;
  box-sizing: border-box;
  float: left;
  border: 1px solid rgb(64, 64, 64);
  overflow: auto;
  z-index: 0;
}

.slot {
  display: block;
  width: 128px;
  height: 128px;
  float: left;
  position: relative;
  box-sizing: border-box;
  border-left: 3px solid rgba(0, 0, 0, 0.5);
  border-right: 3px solid rgba(0, 0, 0, 0.5);
  z-index: 0;
  transition: all 0.2s linear;
}

.slot_animate {
  animation: roll 0.20s infinite linear;
}

@keyframes roll {
  0% {
    top: 0px;
  }
  100% {
    top: -384px;
  }
}

.slot_win {
  border: 3px solid rgba(255, 255, 255, 1.0);
  animation: slot_win 1.0s infinite ease;
}

@keyframes slot_win {
  0% {
    transform: scale(1.0, 1.0);
  }
  50% {
    transform: scale(0.9, 0.9);
  }
  100% {
    transform: scale(1.0, 1.0);
  }
}









/* BOTTOM BUTTONS */
footer {
  display: table;
  margin: auto;
  width: 640px;
  height: 32px;
  border-width: 6px;
  border-style: solid;
  border-color: white;
  border-image-source: linear-gradient(0deg,
                                       rgb(32, 255, 23) 0%,
                                       rgb(32, 128, 23) 100%);
  border-image-slice: 1;
  border-top: 0px;
  
  background: linear-gradient(0deg, rgb(24, 8, 0) 0%, rgb(32, 16, 0) 100%);
}

#info {
  border: 0px;
  background: none;
  color: black;
  margin-top: 8px;
}

.container {
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}

.table {
  display: table;
  table-layout: fixed;
  width: 100%;
  height: 100%;
}

.cell {
  display: table-cell;
  vertical-align: middle;
  text-align: center;
}

#current_win {
  display: none;
  position: absolute;
  margin-top: 100px;
  width: 640px;
  height: 384px;
  margin-left: -320px;
  left: 50%;
  overflow: hidden;
  z-index: 1000;
  font-size: 10em;
  color: rgb(255, 255, 0);
  text-shadow: 0px 0px 12px rgb(16, 16, 16);
  animation: slot_win 1.0s infinite ease;
}

button {
  width: 32px;
  padding: 0px;
  border: 0px;
  background-color: rgb(200, 255, 0);
  height: 32px;
  border-radius: 100%;
}

button:hover {
  background-color: rgb(255, 255, 0);
}

container p {
  height: 48px;
  display: block;
  float: left;
}

#button_roll {
  width: 80px;
  height: 80px;
}

.button_bet {
  display: block;
  float: left;
}

footer button, footer img {
  float: left;
  height: 32px;
  margin: 4px;
}

button:disabled {
  background: none;
  color: rgb(100, 100, 100);
}

#button_roll {
  float: none;
  margin: auto;
}

#button_reset {
  float:right;
  margin-right: 16px;
}

#current_bet, #current_money {
  font-size: 2em;
  color: white;
}
              
            
!

JS

              
                var cols = document.getElementsByClassName("slot_column");

var slot_images = ["slot1", "slot2", "slot3", "slot4", "slot5"];
var slot_image_url = "https://www.lieksa.info/demot/codepen/slotgame/";
var current_column = 0;
var current_winning = 0;
var current_money = 20.0;
var current_bet = 1.0;

var audio_music = new Audio('https://www.lieksa.info/demot/codepen/slotgame/bensound-happiness.mp3');
var audio_fx_roll = new Audio('https://www.lieksa.info/demot/codepen/slotgame/coingame_rolling.mp3');

var audio_fx_slot1 = new Audio('https://www.lieksa.info/demot/codepen/slotgame/slot1.mp3');

var audio_fx_slot2 = new Audio('https://www.lieksa.info/demot/codepen/slotgame/slot2.mp3');

var audio_fx_slot3 = new Audio('https://www.lieksa.info/demot/codepen/slotgame/slot3.mp3');

var audio_fx_slot4 = new Audio('https://www.lieksa.info/demot/codepen/slotgame/slot4.mp3');

var audio_fx_slot5 = new Audio('https://www.lieksa.info/demot/codepen/slotgame/slot5.mp3');

audio_music.volume = 0.1;
audio_fx_roll.volume = 1.0;

function audio_loop() {
  this.currentTime = 0;
  this.play();
}

function audioPlay(audio) {
  audio.addEventListener('ended', audio_loop);
  audio.play();
}

function audioPlayOnce(audio) {
  audio.play();
}

function audioStop(audio)  {
  audio.pause();
  audio.currentTime = 0;
  audio.removeEventListener('ended', audio_loop);
}

// LANES
var laneA = [
  cols[0].children[0],
  cols[1].children[0],
  cols[2].children[0],
  cols[3].children[0],
  cols[4].children[0],
];

var laneB = [
  cols[0].children[1],
  cols[1].children[1],
  cols[2].children[1],
  cols[3].children[1],
  cols[4].children[1],
];

var laneC = [
  cols[0].children[2],
  cols[1].children[2],
  cols[2].children[2],
  cols[3].children[2],
  cols[4].children[2],
];

var laneD = [
  cols[0].children[0],
  cols[1].children[1],
  cols[2].children[2],
  cols[3].children[1],
  cols[4].children[0],
];

var laneE = [
  cols[0].children[2],
  cols[1].children[1],
  cols[2].children[0],
  cols[3].children[1],
  cols[4].children[2],
];

var laneF = [
  cols[0].children[0],
  cols[1].children[1],
  cols[2].children[1],
  cols[3].children[1],
  cols[4].children[0],
];

var laneG = [
  cols[0].children[1],
  cols[1].children[0],
  cols[2].children[0],
  cols[3].children[0],
  cols[4].children[1],
];

var laneH = [
  cols[0].children[1],
  cols[1].children[2],
  cols[2].children[2],
  cols[3].children[2],
  cols[4].children[1],
];

var laneI = [
  cols[0].children[2],
  cols[1].children[1],
  cols[2].children[1],
  cols[3].children[1],
  cols[4].children[2],
];

var laneJ = [
  cols[0].children[0],
  cols[1].children[1],
  cols[2].children[0],
  cols[3].children[1],
  cols[4].children[0],
];

var laneK = [
  cols[0].children[1],
  cols[1].children[0],
  cols[2].children[1],
  cols[3].children[0],
  cols[4].children[1],
];

var laneL = [
  cols[0].children[1],
  cols[1].children[2],
  cols[2].children[1],
  cols[3].children[2],
  cols[4].children[1],
];

var laneM = [
  cols[0].children[2],
  cols[1].children[1],
  cols[2].children[2],
  cols[3].children[1],
  cols[4].children[2],
];

var laneN = [
  cols[0].children[0],
  cols[1].children[0],
  cols[2].children[1],
  cols[3].children[0],
  cols[4].children[0],
];

var laneO = [
  cols[0].children[1],
  cols[1].children[1],
  cols[2].children[0],
  cols[3].children[1],
  cols[4].children[1],
];

var laneP = [
  cols[0].children[2],
  cols[1].children[2],
  cols[2].children[1],
  cols[3].children[2],
  cols[4].children[2],
];

var laneQ = [
  cols[0].children[1],
  cols[1].children[1],
  cols[2].children[2],
  cols[3].children[1],
  cols[4].children[1],
];

var lanes = [laneA, laneB, laneC, laneD, laneE, laneF, laneG, laneH, laneI, laneJ, laneK, laneL, laneM, laneN, laneO, laneP, laneQ];

// UPDATE UI
function updateUI() {
  var bet_deg = document.getElementById("button_bet_deg");
  var bet_inc = document.getElementById("button_bet_inc");
  var bet = document.getElementById("current_bet");
  var money = document.getElementById("current_money");
  
  bet.innerHTML = current_bet;
  money.innerHTML = current_money;
}

function addToWinnings(slot, count) {
  count = count-2;
  var value = 0;
  switch (slot) {
    case "https://www.lieksa.info/demot/codepen/slotgame/slot1.png":
      value = current_bet*count;
      audioPlayOnce(audio_fx_slot1);
      break;
    case "https://www.lieksa.info/demot/codepen/slotgame/slot2.png":
      value = current_bet*count;
      audioPlayOnce(audio_fx_slot2);
      break;
    case "https://www.lieksa.info/demot/codepen/slotgame/slot3.png":
      value = current_bet*2*count;
      audioPlayOnce(audio_fx_slot3);
      break;
    case "https://www.lieksa.info/demot/codepen/slotgame/slot4.png":
      value = current_bet*2*count;
      audioPlayOnce(audio_fx_slot4);
      break;
    case "https://www.lieksa.info/demot/codepen/slotgame/slot5.png":
      value = current_bet*3*count;
      audioPlayOnce(audio_fx_slot5);
      break;      
  }
  
  current_winning += value;
}

function check_winnings() {
  current_winnings = 0;

  lanes.forEach( function(lane) {
    var matches = [lane[0]];
    for (var i=1; i < lane.length; i++) {
      if (lane[i].src === lane[0].src && matches.length === i) {
        matches.push(lane[i]);
      }
    }
    
    if (matches.length > 2) {
      addToWinnings(matches[0].src, matches.length);
    }
  });
}

function applyWinnings() {

  if (current_winning > 0) {
    var win = document.getElementById("current_win");
    win.style.display = "table-cell";
    win.innerHTML = "<p>"+current_winning+'<img src="https://cryptocannabis.app/wp-content/uploads/2019/06/CCG-DApp-Sun.png" /></p>';
  }
  
  current_money += current_winning;
  updateUI();
}

function initSlots(elem) {
  var cols = document.getElementsByClassName("slot_column");
  for (var x=0; x < cols.length; x++) {
    for (var y=0; y < 6; y++) {
      cols[x].children[y].src = slot_image_url+slot_images[Math.floor(Math.random() * slot_images.length)]+".png";
    }
  }
}

function shuffleSlots() {
  var cols = document.getElementsByClassName("slot_column");
  for (var x=current_column; x < cols.length; x++) {
    for (var y=0; y < 6; y++) {
      var random_image = slot_images[Math.floor(Math.random() * slot_images.length)];
      cols[x].children[y].src = slot_image_url + random_image + ".png";
    }
  }
}

function changeBet(elem) {  
  if (elem.innerHTML == "-") {
    if (current_bet > 0.25) {
      current_bet = current_bet/2;
    }
  } else {
    if (current_bet < 8.0) {
      current_bet = current_bet * 2;
    }
  }
  
  updateUI();
}

function takeBet() {
  if ( (current_money-current_bet) < 0) {
    return false;
  } else {
    current_money -= current_bet;
  }

  updateUI();
  return true;
}

function roll() {
  current_winning = 0;
  
  // Failed to take money (ran out).
  if ( !takeBet() ) {
    return false;
  }
  
  // Clear slot FX
  var cols = document.getElementsByClassName("slot_column");
  for (var x=current_column; x < cols.length; x++) {
    for (var y=0; y < 6; y++) {
      cols[x].children[y].classList.remove("slot_win");
    }
  }
  
  // Clear winning text
  var win = document.getElementById("current_win");
  win.style.display = "none";
  win.innerHTML = "";

  // Disable buttons
  ButtonsDisabled(true);

  // Animate columns
  var columns = document.getElementsByClassName("slot_column");
  for (var i=0; i < columns.length; i++) {
    columns[i].classList.add("slot_animate");
  }
  
  // Roll phase
  window.setTimeout(roll_check, 2000);
  
  // Set audio
  audioPlay(audio_fx_roll);
}

function roll_check() {
  var columns = document.getElementsByClassName("slot_column");
  columns[current_column].classList.remove("slot_animate");
  
  shuffleSlots();
  
  if (current_column < 4) {
    window.setTimeout(roll_check, laneCheck() );
    current_column++;
  } else {
    // END OF ROLL
    
    // END AUDIO
    audioStop(audio_fx_roll);
    
    // Check winnings
    check_winnings();
    applyWinnings();
    
    // Reset values
    current_column = 0;
    ButtonsDisabled(false);
  }
}

// Check slots for possible winning lines.
function laneCheck() {
  var cols = document.getElementsByClassName("slot_column");

  var t = 2;

  lanes.forEach( function(lane) {
    var matches = [lane[0]];
    for (var i=1; i <= current_column; i++) {
      if (lane[i].src === lane[0].src && matches.length === i) {
        matches.push(lane[i]);
      }
    }

    if (matches.length > 2) {
      matches.forEach( function(current) {
        if ( !current.classList.contains("slot_win") ) {
          current.classList.add("slot_win");
        }
      });
      
      t = 3;
    } else {
      if (current_column >= 2 && t != 3) { t = 1; }
    }
  });
  
  return t*1000;
}

function ButtonsDisabled(status) {
  // Enable roll button again.
  document.getElementById("button_roll").disabled = status;
  document.getElementById("button_reset").disabled = status;
  document.getElementById("button_bet_deg").disabled = status;
  document.getElementById("button_bet_inc").disabled = status;
}

function resetGame() {
  current_winning = 0;
  current_money = 20.0;
  current_bet = 1.0;
  initSlots();
  updateUI();
}

// Tune background
function setBackground() {
  var body = document.body;
  var w = window.innerWidth;
  var h = window.innerHeight;
  
  if (w > h) {
    body.style.backgroundSize = "100% auto";
  } else {
    body.style.backgroundSize = "auto 100%";
  }
}

window.onload = function() {
  initSlots();
  resetGame();
  audioPlay(audio_music);
  setBackground();
}

window.resize = function() {
  setBackground();
}
              
            
!
999px

Console