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 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

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

              
                <body>
   <div id="app"></div>
</body>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Rubik+Mono+One');

body {
   text-align: center;
   font-family: 'Rubik Mono One', sans-serif;
   margin-top: 30px;
   
   .game-board {
      
      width: 1125px;
      height: 550px;
      margin: 0 auto;
      
   }
   .grid {
      
      margin-top: 25px;
      width: 1125px;
      height: 550px;
      margin: 0 auto;
      overflow: hidden;
   }
   .grid-cover{
      
      position: relative;
      overflow: hidden;
      background: #333;
      margin: 0 auto;
     
   }
   .grid-cover-overlay {
      #player {
         background-color: purple;
         position: relative;
         &:before{
         
         content: ' ';
         display: block;
         position: absolute;
         left: -1250px;
         top: -1250px;
         margin: 0 auto;
         width: 2500px;
         height: 2500px;
        
         background: radial-gradient(transparent 20px, #333 10%);
         }
      }
   }
   .cell {
      width: 25px;
      height: 25px;
      display: inline-block;
      float: left;
   }
   .game-dashboard {
      font-size: 15px;
      color: white;
      display: inline;
      background: black;
      border: 5px solid lime;
      padding: 5px 5px 5px 5px;
   }
   .btn {
         margin-left: 10px;
   }
    
   #floor {
         background-color: lightgray;
      }
      #player {
         background-color: purple;
   }
      #wall {
         background-color: #323232;
      }
      #health {
         background-color: green;
      }
      #weapon {
         background-color: yellow;
      }
      #enemy {
         background-color: red;
      }
      #boss {
         background-color: darkred;
         border: 1px solid black;
      }
}
              
            
!

JS

              
                // jshint esversion: 6


let grid = [
            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0],
            [0,1,0,0,0,3,1,0,0,0,1,0,0,0,1,3,0,0,0,1,0,1,5,0,0,1,1,1,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,5,1],
            [0,1,0,0,1,1,1,1,1,0,0,0,0,0,1,0,0,0,0,1,0,1,0,0,3,0,0,0,1,1,0,1,0,0,0,0,1,0,0,0,1,0,0,0,1],
            [0,0,0,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,1],
            [0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,5,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,1],
            [0,1,0,0,0,0,3,0,1,0,0,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,4,0,0,1,0,0,0,1,0,0,0,0,3,1],
            [0,1,0,0,0,0,1,0,0,0,1,0,0,0,0,4,1,0,0,1,0,0,0,1,1,1,0,1,1,1,0,1,0,0,0,0,1,0,0,0,1,0,0,0,1],
            [0,1,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,5,0,0,0,4,0,0,0,0,0,0,0,1,3,0,0,1,0,0,0,0,0,1],
            [0,1,4,0,1,1,1,1,1,0,0,0,0,0,1,0,0,0,0,1,0,0,3,0,0,1,1,1,1,1,0,0,0,0,1,0,0,0,1,0,0,0,0,0,1],
            [0,0,0,0,0,0,5,0,0,0,0,1,1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,1],
            [0,1,1,1,3,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,0,1],
            [0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,1,0,0,0,1,4,0,0,0,0,0,1,1,1,1,0,1,0,0,0,0,1,0,0,0,1,0,0,0,1],
            [0,1,4,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,0,1],
            [0,1,0,0,0,0,1,0,0,0,1,0,5,0,1,0,0,0,0,1,0,2,0,0,0,3,0,0,0,0,5,0,1,0,0,0,0,1,0,0,0,0,0,3,0],
            [0,1,0,0,1,1,1,1,1,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,3,1,0,0,0,0,0,1],
            [0,0,5,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,1],
            [0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,5,0,1,0,0,0,0,1,0,0,0,0,0,3,0],
            [0,1,0,0,0,5,0,0,1,0,0,0,0,0,1,1,0,0,0,1,0,0,4,0,0,0,0,5,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,4,1],
            [0,1,0,0,1,1,1,1,1,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,0,1],
            [5,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,1]
         ];

//weapons to be used in game
const WEAPONS = [
   {type: 'fist'},
   {type: 'baseball bat'},
   {type: 'stick'},
   {type: 'club'},
   {type: 'sword'},
   {type: 'laser'}
];

let timer;

let CHARACTER = {
   health: 10,
   weapon: WEAPONS[0].type,
   level: 1,
   xp: 60,
   damage: 5,
   pickupCount: 0
}

let ENEMY = {
   health: 25,
   level: 1,
   damage: 3
}

let HEALTH = {
   health: 10,
   level: 1
}

let BOSS = {
   health: 50,
   level: 4,
   damage: 50
}

//Cell component for each cell on the board

class Cells extends React.Component {
   componentDidMount(){
      clearTimeout(timer);
      window.addEventListener('keydown',(e)=> { e.preventDefault(); });
      window.addEventListener('keyup',this.handleKeyPress.bind(this));
      document.body.scroll = "no";
   }
   upgradeEnemies(){
		let enemy = this.props.board.state.enemy;
		enemy.level += 1;
		enemy.health = Math.round(Math.random() * enemy.level*2) + 3;
		enemy.damage += Math.round(Math.random() * enemy.level) + 5;
		this.createEnemies();
	}
	createEnemies(){
		let grid = this.props.board.state.grid;
		let player = this.props.board.state.player;
		let count = 0;
      
		grid.map((row, rI) => {
			row.map((cell, cI) => {
				if(cell === 0 && count < 10 && player.level < 4 && Math.round(Math.random() * 25) + 1 === 5){
				   grid[rI][cI] = 5;
					count++ 
				}
            if(cell === 0 && player.level === 4){
               grid[8][21] = 6;
            }
			})
		})
		this.props.board.setState({grid: grid});
	}
   
	placeRandomItems(){
		let grid = this.props.board.state.grid;
		let count = 0;
		grid.map((row, rI) => {
			row.map((cell, cI) => {
            
				if(cell === 0 && count < 15 && cell !== 2 && Math.round(Math.random() * 25) + 1 === 3){
					grid[rI][cI] = 3;
					count++
				}
				if(cell === 0 && count < 15 && cell !== 2 && Math.round(Math.random() * 25) + 1 === 4){
					grid[rI][cI] = 4;
					count++
				
            }
			})
		})
		this.props.board.setState({grid: grid});
	}
   
   pickUpItems(pos){
      let grid = this.props.board.state.grid;
      let player = this.props.board.state.player;
      
      if(grid[pos.rI][pos.cI] === 3){
         player.health += HEALTH.health;
         player.pickupCount += 1;
         if(player.pickupCount > 24 && player.xp > 250){
            player.level = player.level + 1;
            player.pickupCount = 0;
            this.placeRandomItems();
            this.upgradeEnemies();
         }
      }
      if(grid[pos.rI][pos.cI] === 4){
         let index = Math.floor(Math.random()*6);
         player.weapon = WEAPONS[index].type;
         player.damage += 5;
         player.health += 5;
         player.pickupCount += 1;
         if(player.pickupCount > 24 && player.xp > 250){
            player.level += 1;
            player.pickupCount = 0;
            this.placeRandomItems();
            this.upgradeEnemies();
         }
      }
      this.props.board.setState({grid: grid});
      
   }
   
   fightEnemies(pos){
      let grid = this.props.board.state.grid;
      let player = this.props.board.state.player;
      let enemy = this.props.board.state.enemy;
      let boss = this.props.board.state.boss;
      
      
      if(grid[pos.rI][pos.cI] === 5){
         enemy.health = enemy.health - player.damage;
         player.health = player.health - enemy.damage;
         player.pickupCount += 1;
         
         if(player.pickupCount > 24 && player.xp > 250){
            player.level += 1;
            player.pickupCount = 0;
            this.upgradeEnemies();
            this.placeRandomItems();
         }
         if(player.health <= 0){
            player.health = 10;
            player.weapon = WEAPONS[0].type;
            player.xp = 60;
            player.level = 1;
            player.pickupCount = 0;
            document.getElementById('win').innerHTML = "You Lost...";
            document.getElementById('msg').innerHTML = "resetting...";
            this.placeRandomItems();
            this.createEnemies();
            this.resetGame();
         }
         if(enemy.health <= 0){
            grid[pos.rI][pos.cI] = 0;
            player.pickupCount += 1;
            player.xp = player.xp + 20;
         }
      }
      
      if(grid[pos.rI][pos.cI] === 6){
         boss.health = boss.health - player.damage;
         player.health = player.health - boss.damage;
         player.pickupCount += 1;

         if(player.health <= 0){
            player.health = 10;
            player.weapon = WEAPONS[0].type;
            player.xp = 60;
            player.level = 1;
            player.pickupCount = 0;
            this.placeRandomItems();
            this.createEnemies();
            document.getElementById('win').innerHTML = "You Lost...";
            document.getElementById('msg').innerHTML = "resetting...";
            this.resetGame();
         }
         if(boss.health <= 0){
            player.health = 10;
            player.weapon = WEAPONS[0].type;
            player.xp = 60;
            player.level = 1;
            player.pickupCount = 0;
            this.placeRandomItems();
            document.getElementById('win').innerHTML = "You Won!";
            document.getElementById('msg').innerHTML = "resetting...";
            this.resetGame();
         }
      }
      this.props.board.setState({grid: grid});
   }
   
   resetGame(){
      let board = this.props.board;
      let player = this.props.board.state.player;
      timer = setTimeout(function(){
      board.setState({grid: grid, player: board.state.player, enemy: board.state.enemy, boss: board.state.boss});
         document.getElementById('win').innerHTML = " ";
         document.getElementById('msg').innerHTML = " ";
      },3000);
      
   }
   
   handleKeyPress(e){
      
      let grid = this.props.board.state.grid;
      let player = this.props.board.state.player;
      let enemy = this.props.board.state.enemy;
      let boss = this.props.board.state.boss;
      //up
		if(e.key === "ArrowUp"){
			e.stopImmediatePropagation();
         
			let moved = false;
			grid.map((row, rI) => {
				row.map((cell, cI) => {
					if(cell === 2 && !moved && grid[rI-1][cI] !== 1 && grid[rI-1][cI] !== 5 && grid[rI-1] !== undefined && grid[rI-1][cI] === 0 && grid[rI-1] !== row.length && cI !== row.length){
						row[cI] = grid[rI - 1][cI];
						grid[rI - 1][cI] = 2;
						moved = true;
					}
					//fight enemies
					if(cell === 2 && !moved && grid[rI-1][cI] === 5 && grid[rI-1][cI] !== 1 && grid[rI-1] !== undefined && grid[rI-1] !== row.length && cI !== row.length){
						this.fightEnemies({rI: rI-1, cI});
                  if(enemy.health <= 0){
                  grid[rI-1][cI] = 0;
                  }
					}
					//Pick up Health and Weapons
					if(cell === 2 && !moved && grid[rI-1][cI] !== 1 && grid[rI-1][cI] !== 5 && (grid[rI-1][cI] === 3 || grid[rI-1][cI] === 4) && grid[rI-1] !== undefined && grid[rI-1][cI] !== 0 && grid[rI-1] !== row.length && cI !== row.length){
						this.pickUpItems({rI: rI-1, cI});	 
						grid[rI-1][cI] = 0;
					}
               //fight boss
               
              if(cell === 2 && !moved && grid[rI-1][cI] !== 1 && grid[rI-1][cI] !== 5 && grid[rI-1] !== undefined && (grid[rI-1][cI] !== 3 || grid[rI-1][cI] !== 4) && grid[rI-1][cI] === 6 && grid[rI-1][cI] !== 0){
						
						this.fightEnemies({rI, cI: cI+1});
                 if(boss.health <= 0){
                  grid[rI-1][cI] = 0;
                 }
					}
               
					return row;
				})
				return grid;
			})
			this.props.board.setState({grid: grid});
		}
		//RIGHT
		if(e.key === "ArrowRight"){
			e.stopImmediatePropagation();
         
			let moved = false;
			grid.map((row, rI) => {
				row.map((cell, cI) => {
					if(cell === 2 && !moved && row[cI+1] !== 1 && row[cI+1] !== 5 && cI+1 !== row.length && row[cI+1] === 0 && row[cI+1] !== 6){
						row[cI] = row[cI + 1];
						row[cI + 1] = 2;
						moved = true;
             	}
               //fight enemies
					if(cell === 2 && !moved && row[cI+1] === 5 && row[cI+1] !== 1 && cI+1 !== row.length && row[cI+1] !== 6){
                  
						this.fightEnemies({rI, cI: cI+1});
                  if(enemy.health <= 0){
                  row[cI + 1] = 0;
                  }
					}
               //pick up weapon or health item
					if(cell === 2 && !moved && row[cI+1] !== 1 && row[cI+1] !== 5 && cI+1 !== row.length && (row[cI+1] === 3 || row[cI+1] === 4) && row[cI+1] !== 0 && row[cI+1] !== 6){
						this.pickUpItems({rI, cI: cI+1});
						row[cI + 1] = 0;
					}
               //fight boss
					if(cell === 2 && !moved && row[cI+1] !== 1 && row[cI+1] !== 5 && cI+1 !== row.length && (row[cI+1] !== 3 || row[cI+1] !== 4) && row[cI+1] === 6 && row[cI+1] !== 0){
						
						this.fightEnemies({rI, cI: cI+1});
                  if(boss.health <= 0){
                  row[cI + 1] = 0;
                  }
					}
					return row;
				})
				return grid;
			})
			this.props.board.setState({grid: grid})
		}
		//DOWN
		if(e.key === "ArrowDown"){
			e.stopImmediatePropagation();
         
			let moved = false;
			grid.map((row, rI) => {
				row.map((cell, cI) => {
					if(cell === 2 && !moved && grid[rI+1][cI] !== 1 &&  grid[rI+1][cI] !== 5 &&  grid[rI+1] !== undefined &&  grid[rI+1][cI] === 0 && grid[rI+1] !== row.length && cI !== row.length){
						row[cI] = grid[rI+1][cI]
						 grid[rI+1][cI] = 2
						moved = true;
               }
               //fight enemies
					if(cell === 2 && !moved &&  grid[rI+1][cI] === 5 &&  grid[rI+1][cI] !== 1 &&  grid[rI+1] !== undefined && grid[rI+1] !== row.length && cI !== row.length){
						this.fightEnemies({rI: rI+1, cI});
                  if(enemy.health <= 0){
                  grid[rI+1][cI] = 0;
                  }
					}
               // pickup health or weapon items
					if(cell === 2 && !moved &&  grid[rI+1][cI] !== 1 &&  grid[rI+1][cI] !== 5 && ( grid[rI+1][cI] === 3 ||  grid[rI+1][cI] === 4) &&  grid[rI+1] !== undefined &&  grid[rI+1][cI] !== 0 && grid[rI+1] !== row.length && cI !== row.length){
						this.pickUpItems({rI: rI+1, cI})	 
						 grid[rI+1][cI] = 0;
					}
               //fight boss
               if(cell === 2 && !moved &&  grid[rI+1][cI] === 6 &&  grid[rI+1][cI] !== 1 &&  grid[rI+1] !== undefined && (grid[rI+1][cI] !== 3 || grid[rI+1][cI] !== 4) && grid[rI+1][cI] !== 0 && grid[rI+1] !== row.length && cI !== row.length){
						this.fightEnemies({rI: rI+1, cI});
                  if(boss.health <= 0){
                  grid[rI+1][cI] = 0;
                  }
					}
					return row;
				})
				return grid;
			})
			this.props.board.setState({grid: grid});
		}
		//LEFT
		if(e.key === "ArrowLeft"){
			e.stopImmediatePropagation();
         
			let moved = false;
			grid.map((row, rI) => {
				row.map((cell, cI) => {
					if(cell === 2 && !moved && row[cI-1] !== 1 && row[cI-1] !== 5 && cI-1 !== -1 && row[cI-1] === 0){
						row[cI] = row[cI - 1];
						row[cI - 1] = 2;
						moved = true;
               }
               //fight enemies
					if(cell === 2 && !moved && row[cI-1] === 5 && row[cI-1] !== 1 && cI-1 !== -1){
						this.fightEnemies({rI, cI: cI-1});
                  if(enemy.health <= 0){
						row[cI - 1] = 0;
                  }
					}
               //pick up health or weapon items
					if(cell === 2 && !moved && row[cI-1] !== 1 && row[cI-1]  !== 5 && cI-1 !== row.length && (row[cI-1] === 3 || row[cI-1] === 4) && row[cI-1] !== 0){
						this.pickUpItems({rI, cI: cI-1})
                  row[cI - 1] = 0;
					}
               //fight boss 
               if(cell === 2 && !moved && row[cI-1] !== 1 && row[cI-1] !== 5 && cI-1 !== row.length && (row[cI-1] !== 3 || row[cI-1] !== 4) && row[cI-1] === 6 && row[cI-1] !== 0){
						this.fightEnemies({rI, cI: cI-1});
                  if(boss.health <= 0){
                  row[cI - 1] = 0;
                  }
					}
               
					return row;
				})
				return grid;
			})
			this.props.board.setState({grid: grid});
		}
     
   }
   
   render(){
      
      return ( <div className="cell" id={this.props.type}></div> );
   }
}

//this component deals with the game board assembly, renders <Cell /> component
class GameBoard extends React.Component {
   constructor(){
      super();
      this.state = {
         grid: grid,
         player: CHARACTER,
         enemy: ENEMY,
         boss: BOSS,
         dark: true
      };
   }
   changeStatus(num){
     let type;
     switch(num){
        case 0: type = "floor";
           break;
        case 1: type = "wall";
           break;
        case 2: type = "player";
           break;
        case 3: type = "health";
           break;
        case 4: type = "weapon";
           break;
        case 5: type = "enemy";
           break;
        case 6: type = "boss";
           break;
        default: type = ""
               }
     return type;
  }
   
   toggleDark(){
     this.setState({dark: !this.state.dark});  
   }
   
   render(){
      let grid = this.state.grid;
      let player = this.state.player;
      let dark = this.state.dark;
      let createLevel = grid.map((row) => {
         return row.map((each,i) => {
            
            return <Cells type={this.changeStatus(each)} index={i} board={this} player={player}/>;
         });
      });
      
      let toggle = this.state.dark ? "grid-cover-overlay" : "grid-cover";
  
 
      return (
         <div>
            <Dashboard player={player} toggleDark={this.toggleDark.bind(this)}/>
         <div className="game-board">
         <div id="grid-cover" className={toggle}>
            <div className="grid">{createLevel}</div>
            </div>
            </div>
            </div>
      );
   }
   
}


class Game extends React.Component {
   render(){
      
      return (
         <div>
         <div className="grid container-fluid">
          <GameBoard />  
         </div>
            </div>
      );
   }
}

class Dashboard extends React.Component {
   render(){
      return (
         <span>
            <h2 className="game-dashboard">Health: {this.props.player.health} </h2>
            <h2 className="game-dashboard">Weapon: {this.props.player.weapon} </h2>
            <h2 className="game-dashboard">Level: {this.props.player.level}</h2>
            <h2 className="game-dashboard">XP: {this.props.player.xp} </h2>
            <button className="btn btn-primary" onClick={this.props.toggleDark}>Toggle</button>
          </span>
      );
   }
}

class App extends React.Component {   
   
   render(){
      return (
         <div>
            <h1>Rogue-like Dungeon Crawler Game</h1>
            <p id="win"></p>
            <p id="msg"></p>
            <Game /> 
         </div>
      );
   }
}

ReactDOM.render(<App />, document.getElementById('app'));
              
            
!
999px

Console