Javascript Game

In this example below you will see how to do a Javascript Game with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by 1Acid, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright 1Acid ©

Technologies

  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Javascript Game</title>
  
  
  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <!DOCTYPE html>
<html lang="eng">
  <head>
  <script src="https://s3.amazonaws.com/codecademy-content/projects/jquery.min.js"></script>	
  
   <link rel='stylesheet' href="css/style.css">
  </head>
 	<body>
 		
 		 <div class="header">
      <div class="container">
        <h1>Plane Crash</h1>
      </div>
    </div>

    <div class="main">
      <div class="container">
        
        <form class="form">
          <input id="comment" type="text" placeholder="What do you do?">
          <button type="submit" id = "123"class="btn action">Take Action!</button>
          <button type="submit" id ="proceed" class="btn proceed">proceed</button>
        </form>

        <ul class="comments" id="begin">
        	
          
        </ul>
      </div>
    </div>

    
    <script type="text/javascript" src="js/script.js"></script>
 	</body>
 	
</html>
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/1Acid/javascript-game-pbwmZJ */
html, body {
  font-family: 'Montserrat', sans-serif;
  margin: 0;
  padding: 0;
  
}
html{
	background:url(	http://img01.deviantart.net/d460/i/2012/100/9/f/deep_green_by_alexlinde-d4vll9s.jpg) no-repeat center center fixed; 
  -webkit-background-size: fixed;
  -moz-background-size: fixed;
  -o-background-size: fixed;
  background-size: fixed;
  text-align:center;
  background-attachment: fixed;
}

h1 {
	color:#00F28F;
  border-left: 2px solid #00f28f;
  font-size: 48px;
  font-weight: 400;
  margin: 0;
  padding-left: 20px;
}

.header {
  margin: 80px 0 100px 80px;
}

.main {
  margin: 0 40px 0 40px;
  max-height: 1100px;
  overflow: hidden;
}

form {
  margin: 0 0 100px 0;
}

form input {
  background: #f0f0f0;
  border: none;
  border-left: 2px solid #fff;
  font-size: 28px;
  font-family: 'Montserrat', sans-serif;
  padding: 10px;
  transition: background 2s, border-left 2s;
  width: 400px;
}

form input:focus {
  background: #fff;
  border-left: 2px solid #000;
  box-shadow: none;
  outline: none;
  transition: background 2s, border-left 2s;
}

button.btn {
  background: transparent;
  border: none;
  color: #00f28f;
  cursor: pointer;
  font-size: 28px;
  padding: 10px 14px;
  transition: background 2s, color 1s;
}

button.btn:hover {
  background: #00f28f;
  color: #fff;
  transition: background 2s, color 1s;
}

ul {
  list-style: none;
  margin: 0;
  padding: 0;
  background-color:rgba(0,0,0,.5);
  color:#F0F0F0;
}

li {
  border-left: 2px solid #000;
  font-size: 28px;
  margin: 0 0 36px;
  opacity: 0;
  padding-left: 20px;
  
}

li:nth-child(1) {
  opacity: 1;
}
li:nth-child(2) {
  opacity: 0.9;
}
li:nth-child(3) {
  opacity: 0.8;
}
li:nth-child(4) {
  opacity: 0.7;
}
li:nth-child(5) {
  opacity: 0.6;
}
li:nth-child(6) {
  opacity: 0.5;
}
li:nth-child(7) {
  opacity: 0.4;
}
li:nth-child(8) {
  opacity: 0.3;
}
li:nth-child(9) {
  opacity: 0.2;
}
li:nth-child(10) {
  opacity: 0.1;
}
li:nth-child(11) {
  opacity: 0;
}

@media (max-width: 840px) {
  .header {
    margin: 40px 0 100px 40px;
  }
  
  .main {
    margin: 0 0 0 40px;
    max-height: 2000px;
  }
  form input {
    font-size: 24px;
    width: 50%;
  }
  li, button.btn {
    font-size: 24px;
  }
}

/*Downloaded from https://www.codeseek.co/1Acid/javascript-game-pbwmZJ */
var main = function(){
	var pockets = ["gum"];
	var pockets_max = 4;
	
	var backpack = [];
	var backpack_max = 8;
	
	var cockpit =["broken radio", "broken glasses"];
	var fuselage = ["coke", "candy bar", "matches"];
	var plane_path = ["water", "baseball cap"];
	var woods_path = [];
	
	var area = function(items, direction1, direction2, text){
		this.items = items;
		this.direction1 = direction1;
		this.direction2 = direction2;
		this.text = text;
	};
	
	var fuselage_area = new area(fuselage, cockpit_area, plane_path, "<li>Fuselage text</li>");
	var cockpit_area = new area(cockpit, fuselage_area, plane_path, "<li>Cockpit area text</li>");
	var plane_path_area = new area(plane_path, cockpit_area, woods_area, "<li>plane path area text</li>");	
	var woods_area = new area(woods_path, cockpit_area, woods_area, "<li>woods area text</li>");	
	
	
	
	
	
	// This function will display the items you are holding
	var display = function(){
		if(backpack.length > 0){
			$(".comments").prepend("<li>Pockkets: "+ pockets + "</li>");
			$(".comments").prepend("<li>Backpack: "+ backpack + "</li>");
			
		}
		else{
			$(".comments").prepend("<li>Pockkets: "+ pockets + "</li>");
		};
	};
	
	
	// This function checks to see if the pickup location contains the item you wish to pickup
	var check_contain = function(array, item){
		var count = array.length;
		for(i=0; i< count; i++){
			if(array[i] === item){
				//alert("True");
        		return true;
				
			};
			
		};
       return false;
    
	};
	
	//function to drop an item - Takes in two parameters, the item you want to drop and the location you are dropping it to
	var drop = function(item, drop_location){
		//check if the item is in the pockets
	if (check_contain(pockets, item))
		{
			//find the length of the array of the drop location
			var x = drop_location.length;
			//add the dropped item into the last position of the drop locations array
			drop_location[x] = item;
			//find the index of the dropped item from the storage location and remove it from that array
			var index = pockets.indexOf(item);
			pockets.splice(index, 1);
			return drop_location;
		}
		//check to see if the item is in the packpack and available to drop
		else if (check_contain(backpack, item)){
			//find the length of the array of the drop location
			var x = drop_location.length;
			//add the dropped item into the last position of the drop locations array
			drop_location[x] = item;
			//find the index of the dropped item from the storage location and remove it from that array
			var index = backpack.indexOf(item);
			pockets.splice(index, 1);
			return drop_location;
		};
	};

	
	//function to check if pockets is full
	var pockets_space = function(){
		if(pockets.length< 5){
			return true;
		}
		else{
			return false;
		};
	};
	
	//function to check if backpack is full
	var backpack_space = function(){
		if(backpack.length < 9){
			return true;
		}
		else{
			return false;
		};
	};
	//the pick function should allow you to take a item from 1 locations array and add it to one of your storage array
	//Location is the place you are picking the item up from
	//drop_item is the item you are removing from pocket/backpack
	//pickup item is the item you are picking up and adding your pocket/backpack
	
	var pick = function(pickup_item, location){
		if (check_contain(location, pickup_item) === true){
		
		//check if there is space in the pockets to store the item
				if(pockets_space()){
					//find the end of the array
					var x = pockets.length;
					//add the pickup_tem to the pockets array
					pockets[x] = pickup_item;
					//need to remove pickup_item from location
					var index = location.indexOf(pickup_item);
					//this should delete the picked up item from the pickup locations array
					location.splice(index, 1);
					return location;
					//display message stating item was succesfully stored in pocket
					$(".comments").prepend("<li>You picked up: "+ pickup_item +"</li>");
				}
				//if no space in pockets, is there space in the backpack?
				else if(backpack_space()){
					//find the end of the array
					var x = backpack.length;
					//add the pickup it to the backpack
					backpack[x] = pickup_item;
					//need to remove pickup_item from location
					var index = location.indexOf(pickup_item);
					//this should delete the picked up item from the pickup locations array
					location.splice(index, 1);
					return location;
					//display message stating item was succesfully stored in backpack
					$(".comments").prepend("<li>You picked up: "+ pickup_item +"</li>");
				};
		}
		else{
			 $(".comments").prepend("<li>This item is not available for pickup: "+ pickup_item +"</li>");
		};
		return false;
	};
	
	//test
	/*
	check_contain(fuselage,"coke");
	pick("coke", fuselage);
	alert("You have the following items in your pocket: "+ pockets);
	alert("the following items are available at this location: "+ fuselage);
	drop("gum", fuselage);
	alert("You have the following items in your pocket: "+ pockets);
	alert("the following items are available at this location: "+ fuselage);
	TEST WORKS!!
	*/
	
	var intro = function(){
		
		var i = 0;
		$(".comments").prepend("<li>Welcome to my game....</li>");
		
		$("#proceed").on('click',function(){
			if (i == 0) {
			 //add message1 
			 $(".comments").prepend("<li>Imagine this... You are flying over the Amazon rain forest in a small twin engine aeroplane. Suddenly there is some turbulence, the engines malfunctionand the plane begins to decent extremely quickly. The pilot tells you and the other passengers to brace for a crash landing.</li>");
          		i++;
          		//$("html").css("background", "url(http://orig06.deviantart.net/43b5/f/2008/253/1/6/mayday_by_andreewallin.jpg) no-repeat center center fixed ");
			} 
			else if (i == 1) {
			  //add message 2
			  $(".comments").prepend("<li>The pilot tries his best, but the plane is going to crash. As you decend into the forest, it us utter chaos as the wings hit the tops of trees, eventually breaking off. The plane shoots through the forest like a comet falling from the sky.<br>The Crash landing is extremely rough, and you loose conciousness</li>");
        	i++;
			}
			else if (i == 2) {
			  //add message 3
			  $(".comments").prepend("<li>You wake up a few hours later, the smell of gasoline, smoke and debris litter the immediate area. You realize you are the only survivor and the plane has been broken into three pieces, The cockpit, the fuselage and the tail section. You are bleeding but you are alive and you will survive if you can get out of the forest and back to civilisation.</li>");          
        	i++;
			} 
      		else if(i ==3) {
			 //add message 4
			 $(".comments").prepend("<li>This is where your journey begins in the fuselage, you need to figure out how you will get back and survive...</li>");
				i++;	
			}
			else if(i ==4) {
			 //add message 5
			 $(".comments").prepend("<li>You are able to pickup and drop items, by typing either 'pick up XYZ' or 'drop XYZ'. You will need varoius items to help you get out of the forest. If you drop and item, it will be available in that same spot later. You can only fit four items in your pockets at a time, and you will need to find a storage device to increase you storage ability</li>");
				//$("html").css("background", "url(https://s-media-cache-ak0.pinimg.com/originals/c1/30/3b/c1303b2099e0589e6e2029823282a6ab.jpg) no-repeat center center fixed ");
				i++;	
			}
			else if(i ==5) {
			 //add message 6
			 $(".comments").prepend("<li>You should meet and interact with varoius characters on your journey who can assist you in different ways and may require different tasks from you</li>");
				i++;	
			}
			else if(i ==6) {
			 //add message 7
			 $(".comments").prepend("<li>You have the following items on you:"+ pockets +"</li>");
				i++;	
				
			}		
			else if(i===7){
				game_start();
			};
			return false;
			});
		
		 	
	
	};
	
	//this is the start of the game	
	var game_start = function(){
				var still_playing = true;
				var currentArea = fuselage_area;
				
				while (still_playing){
					$(".comments").prepend("<li>You have the following items on you:"+ currentArea.items +"</li>");
					$(".comments").prepend(currentArea.text);
					
				};
				
				
				return false;
		 	};
	intro();

};
$(document).ready(main());

Comments