Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>NinjaMan</title>
- <style type="text/css">
- * {
- margin: 0;
- padding: 0;
- }
- .row {
- line-height: 0;
- }
- .wall {
- background-color: blue;
- height: 40px;
- width: 40px;
- display: inline-block;
- }
- .blank {
- background-color: black;
- height: 40px;
- width: 40px;
- display: inline-block;
- }
- .sushi {
- background-color: black;
- height: 40px;
- width: 40px;
- display: inline-block;
- background-image: url("img/sushi.png");
- background-size: contain;
- }
- .onigiri {
- background-color: black;
- height: 40px;
- width: 40px;
- display: inline-block;
- background-image: url("img/onigiri.png");
- background-size: contain;
- }
- #ninjaman {
- background-color: black;
- height: 40px;
- width: 40px;
- display: inline-block;
- background-image: url("img/ninja.gif");
- background-size: contain;
- position: absolute;
- }
- </style>
- </head>
- <body>
- <div id="world"></div>
- <div id="ninjaman"></div>
- <div id="score"></div>
- </body>
- <script type="text/javascript">
- var world = [
- [1,1,1,1,1],
- [1,0,2,3,1],
- [1,2,1,2,1],
- [1,3,2,3,1],
- [1,1,1,1,1]
- ]
- var worldDict = {
- 0: 'blank',
- 1: 'wall',
- 2: 'sushi',
- 3: 'onigiri'
- }
- function drawWorld(){
- output = "";
- for(var x = 0; x < world.length; x++){
- output += "<div class = 'row'>";
- for(var y = 0; y < world[row].length; y++){
- output += "<div class = '"+worldDict[world[x][y]]+"'></div>"
- }
- output += "</div>";
- }
- document.getElementById("world").innerHTML = output;
- document.getElementById("score").innerHTML = "Score: " + ninjaman.score;
- }
- var ninjaman = {
- x: 1,
- y: 1,
- score: 0
- }
- function drawNinjaman(){
- document.getElementById("ninjaman").style.top = (40*ninjaman.x) + "px"
- document.getElementById("ninjaman").style.left = (40*ninjaman.y) + "px"
- }
- document.onkeydown = function(e){
- if(e.keyCode == 37){ //left
- if(world[ninjaman.y][ninjaman.x - 1] != 1){
- ninjaman.x--;
- }
- }
- if(e.keyCode == 39){ //right
- if(world[ninjaman.y][ninjaman.x + 1] != 1){
- ninjaman.x++;
- }
- }
- if(e.keyCode == 38){ //up
- if(world[ninjaman.y - 1][ninjaman.x] != 1){
- ninjaman.y--;
- }
- }
- if(e.keyCode == 40){ //down
- if(world[ninjaman.y - 1][ninjaman.x] != 1){
- ninjaman.y++;
- }
- }
- drawNinjaman();
- if(world[ninjaman.x][ninjaman.y] == 2){
- ninjaman.score += 10;
- }
- if(world[ninjaman.x][ninjaman.y] == 3){
- ninjaman.score += 5;
- }
- world[ninjaman.x][ninjaman.y] = 0;
- drawNinjaman();
- drawWorld();
- }
Add Comment
Please, Sign In to add comment