{"id":75478,"date":"2022-11-10T02:24:48","date_gmt":"2022-11-10T07:24:48","guid":{"rendered":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/?p=75478"},"modified":"2022-11-10T02:31:10","modified_gmt":"2022-11-10T07:31:10","slug":"space-battle","status":"publish","type":"post","link":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/2022\/11\/10\/space-battle\/","title":{"rendered":"Space battle"},"content":{"rendered":"<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD HTML 4.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/REC-html40\/loose.dtd\">\n<html><body><p>The spaceship engages in fights with enemies by using energy machine guns and lasers. The most challenging part actually comes in making all the visuals work correctly.<\/p>\n\n\n\n<p><strong>Four characters\/sounds:<\/strong><\/p>\n\n\n\n<ul><li>Firing spaceship <\/li><li>Explosion<\/li><li>Charge laser<\/li><li>Shoot laser<\/li><\/ul><p><a class=\"p5_sketch_link\" data-width=\"400\" data-height=\"200\" href=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-content\/uploads\/2022\/11\/Space-battle.js\">Space battle<\/a><iframe src=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-content\/plugins\/p5-embedder\/p5_iframe.html\" class=\"p5_exampleFrame\" width=\"400\" height=\"200\"><\/iframe><pre class=\"language-javascript\"><code class=\"p5_editor language-javascript\">\/\/Jason Jiang\n\/\/Section E\n\n\/\/\/Storyline: The spaceship engages fights with enemies through using energy machine gun and laser.\n\n\/\/setting up variables for stars\nvar star = [];\nvar N = 10; \/\/number of starts\nvar dx = 10; \/\/speed of star\nvar w = 600; \/\/x location of enemy\nvar dShip = 10; \/\/speed of enemy\nvar a = 0;\/\/scale of laser beam\nvar imgX = 120;\/\/x location of ship\nvar imgY = 100;\/\/y location of ship\nvar angle = 0;\/\/angle of ship\nvar laserCharged = false; \/\/whether laser is charged\nvar laserFired = false; \/\/whether laser is fired\nvar enemyDestroyed = false; \/\/whether enemy is destroyed\n\n\n\/\/defining assets\nvar machineGun;\nvar explosion1;\nvar explosion2;\nvar explosion3;\nvar laserEnergy;\nvar laserBeam;\nvar flyEngine;\nvar machineGun;\nvar explosion;\nvar charge;\nvar laser;\n\n\/\/add assets\nfunction preload(){\n    spaceShip = loadImage(\"https:\/\/i.imgur.com\/D1QDG4A.png\");\n    spaceShipFire = loadImage(\"https:\/\/i.imgur.com\/h8bQMXa.png\");\n    enemy1 = loadImage(\"https:\/\/i.imgur.com\/OE8l2tK.png\");\n    explosion1 = loadImage(\"https:\/\/i.imgur.com\/c9tLLTo.png\");\n    explosion2 = loadImage(\"https:\/\/i.imgur.com\/kLTBXIa.png\");\n    explosion3 = loadImage(\"https:\/\/i.imgur.com\/0zhmrzn.png\");\n    laserEnergy = loadImage(\"https:\/\/i.imgur.com\/bmn9wZX.png\");\n    laserBeam = loadImage(\"https:\/\/i.imgur.com\/ABposfH.png\");\n    machineGun = loadSound(\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-content\/uploads\/2022\/11\/machineGun.mp3\");\n    explosion = loadSound(\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-content\/uploads\/2022\/11\/explosion.wav\");\n    charge = loadSound(\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-content\/uploads\/2022\/11\/charge.wav\")\n    laser = loadSound(\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-content\/uploads\/2022\/11\/laser.wav\")\n}\n\n\/\/ setup for audio generation\nfunction soundSetup() { \n    machineGun.setVolume(0.1);\n    explosion.setVolume(0.1);\n    charge.setVolume(0.2);\n    laser.setVolume(0.1);\n}\n\n\/\/Create galaxy background\nfunction updateStar(){\n    this.x -= dx;\n}\n\nfunction createStar(){\n    strokeWeight(3);\n    stroke(255);\n    point(this.x, this.y);\n}\n\nfunction generateStar(sx, sy){\n   for (var i=0; i&lt;N; i++){\n        var s = {\n            x: sx, y: sy,\n            stepFunction: updateStar,\n            drawFunction: createStar\n        }\n\n    }\n    return s;\n} \n\nfunction setup() {\n    createCanvas(400, 200);\n    frameRate(10);\n    imageMode(CENTER);\n    for (var i=0; i&lt;N; i++){\n        var s = generateStar(random(width), random(height))\n        star.push(s);\n }\n     useSound();\n}\n\n\nfunction draw() {\n    background(0);\n    \/\/setting up sequence for charging laser and firing laser\n    if (a &gt;= 1){\n            if(laserCharged == false){\n                laserCharged = true\n                a = 0\n                charge.stop()\n                laser.play();\n            }\n            else if(laserCharged == true & laserFired == false){\n                laserFired = true\n                a = 0\n                laser.pause();\n            }\n        }\n    \/\/resetting enemy position if destroyed  \n    if (enemyDestroyed == true){\n            w = 600;\n        }\n\n   \/\/Update star\n    for (var i=0; i&lt;N; i++){\n        var s = star[i]\n        s.stepFunction();\n        s.drawFunction();\n        \/\/Remove star that is out of bounds\n        if (s.x &lt; 0) {\n            var s = generateStar(random(width+5, width+500), random(height))\n            star.splice(i, 1, s);\n        }\n   \n    }\n    \n    \/\/flying spaceship\n    if (frameCount &gt;= 0 & frameCount < 30) { \n        flyinSpace(imgX, imgY, 0); \n    }\n    \n    \/\/killing enemy\n    if (frameCount &gt;= 30 & frameCount < 60) { \n        \n        \/\/play ship firing sound\n        if (w == 600){\n            machineGun.loop();  \n        }\n        \n            \/\/ship firing\n            shipFiring(w);\n            \/\/enemy Ship approaching\n            enemyShip(w, height\/2);\n            w -= dShip; \n       \n    \n        }\n    \n    if (frameCount &gt;= 60 & frameCount < 70) { \n        \/\/stop firing\n        flyinSpace(imgX, imgY, 0);\n        \/\/play explosion sound when the enemy is close\n        if(frameCount == 60){\n            machineGun.stop();\n            explosion.play()\n        }\n            explode(w, height\/2, 60);\n        }\n    \n    if (frameCount &gt;= 70 & frameCount < 110) { \n        \/\/setting up next enemy\n        enemyDestroyed = false;\n        if(frameCount == 70){\n            charge.loop();\n        }\n\n        \/\/dodge laser \n        if(dist(imgX, imgY, 120, 160)&gt;0 & laserCharged == true){\n        flyinSpace(imgX, imgY, 0);\n        imgY += dShip;  \n        }\n        else{\n            flyinSpace(imgX, imgY, 0);\n        }\n\n        \/\/enemy Ship\n            enemyShip(w, height\/2);\n            w -= 0.5*dShip;\n        \n        \/\/charge laser\n        if (laserCharged == false){\n            laserCharge(w, height\/2, a);\n            a+=0.05;\n        }\n        \/\/fire laser\n        else if(laserCharged == true & laserFired == false){\n            \n            laserCharge(w, height\/2, 1);\n            laserFire(w, height\/2, -a);\n            a+=0.05;\n        }\n    \n   \n}\n        \n        if (frameCount &gt;= 110 & frameCount < 140) {\n            \/\/preparing for next shot\n            if (frameCount == 110){\n              laserCharged = false;\n              laserFired = false; \n              charge.loop();\n            }\n\n            \/\/enemy Ship\n            enemyShip(w, height\/2);\n            w -= 0.5*dShip;\n            \n            \/\/ship fire laser\n            \/\/rotate ship\n            if (angle&gt;=-18){\n                flyinSpace(imgX, imgY, angle);\n                angle-=3;\n            }\n            \/\/fire laser after rotation\n            else{\n                \/\/charge laser\n                flyinSpace(imgX, imgY, angle)\n                if (laserCharged == false){\n                    laserCharge(imgX, imgY, a);\n                    a+=0.1;\n                }\n                \/\/fire laser\n                else if(laserCharged == true & laserFired == false){\n                    \n                    laserCharge(imgX, imgY, 1);\n                    laserFire(imgX, imgY, a, angle);\n                a+=0.1;\n            }\n             \n        }\n    }\n        \n        if (frameCount &gt;= 140 & frameCount < 150) { \n        flyinSpace(imgX, imgY, angle);\n        \/\/play explosion sound when the enemy is close\n            if(frameCount == 140){\n                explosion.play();\n            }\n            explode(w, height\/2, 140);\n        }\n\n        if (frameCount &gt;= 150){\n            background(0);\n            noLoop();\n        }\n}\n\n\/\/adding spaceship\nfunction flyinSpace(x, y, a){\n        push()\n        translate(x, y);\n        rotate(radians(a));\n        image(spaceShip, 0, 0);\n        pop()\n        \n}\n\nfunction shipFiring(w){\n        \/\/ship firing\n        if(w%20 == 0){\n            image(spaceShipFire, 120, height\/2);\n        }\n        else{\n            image(spaceShip, 120, height\/2);\n        }\n    }\n\nfunction enemyShip(w, h){ \n        \/\/enemy ship flying\n            image(enemy1, w, h);\n        }\n\nfunction explode(w, h, f){\n        \/\/create explosion when the enemy is close\n                image(explosion1, w, h);\n                if(frameCount-f &gt;= 3){\n                    image(explosion2, w, h);\n                }\n                if(frameCount-f &gt;= 6){\n                    image(explosion3, w, h);\n                    enemyDestroyed = true;\n                }\n        \n        }\n\n\/\/adding laser charge\nfunction laserCharge(x, y, a){\n        push()\n        translate(x, y);\n        scale(a);\n        image(laserEnergy, 0, 0);\n        pop()\n\n}\n\n\/\/adding laser beam\nfunction laserFire(x, y, a, angle){\n        push()\n        translate(x, y);\n        rotate(radians(angle));\n        scale(-a, 1);\n        image(laserBeam, -211, -0.5);\n        pop()\n\n}\n\n\n<\/code><\/pre><\/p><\/body><\/html>\n","protected":false},"excerpt":{"rendered":"<p>The spaceship engages in fights with enemies by using energy machine guns and lasers. The most challenging part actually comes in making all the visuals work correctly. Four characters\/sounds: Firing spaceship Explosion Charge laser Shoot laser Space battle \/\/Jason Jiang \/\/Section E \/\/\/Storyline: The spaceship engages fights with enemies through using energy machine gun and &hellip; <a href=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/2022\/11\/10\/space-battle\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Space battle&#8221;<\/span><\/a><\/p>\n","protected":false},"author":723,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[113,121],"tags":[],"_links":{"self":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/posts\/75478"}],"collection":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/users\/723"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/comments?post=75478"}],"version-history":[{"count":3,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/posts\/75478\/revisions"}],"predecessor-version":[{"id":75488,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/posts\/75478\/revisions\/75488"}],"wp:attachment":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/media?parent=75478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/categories?post=75478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/tags?post=75478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}