Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, minimal-ui">
- <title>Emoji Rampage</title>
- <style type="text/css">
- * {
- -webkit-box-sizing: border-box;
- box-sizing: border-box;
- margin: 0;
- padding: 0;
- }
- html,body {
- width:100%;
- height: 100%;
- position: absolute;
- background: black;
- }
- div,nav {
- -webkit-user-select: none;
- -moz-user-select: none;
- -ms-user-select: none;
- -o-user-select: none;
- user-select: none;
- cursor: default;
- }
- div, aside, nav, label, .label {
- display: block;
- width: 100%;
- height: 100%;
- position: absolute;
- top: 0;
- left: 0;
- }
- input,input[type=button] {
- display: inline-block;
- padding: .5em;
- position: relative;
- z-index: 100;
- font-size: 14pt;
- }
- i {
- font-style: normal;
- display: block;
- width: calc(100%/16);
- height: calc(100%/16);
- float: left;
- font-size:1em;
- text-align: center;
- line-height: 170%;
- }
- /* View Styles */
- #menu.name {
- background: green;
- text-align: center;
- padding: 30% 0 0;
- }
- #menu.title {
- background: grey;
- text-align: center;
- padding: 30% 0 0;
- }
- #menu.pause {
- background: rgba(0,0,0,.5);
- text-align: center;
- color: white;
- padding: 20% 0 0;
- }
- #menu.pause input {
- display: block;
- margin: 10px auto;
- }
- /* HUD Styles */
- #hud {
- position: relative;
- font-size: 16pt;
- opacity: .7;
- }
- #health {
- position: absolute;
- top: 5px;
- left: 5px;
- color: mediumblue;
- }
- #score {
- position: absolute;
- top: 5px;
- right: 5px;
- color: white;
- }
- /* Direction Correction */
- [data-lean=left] {
- -webkit-transform: skewX(5deg);
- transform: skewX(5deg);
- }
- [data-lean=right] {
- -webkit-transform: skewX(-5deg);
- transform: skewX(-5deg);
- }
- [data-dir=left] {
- -moz-transform: scaleX(-1);
- -webkit-transform: scaleX(-1);
- transform: scaleX(-1);
- filter: FlipH;
- -ms-filter: "FlipH";
- }
- [data-dir=up] {
- -webkit-transform: rotate(-90deg);
- transform: rotate(-90deg);
- }
- [data-dir=down] {
- -webkit-transform: rotate(90deg);
- transform: rotate(90deg);
- }
- /* Touch Controls */
- #touch_controls {
- display: none;
- width: 100%;
- height: 150px;
- position: fixed;
- top: calc(100% - 150px);
- left: 0;
- padding: 15px;
- z-index: 0;
- }
- #touch_controls input {
- position: absolute;
- border-radius:100%;
- width: 50px;
- height: 50px;
- outline: none;
- }
- #keyup {
- top:5px;
- left:65px;
- }
- #keydown {
- bottom:5px;
- left:65px;
- }
- #keyleft {
- top:calc(50% - 25px);
- left:15px;
- }
- #keyright {
- top:calc(50% - 25px);
- left:115px;
- }
- #touch_controls #attack {
- width:100px;
- height:100px;
- top:calc(50% - 50px);
- right:15px;
- }
- #touch_controls #pause {
- right: 140px;
- bottom:10px;
- width:40px;
- height:40px;
- font-size:12pt;
- padding:0;
- }
- /* Responsive Resizing */
- @media (orientation: portrait) {
- body {
- width: 100vw;
- height: 100vw;
- top: 0;
- left: calc(50% - 50vw);
- }
- }
- @media (min-width:400px) and (orientation: portrait) { i{font-size:1em} }
- @media (min-width:500px) and (orientation: portrait) { i{font-size:1.25em} }
- @media (min-width:600px) and (orientation: portrait) { i{font-size:1.5em} }
- @media (min-width:700px) and (orientation: portrait) { i{font-size:2em} }
- @media (min-width:800px) and (orientation: portrait) { i{font-size:2.25em} }
- @media (min-width:900px) and (orientation: portrait) { i{font-size:2.5em} }
- @media (orientation: landscape) {
- body {
- width: 100vh;
- height: 100vh;
- top: calc(50% - 50vh);
- left: calc(50% - 50vh);
- }
- }
- @media (min-height:400px) and (orientation: landscape) { i{font-size:1em} }
- @media (min-height:500px) and (orientation: landscape) { i{font-size:1.25em} }
- @media (min-height:600px) and (orientation: landscape) { i{font-size:1.5em} }
- @media (min-height:700px) and (orientation: landscape) { i{font-size:2em} }
- @media (min-height:800px) and (orientation: landscape) { i{font-size:2.25em} }
- @media (min-height:900px) and (orientation: landscape) { i{font-size:2.5em} }
- </style>
- </head>
- <body>
- <div id=board></div>
- <div id=characters></div>
- <nav id=hud>
- <span id=health></span>
- <span id=score></span>
- </nav>
- <aside id=menu></aside>
- <!-- Menu Views -->
- <script id=clear type="text/html">
- </script>
- <script id=pause type="text/html">
- <h1>pause</h1>
- <input type=button value="Exit" onclick="pause();clearBoard('all');updateMenu('title')">
- <input type=button value="Regenerate" onclick="pause();clearBoard('all');updateMenu('clear');startGame()">
- <input type=button value="Return to Game" onclick="pause();updateMenu('clear')">
- </script>
- <script id=name type="text/html">
- <h1>Set Player Name</h1>
- <input id=playername type=text autofocus>
- <input type=submit onclick="player.name=playername.value;startGame()">
- <label for=playername></label>
- </script>
- <script id=title type="text/html">
- <h1>Emoji Rampage</h1>
- <p>Click Anywhere to start a new game</p>
- <br>
- <h4><strong>Controls</strong></h4>
- <p>Use the <kbd>arrow</kbd> keys to <em>move</em></p>
- <p>Use the <kbd>space</kbd> key to <em>attack</em></p>
- <p>Use the <kbd>escape</kbd> key to <em>pause</em></p>
- <span class=label onclick=startGame()></span>
- </script>
- <!-- Touch Controls -->
- <nav id=touch_controls>
- <input type=button id=keyleft value="←" onclick="move('left',1)">
- <input type=button id=keyup value="↑" onclick="move('up',1)">
- <input type=button id=keydown value="↓" onclick="move('down',1)">
- <input type=button id=keyright value="→" onclick="move('right',1)">
- <input type=button id=attack value="Attack" onclick="attack()">
- <input type=button id=pause value="Ⅱ" onclick="pause()">
- </nav>
- <script type="text/javascript">
- var player = {
- name: '',
- character: '😃',
- score: 0,
- health: 3,
- position: '',
- direction: 'down'
- },
- tiles = [ // name : css color : collide
- ['dirt','sienna','',false],
- ['grass','forestgreen','',false],
- ['turf','green','',false],
- ['gravel','dimgray','',false],
- ['hole','black','',true]
- ],
- items = [ // name : character : css color : hp : score : movement : damage: probability
- ['cactus','🌵','limegreen',1,0,0,0,0],
- ['mushroom','🍄','saddlebrown',1,0,0,0,0],
- ['plant','🌿','darkgreen',1,0,0,0,0],
- ['croc','🐊','greenyellow',3,15,1,2,50],
- ['ant','🐜','firebrick',2,5,2,1,20],
- ['snake','🐍','yellowgreen',2,10,0,1,10]
- ],
- board = document.getElementById('board'),
- menu = document.getElementById('menu'),
- characters = document.getElementById('characters'),
- boardTiles = [],
- boardItems = [],
- session = false;
- // Game functions
- function startGame() {
- if (player.name == ''){
- updateMenu('name');
- } else {
- player.health = 3;
- player.score = 0;
- generateBoard();
- updateMenu('clear');
- }
- }
- onload = function(){updateMenu('title');spawnBoost();spawnBeast()}
- // View related functions
- function clearBoard(scope) {
- if(scope == 'all') {
- boardTiles = [];
- boardItems = [];
- }
- for(i=0;i<256;i++){
- board.removeChild(board.firstChild);
- characters.removeChild(characters.firstChild);
- }
- }
- function generateBoard() {
- for(i=0;i<256;i++){
- boardTiles.push(Math.round(Math.random()*4));
- }
- spawnPlayer();
- for(i=0;i<256;i++){
- if(i == player.position){
- boardItems.push('player');
- } else {
- if(Math.random() > .9) {
- var random = Math.round(Math.random()*2);
- if (tiles[boardTiles[i]][3] == false) {
- boardItems.push(random + '_' + items[random][3]);
- } else {
- boardItems.push(undefined);
- }
- } else {
- boardItems.push(undefined);
- }
- }
- }
- session = true;
- updateBoard();
- }
- function updateBoard() {
- for (i=0;i<boardTiles.length;i++){
- var tile = document.createElement('i');
- tile.style.background = tiles[boardTiles[i]][1];
- tile.innerHTML = tiles[boardTiles[i]][2];
- board.appendChild(tile);
- }
- for (i=0;i<boardItems.length;i++){
- var tile = document.createElement('i');
- if ((boardItems[i] != 'player') && (boardItems[i] != 'boost') && (boardItems[i] != undefined)) {
- tile.innerHTML = items[boardItems[i].split('_')[0]][1];
- tile.setAttribute('data-beast',boardItems[i].split('_')[0]);
- tile.setAttribute('data-hp',boardItems[i].split('_')[1]);
- tile.style.color = items[boardItems[i].split('_')[0]][2];
- } else if (boardItems[i] == 'boost') {
- tile.innerHTML = '🍕';
- tile.style.color = 'orange';
- }
- if(i == player.position){
- tile.innerHTML = player.character;
- tile.style.color = 'gold';
- tile.setAttribute('data-lean',player.direction);
- }
- characters.appendChild(tile);
- }
- updateHud();
- }
- function spawnBoost() {
- if(session == true) {
- var pick = Math.round(Math.random()*256);
- if ((boardItems[pick] == undefined) && (tiles[boardTiles[pick]][3] == false)) {
- boardItems[pick] = 'boost';
- clearBoard();
- updateBoard();
- }
- }
- setTimeout(function(){spawnBoost()},(Math.round(Math.random()*15000)+15000))
- }
- function spawnBeast() {
- if(session == true) {
- var pick = Math.round(Math.random()*256),
- random = Math.round(Math.random()*2)+3;
- if ((boardItems[pick] == undefined) && (tiles[boardTiles[pick]][3] == false)) {
- boardItems[pick] = random + '_' + items[random][3];
- clearBoard();
- updateBoard();
- }
- }
- setTimeout(function(){spawnBeast()},(Math.round(Math.random()*1000)+2500))
- }
- function updateMenu(view) {
- menu.innerHTML = document.getElementById(view).innerHTML;
- menu.setAttribute('class',view);
- }
- function updateHud() {
- var hp = '';
- for(i=0;i<player.health;i++){
- hp = hp + '💙';
- }
- document.getElementById('health').innerHTML = hp;
- document.getElementById('score').innerHTML = player.name + ': ' + player.score;
- }
- // Player related functions
- function spawnPlayer() {
- player.position = Math.round(Math.random()*256);
- if (tiles[boardTiles[player.position]][3] == true) {
- spawnPlayer();
- }
- }
- function move(dir,step){
- var attempt = '';
- player.direction = dir;
- if (dir == 'up') {
- attempt = player.position - (16*step);
- }
- if (dir == 'right') {
- attempt = player.position + (1*step);
- }
- if (dir == 'down') {
- attempt = player.position + (16*step);
- }
- if (dir == 'left') {
- attempt = player.position - (1*step);
- }
- if (attempt < 0) {
- attempt = attempt + 256;
- }
- if (attempt > 256) {
- attempt = attempt - 256;
- }
- if ((tiles[boardTiles[attempt]][3] == false) && (boardItems[attempt] == undefined) || (boardItems[attempt] == 'player') || (boardItems[attempt] == 'boost')) {
- player.position = attempt;
- }
- if (boardItems[attempt] == 'boost') {
- if (player.health <= 4) {
- player.health = player.health + 1;
- }
- boardItems[attempt] = undefined;
- }
- clearBoard();
- updateBoard();
- }
- function attack(){
- if (player.direction == 'up') {
- attempt = player.position - (16);
- }
- if (player.direction == 'right') {
- attempt = player.position + (1);
- }
- if (player.direction == 'down') {
- attempt = player.position + (16);
- }
- if (player.direction == 'left') {
- attempt = player.position - (1);
- }
- if ((attempt >= 0) && (attempt <=256)) {
- if ((boardItems[attempt] != 'player') && (boardItems[attempt] != undefined) && (boardItems[attempt] != 'boost')) {
- health = (document.querySelectorAll('#characters i')[attempt].getAttribute('data-hp') - 1);
- if (health >= 1) {
- boardItems[attempt] = (boardItems[attempt].split('_')[0] +'_'+ health);
- } else {
- player.score = player.score + items[boardItems[attempt].split('_')[0]][4];
- boardItems[attempt] = undefined;
- }
- }
- document.querySelectorAll('#characters i')[attempt].innerHTML = '🔪';
- document.querySelectorAll('#characters i')[attempt].style.color = 'white';
- document.querySelectorAll('[data-lean]')[0].innerHTML = '😆';
- document.querySelectorAll('#characters i')[attempt].setAttribute('data-dir',player.direction);
- setTimeout(function(){
- clearBoard();
- updateBoard();
- },250)
- }
- }
- function run() {
- var beasts = document.querySelectorAll('[data-beast]');
- for(i=0;i<beasts.length;i++){
- var dir = Math.round(Math.random()*3)+1,
- speed = items[beasts[i].getAttribute('data-beast')][6],
- damage = items[beasts[i].getAttribute('data-beast')][7],
- probability = items[beasts[i].getAttribute('data-beast')][8];
- beasts[i].setAttribute('data-dir',dir);
- }
- }
- function animate() {
- }
- function die() {
- session = false;
- updateMenu('title');
- }
- function pause() {
- if(session==true){
- session=false
- } else {
- session=true
- };
- if(menu.className != 'pause'){
- updateMenu('pause')} else {updateMenu('clear')
- }
- }
- // Keyboard Controls
- if(('ontouchstart' in window)||(navigator.msMaxTouchPoints>0)){
- document.getElementById('touch_controls').style.display = 'block';
- }
- document.onkeydown = function(e) { e = e || window.event;
- var charCode = (typeof e.which == "number") ? e.which : e.keyCode;
- if (charCode == 38) {move('up',1)}
- if (charCode == 39) {move('right',1)}
- if (charCode == 40) {move('down',1)}
- if (charCode == 37) {move('left',1)}
- if (charCode == 32) {attack()}
- if (charCode == 27) {pause()}
- }
- </script>
- </body>
- </html>
Add Comment
Please, Sign In to add comment