ForeverDev

my gameee

Oct 15th, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2.     <head>
  3.         <script src = "https://code.jquery.com/jquery-2.1.1.min.js"></script>
  4.     </head>
  5.  
  6.     <script>
  7.         var newWindow = window.open("", "", "top = " + ((screen.height / 2) - 300) + ", left = " + ((screen.width / 2) - 300));
  8.         newWindow.document.title = "Some game thing by David Wilson"
  9.         newWindow.resizeTo(600, 600)
  10.         newWindow.focus()
  11.  
  12.         var gameWindow = newWindow.document.body
  13.  
  14.         $(gameWindow).append(
  15.             "<br><br><br><br>" +
  16.             "<h2 style = 'font-size: 40px; color: #2D528F; -webkit-text-stroke: 2px black; text-align: center; margin-bottom: 0; padding-bottom: 0;'>THE GAME</h2>" +
  17.             "<p style = 'text-align: center; margin-top: 0; padding-top: 0; padding-bottom: 0; margin-bottom: 0;'>by: David Wilson</p>" +
  18.             "<p style = 'text-align: center;'> Controls: A / D to move, K to shoot</p>" +
  19.             "<br><br><input type = 'button' value = 'Click me to play!' id = 'clickToStart'></input>"
  20.         )
  21.         $(gameWindow).find("#clickToStart").css({
  22.             "background-color" : "#B20000",
  23.             "-moz-border-radius" : "28px",
  24.             "-webkit-border-radius" : "28px",
  25.             "border-radius" : "28px",
  26.             "border" : "1px solid rgb(0,0,0)",
  27.             "display" : "block",
  28.             "cursor" : "pointer",
  29.             "color" : "#ffffff",
  30.             "font-family" : "arial",
  31.             "font-size" : "17px",
  32.             "padding" : "16px 31px",
  33.             "text-decoration" : "none",
  34.             "text-shadow" : "0px 1px 0px #2f6627",
  35.             "text-align" : "center",
  36.             "margin-left" : "auto",
  37.             "margin-right" : "auto"
  38.         })
  39.  
  40.         function startGame() {
  41.             $(gameWindow).append(
  42.                 "<p id = 'lifeCounter'> Lives: 3 </p>" +
  43.                 "<p id = 'score'> Score: 0 </p>"
  44.             )
  45.  
  46.             var count = 0
  47.             var timeCount = 0
  48.             var score = 0
  49.             var game = {
  50.                 newEnemy : 75,
  51.                 enemyMove : 1,
  52.                 lives : 3,
  53.                 over : false,
  54.                 players : {},
  55.                 enemies : {},
  56.                 bullets : {},
  57.                 sounds : {
  58.                     lose : new Audio("LoseSound.mp3"),
  59.                     shoot : new Audio("Bullet.mp3")
  60.                 },
  61.                 addBody : function(properties) {
  62.                     if (properties["class"] == "enemy" && !game.enemies[properties["name"]]) {
  63.                         game.enemies[properties["name"]] = properties
  64.                     } else if (!game.players[properties["name"]]) {
  65.                         game.players[properties["name"]] = properties
  66.                     }
  67.                     $(gameWindow).append("<div id = '" + properties["id"] + "'></div>").find("#" + properties["id"]).css({
  68.                         "border" : "2px solid black",
  69.                         "position" : "absolute",
  70.                         "background-color" : (properties["color"] || "white"),
  71.                         "width" : (properties["sx"] || 60),
  72.                         "height" : (properties["sy"] || 60),
  73.                         "left" : (properties["x"] || 0),
  74.                         "top" : (properties["y"] || 0),
  75.                     })
  76.                 },
  77.                 create : function(object) {
  78.                     count++
  79.                     object["id"] = count
  80.                     if (object["class"] == "player" || object["class"] == "enemy") {
  81.                         game.addBody(object)
  82.                     } else if (object["class"] == "gameObject") {
  83.  
  84.                     }
  85.                 },
  86.                 movePlayer : function(keyPressed) {
  87.                     var player = game.players["me"]
  88.                     var horizontal = (keyPressed == 97 ? "-" + player["speed"] : keyPressed == 100 ? player["speed"] : 0)
  89.                     $(gameWindow).find("#" + player["id"]).animate({
  90.                         left: ("+=" + horizontal)
  91.                     }, 1)
  92.                 },
  93.                 moveEnemies : function() {
  94.                     for (var enemy in game.enemies) {
  95.                         $(gameWindow).find("#" + game.enemies[enemy]["id"]).animate({
  96.                             top: ("+=" + game.enemies[enemy]["speed"]),
  97.                         }, 1)
  98.                     }
  99.                 },
  100.                 wallCollision : function(object) {
  101.                     var div = $($(gameWindow).find("#" + object["id"]))
  102.                     var x = div.css("left"), y = $(div).css("top")
  103.                     if (parseInt(x) >= 600) {
  104.                         div.css("left", (600 - object["sx"]) + "px")
  105.                     } else if (parseInt(x) < 0) {
  106.                         div.css("left", 0)
  107.                     } else if (parseInt(y) >= 540) {
  108.                         if (object["class"] == "enemy") {
  109.                             $(gameWindow).find("#" + object["id"]).remove()
  110.                             delete game.enemies[object["name"]]
  111.                             game.lives--
  112.                             $(gameWindow).find("#lifeCounter").text("Lives: " + game.lives)
  113.                             if (game.lives == 0) {
  114.                                 game.over = true
  115.                                 $(gameWindow).empty().append(
  116.                                     "<br><br><br><br>" +
  117.                                     "<h1 style = 'color: red; text-align: center;'> GAME OVER! </h1>" +
  118.                                     "<p style = 'text-align: center; font-size: 30px;'> Score: " + score + " </p>" +  
  119.                                     "<img src = 'http://ww2.valdosta.edu/~aamckoy/sadface.jpg' style = 'margin-left: auto; margin-right: auto; display: block;'></img>"
  120.                                 )
  121.                                 game.sounds.lose.play()
  122.                                 clearInterval(gameControl)
  123.                             }
  124.                         } else {
  125.                             div.css("top", (540 - object["sy"]) + "px")
  126.                         }
  127.                     }
  128.                 },
  129.                 fireBullet : function(properties) {
  130.                     $(gameWindow).append("<div id = 'bullet" + timeCount + "'></div>").find("#bullet" + timeCount).css({
  131.                         "border" : "2px solid black",
  132.                         "position" : "absolute",
  133.                         "width" : "10px",
  134.                         "height" : "30px",
  135.                         "left" : properties["x"],
  136.                         "top" : 450,
  137.                         "background-color" : "green",
  138.                     })
  139.                     game.bullets[timeCount.toString()] = properties
  140.                 },
  141.                 moveBullets : function() {
  142.                     for (var bullet in game.bullets) {
  143.                         $(gameWindow).find("#" + game.bullets[bullet]["id"]).css({
  144.                             "top" : ("-=" + game.bullets[bullet]["speed"])
  145.                         })
  146.                     }
  147.                 },
  148.                 collision : function(body1, body2) {
  149.                     var b1sx = body1["sx"]
  150.                     var b1sy = body1["sy"]
  151.                     var b1x = parseInt($(gameWindow).find("#" + body1["id"]).css("left"))
  152.                     var b1y = parseInt($(gameWindow).find("#" + body1["id"]).css("top"))
  153.  
  154.                     var b2sx = body2["sx"]
  155.                     var b2sy = body2["sy"]
  156.                     var b2x = parseInt($(gameWindow).find("#" + body2["id"]).css("left"))
  157.                     var b2y = parseInt($(gameWindow).find("#" + body2["id"]).css("top"))
  158.  
  159.                     var hit = (
  160.                         b1x + b1sx > b2x &&
  161.                         b2x + b2sx > b1x &&
  162.                         b1y + b1sy > b2y &&
  163.                         b2y + b2sy > b1y
  164.                     )
  165.  
  166.                     if (hit) {
  167.                         $(gameWindow).find("#" + body1["id"]).remove()
  168.                         delete game.bullets[body1["name"]]
  169.                         $(gameWindow).find("#" + body2["id"]).remove()
  170.                         delete game.enemies[body2["name"]]
  171.                         score++
  172.                         $(gameWindow).find("#score").text("Score: " + score)
  173.                     } else if (b1y <= -30) {
  174.                         $(gameWindow).find("#" + body1["id"]).remove()
  175.                         delete game.bullets[body1["name"]]
  176.                     }
  177.                 }
  178.             }
  179.             game.create({
  180.                 "class" : "player",
  181.                 "name" : "me",
  182.                 "speed" : 60,
  183.                 "sx" : 60,
  184.                 "sy" : 60,
  185.                 "x" : 0,
  186.                 "y" : 480,
  187.                 "color" : "#2D528F"
  188.             })
  189.             $(newWindow).keypress(function(key) {
  190.                 if (key.keyCode == 97 || key.keyCode == 100) {
  191.                     game.movePlayer(key.keyCode)
  192.                 } else if (key.keyCode == 107 && !game.over) {
  193.                     game.sounds.shoot.pause()
  194.                     game.sounds.shoot.currentTime = 0
  195.                     game.sounds.shoot.play()
  196.                     game.fireBullet({
  197.                         x : parseInt($(gameWindow).find("#" + game.players["me"]["id"]).css("left")) + 25,
  198.                         y : 480,
  199.                         sx : 10,
  200.                         sy : 10,
  201.                         speed : 25,
  202.                         id : "bullet" + timeCount,
  203.                         name : timeCount
  204.                     })
  205.                 }
  206.             })
  207.             var gameControl = setInterval(function() {
  208.                 timeCount++
  209.                 game.wallCollision(game.players["me"])
  210.                 for (var enemy in game.enemies) {
  211.                     game.wallCollision(game.enemies[enemy])
  212.                 }
  213.                 if (timeCount % (game.newEnemy > 4 ? game.newEnemy : 5) == 0) {
  214.                     game.create({
  215.                         "class" : "enemy",
  216.                         "name" : timeCount.toString(),
  217.                         "speed" : 2,
  218.                         "sx" : 60,
  219.                         "sy" : 60,
  220.                         "x" : Math.ceil((600 - (60 * (Math.random() * (10 - 1) + 1))) / 60) * 60,
  221.                         "y" : -60,
  222.                         "color" : "#B20000"
  223.                     })
  224.                     game.newEnemy--
  225.                 }
  226.                 if (timeCount % game.enemyMove == 0) {
  227.                     game.moveEnemies()
  228.                     game.moveBullets()
  229.                 }
  230.                 for (var bullet in game.bullets) {
  231.                     for (var enemy in game.enemies) {
  232.                         game.collision(game.bullets[bullet], game.enemies[enemy])
  233.                     }
  234.                 }
  235.             }, 25)
  236.         }
  237.         $(gameWindow).find("#clickToStart").click(function() {
  238.             $(gameWindow).empty()
  239.             startGame()
  240.         })
  241.     </script>
  242. </html>
Advertisement
Add Comment
Please, Sign In to add comment