Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <script src = "https://code.jquery.com/jquery-2.1.1.min.js"></script>
- </head>
- <script>
- var newWindow = window.open("", "", "top = " + ((screen.height / 2) - 300) + ", left = " + ((screen.width / 2) - 300));
- newWindow.document.title = "Some game thing by David Wilson"
- newWindow.resizeTo(600, 600)
- newWindow.focus()
- var gameWindow = newWindow.document.body
- $(gameWindow).append(
- "<br><br><br><br>" +
- "<h2 style = 'font-size: 40px; color: #2D528F; -webkit-text-stroke: 2px black; text-align: center; margin-bottom: 0; padding-bottom: 0;'>THE GAME</h2>" +
- "<p style = 'text-align: center; margin-top: 0; padding-top: 0; padding-bottom: 0; margin-bottom: 0;'>by: David Wilson</p>" +
- "<p style = 'text-align: center;'> Controls: A / D to move, K to shoot</p>" +
- "<br><br><input type = 'button' value = 'Click me to play!' id = 'clickToStart'></input>"
- )
- $(gameWindow).find("#clickToStart").css({
- "background-color" : "#B20000",
- "-moz-border-radius" : "28px",
- "-webkit-border-radius" : "28px",
- "border-radius" : "28px",
- "border" : "1px solid rgb(0,0,0)",
- "display" : "block",
- "cursor" : "pointer",
- "color" : "#ffffff",
- "font-family" : "arial",
- "font-size" : "17px",
- "padding" : "16px 31px",
- "text-decoration" : "none",
- "text-shadow" : "0px 1px 0px #2f6627",
- "text-align" : "center",
- "margin-left" : "auto",
- "margin-right" : "auto"
- })
- function startGame() {
- $(gameWindow).append(
- "<p id = 'lifeCounter'> Lives: 3 </p>" +
- "<p id = 'score'> Score: 0 </p>"
- )
- var count = 0
- var timeCount = 0
- var score = 0
- var game = {
- newEnemy : 75,
- enemyMove : 1,
- lives : 3,
- over : false,
- players : {},
- enemies : {},
- bullets : {},
- sounds : {
- lose : new Audio("LoseSound.mp3"),
- shoot : new Audio("Bullet.mp3")
- },
- addBody : function(properties) {
- if (properties["class"] == "enemy" && !game.enemies[properties["name"]]) {
- game.enemies[properties["name"]] = properties
- } else if (!game.players[properties["name"]]) {
- game.players[properties["name"]] = properties
- }
- $(gameWindow).append("<div id = '" + properties["id"] + "'></div>").find("#" + properties["id"]).css({
- "border" : "2px solid black",
- "position" : "absolute",
- "background-color" : (properties["color"] || "white"),
- "width" : (properties["sx"] || 60),
- "height" : (properties["sy"] || 60),
- "left" : (properties["x"] || 0),
- "top" : (properties["y"] || 0),
- })
- },
- create : function(object) {
- count++
- object["id"] = count
- if (object["class"] == "player" || object["class"] == "enemy") {
- game.addBody(object)
- } else if (object["class"] == "gameObject") {
- }
- },
- movePlayer : function(keyPressed) {
- var player = game.players["me"]
- var horizontal = (keyPressed == 97 ? "-" + player["speed"] : keyPressed == 100 ? player["speed"] : 0)
- $(gameWindow).find("#" + player["id"]).animate({
- left: ("+=" + horizontal)
- }, 1)
- },
- moveEnemies : function() {
- for (var enemy in game.enemies) {
- $(gameWindow).find("#" + game.enemies[enemy]["id"]).animate({
- top: ("+=" + game.enemies[enemy]["speed"]),
- }, 1)
- }
- },
- wallCollision : function(object) {
- var div = $($(gameWindow).find("#" + object["id"]))
- var x = div.css("left"), y = $(div).css("top")
- if (parseInt(x) >= 600) {
- div.css("left", (600 - object["sx"]) + "px")
- } else if (parseInt(x) < 0) {
- div.css("left", 0)
- } else if (parseInt(y) >= 540) {
- if (object["class"] == "enemy") {
- $(gameWindow).find("#" + object["id"]).remove()
- delete game.enemies[object["name"]]
- game.lives--
- $(gameWindow).find("#lifeCounter").text("Lives: " + game.lives)
- if (game.lives == 0) {
- game.over = true
- $(gameWindow).empty().append(
- "<br><br><br><br>" +
- "<h1 style = 'color: red; text-align: center;'> GAME OVER! </h1>" +
- "<p style = 'text-align: center; font-size: 30px;'> Score: " + score + " </p>" +
- "<img src = 'http://ww2.valdosta.edu/~aamckoy/sadface.jpg' style = 'margin-left: auto; margin-right: auto; display: block;'></img>"
- )
- game.sounds.lose.play()
- clearInterval(gameControl)
- }
- } else {
- div.css("top", (540 - object["sy"]) + "px")
- }
- }
- },
- fireBullet : function(properties) {
- $(gameWindow).append("<div id = 'bullet" + timeCount + "'></div>").find("#bullet" + timeCount).css({
- "border" : "2px solid black",
- "position" : "absolute",
- "width" : "10px",
- "height" : "30px",
- "left" : properties["x"],
- "top" : 450,
- "background-color" : "green",
- })
- game.bullets[timeCount.toString()] = properties
- },
- moveBullets : function() {
- for (var bullet in game.bullets) {
- $(gameWindow).find("#" + game.bullets[bullet]["id"]).css({
- "top" : ("-=" + game.bullets[bullet]["speed"])
- })
- }
- },
- collision : function(body1, body2) {
- var b1sx = body1["sx"]
- var b1sy = body1["sy"]
- var b1x = parseInt($(gameWindow).find("#" + body1["id"]).css("left"))
- var b1y = parseInt($(gameWindow).find("#" + body1["id"]).css("top"))
- var b2sx = body2["sx"]
- var b2sy = body2["sy"]
- var b2x = parseInt($(gameWindow).find("#" + body2["id"]).css("left"))
- var b2y = parseInt($(gameWindow).find("#" + body2["id"]).css("top"))
- var hit = (
- b1x + b1sx > b2x &&
- b2x + b2sx > b1x &&
- b1y + b1sy > b2y &&
- b2y + b2sy > b1y
- )
- if (hit) {
- $(gameWindow).find("#" + body1["id"]).remove()
- delete game.bullets[body1["name"]]
- $(gameWindow).find("#" + body2["id"]).remove()
- delete game.enemies[body2["name"]]
- score++
- $(gameWindow).find("#score").text("Score: " + score)
- } else if (b1y <= -30) {
- $(gameWindow).find("#" + body1["id"]).remove()
- delete game.bullets[body1["name"]]
- }
- }
- }
- game.create({
- "class" : "player",
- "name" : "me",
- "speed" : 60,
- "sx" : 60,
- "sy" : 60,
- "x" : 0,
- "y" : 480,
- "color" : "#2D528F"
- })
- $(newWindow).keypress(function(key) {
- if (key.keyCode == 97 || key.keyCode == 100) {
- game.movePlayer(key.keyCode)
- } else if (key.keyCode == 107 && !game.over) {
- game.sounds.shoot.pause()
- game.sounds.shoot.currentTime = 0
- game.sounds.shoot.play()
- game.fireBullet({
- x : parseInt($(gameWindow).find("#" + game.players["me"]["id"]).css("left")) + 25,
- y : 480,
- sx : 10,
- sy : 10,
- speed : 25,
- id : "bullet" + timeCount,
- name : timeCount
- })
- }
- })
- var gameControl = setInterval(function() {
- timeCount++
- game.wallCollision(game.players["me"])
- for (var enemy in game.enemies) {
- game.wallCollision(game.enemies[enemy])
- }
- if (timeCount % (game.newEnemy > 4 ? game.newEnemy : 5) == 0) {
- game.create({
- "class" : "enemy",
- "name" : timeCount.toString(),
- "speed" : 2,
- "sx" : 60,
- "sy" : 60,
- "x" : Math.ceil((600 - (60 * (Math.random() * (10 - 1) + 1))) / 60) * 60,
- "y" : -60,
- "color" : "#B20000"
- })
- game.newEnemy--
- }
- if (timeCount % game.enemyMove == 0) {
- game.moveEnemies()
- game.moveBullets()
- }
- for (var bullet in game.bullets) {
- for (var enemy in game.enemies) {
- game.collision(game.bullets[bullet], game.enemies[enemy])
- }
- }
- }, 25)
- }
- $(gameWindow).find("#clickToStart").click(function() {
- $(gameWindow).empty()
- startGame()
- })
- </script>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment