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

              
                <div class="resources">
  <div class="resource" id="days">
    <span class="label">Date</span>
    <span class="value">0</span>
  </div>
  <div class="resource" id="wood">
    <span class="label">Wood</span>
    <span class="value">0</span>
  </div>
  <div class="resource" id="food">
    <span class="label">Food</span>
    <span class="value">0</span>
  </div>
  <div class="resource" id="population">
    <span class="label">People</span>
    <span class="value">0</span>
  </div>
  <div class="resource hidden" id="hungry">
    <span class="label">Starving</span>
    <span class="value">0</span>
  </div>
</div>
<div class="actions">
  <button id="chop-wood">Chop Wood</button>
  <button id="forage">Forage</button>
  <button id="hunt">Hunt</button>
  <label id="achievements" class="btn" for="achievements-checkbox">Achievements</label>
</div>
<input type="checkbox" id="achievements-checkbox" class="hidden">
<div class="achievements">Achievements</div>

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

              
            
!

CSS

              
                body {
  margin: 0;
  font-family: sans;
}

.resources {
  display: flex;
  background: #666;
  padding: 5px 7px;
}

.resource {
  display: flex;
  flex-direction: column;
  text-align: center;
  background: #2d2929;
  color: white;
  margin: 5px 2px;
  padding: 7px;
  border-radius: 5px;
  transition: background-color 1s;
}
.resource.green {
  background: #4b9d4b;
  transition: background-color 100ms;
}
.resource.red {
  background: #c62929;
  transition: background-color 100ms;
}

.resource#hungry {
  background: #c62929;
}

#log {
  overflow: hidden;
}

#log p {
  margin: 0;
  line-height: 2;
  transition: margin-top 0.2s;
  height: 30px;
}
#log p.new {
  margin-top: -30px;
  transition: margin-top 0s;
}

#log p.red {
  color: red;
}

.hidden {
  display: none;
}

button, .btn {
	border-radius: 2px;
	font-size: 20px;
	padding: 0.6em 1em;
	text-transform: uppercase;
	border: 1px solid #666;
	background: #c5d680;
	cursor: pointer;
	line-height: 25px;
	color: #333;
}
button:hover {
  background: white;
}

.actions {
	padding: 8px;
	text-align: center;
	background: #9aad4d;
}

#achievements {
  background: #80bbd6;
}
.achievements {
  overflow: hidden;
  max-height: 200px;
  transition: max-height 500ms;
}
input#achievements-checkbox:not(:checked) + .achievements {
  max-height: 0;
}
              
            
!

JS

              
                const $ = q => document.querySelector(q)
const on = (elem, event, callback) => elem.addEventListener(event, callback)
const resources = {
  wood: 3,
  food: 35,
}
const population = {
  ready: 15,
  hungry: 0,
  sick: 0,
  hurt: 0,
}
const achievements = {
  carpentry: {
    cost: {
      wood: 10,
      food: 10,
      people: 4
    }
  },
  shipyard: {
    requires: [
      'carpentry'
    ]
  }
}

const date = new Date('1499/05/13')
const DAY = 10000

on($('#chop-wood'), 'click', () => fetchWood())
on($('#forage'), 'click', () => forage())
on($('#hunt'), 'click', () => hunt())

const log = (text, color) => {
  if ($('#log .new')) {
    setTimeout(() => log(text, color), 500)
    return
  }
  const newLog = document.createElement('p')
  newLog.innerText = text
  if (color) newLog.classList.add(color)
  newLog.classList.add('new')
  $('#log').prepend(newLog)
  setTimeout(() => {
    newLog.classList.remove('new')
  }, 200)
}

const fetchWood = () => {
  population.ready -= 2
  setTimeout(bring('wood', 2, 5, 0.05), DAY * 0.5)
  log('πŸŒ³πŸ‘¬ 2 people set off to bring wood.')
  updateView()
}

const forage = () => {
  population.ready -= 2
  setTimeout(bring('food', 2, 4, 0), DAY * 0.3)
  log('πŸŒΎπŸ‘¬ 2 people have gone foraging.')
  updateView()
}

const hunt = () => {
  population.ready -= 4
  setTimeout(bring('food', 4, 12, 0.1), DAY * 0.6)
  log('πŸΉπŸ‘¨β€πŸ‘©β€πŸ‘¦β€πŸ‘¦ 4 hunters left to bring food.')
  updateView()
}

const bring = (resource, partySize, amount, risk) => () => {
  if (Math.random() > risk) {
    log(`🌟 A party of ${partySize} has returned with ${amount} ${resource} successfully.`)
    resources[resource] += amount
    population.ready += partySize
  } else {
    log(`πŸ’€ A party of ${partySize} returned from fetching ${resource}, but got attacked by wild animals. 1 person died`, 'red')
    resources[resource] += Math.floor(amount / 2)
    population.ready += partySize - 1
    blink('population', 'red')
  }
  updateView()
  blink(resource, 'green')
}

const blink = (resource, color) => {
  $(`#${resource}`).classList.add(color)
  setTimeout(() => {
    $(`#${resource}`).classList.remove(color)
  }, 100);
}

const updateView = () => {
  $('#wood .value').innerText = resources.wood
  $('#food .value').innerText = resources.food

  $('#population .value').innerText = population.ready
  $('#hungry .value').innerText = population.hungry
  if (population.hungry < 1) {
    $('#hungry').classList.add('hidden')
  } else {
    $('#hungry').classList.remove('hidden')
  }
  
  $('#forage').disabled = population.ready < 2
  $('#chop-wood').disabled = population.ready < 2
  $('#hunt').disabled = population.ready < 4
}

updateDate = () => {
  date.setDate(date.getDate() + 1)
  $('#days .value').innerText = `${date.getDate()} / ${date.getMonth() + 1} / ${date.getFullYear()}`
}

const nextDay = () => {
  updateDate()
  
  if ((population.ready + population.hungry) < 1) {
    log(`πŸ’€πŸ’€πŸ’€ Your population was decimated`, 'red')
    stop()
  }
  if (population.hungry > 0) {
    population.hungry -= 1
    log(`πŸ’€ One person has died from starvation. +5 food.`, 'red')
    resources.food += 5
  }
  
  population.ready += population.hungry
  population.hungry = 0

  resources.food -= population.ready
  if (resources.food < 0) {
    population.ready += resources.food
    population.hungry += -resources.food
    resources.food = 0
    log(`😞 Due to lack of food, ${population.hungry} are starving and can't work.`, 'red')
  }
  updateView()
}

// init
const dayInterval = setInterval(nextDay, DAY)
const stop = () => {
  clearInterval(dayInterval)
}
updateDate()
updateView()

              
            
!
999px

Console