{"id":76243,"date":"2022-12-14T11:30:26","date_gmt":"2022-12-14T16:30:26","guid":{"rendered":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/?p=76243"},"modified":"2022-12-14T11:39:15","modified_gmt":"2022-12-14T16:39:15","slug":"collect-water","status":"publish","type":"post","link":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/2022\/12\/14\/collect-water\/","title":{"rendered":"Collect water!"},"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>This is my final project about collecting raindrops. Your health starts to lose from dehydration and the one way to survive is through collecting raindrops!. Watch out for those black ones, as they deplete your health if you touch them!<\/p>\n\n\n\n<p><a class=\"p5_sketch_link\" data-width=\"500\" data-height=\"300\" href=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-content\/uploads\/2022\/12\/Get-water-final.js\" https:=\"\" courses.ideate.cmu.edu=\"\" f2022=\"\" wp-content=\"\" uploads=\"\" raindrop.js=\"\">Get water final\n<\/a><iframe src=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-content\/plugins\/p5-embedder\/p5_iframe.html\" class=\"p5_exampleFrame\" width=\"500\" height=\"300\"><\/iframe><pre class=\"language-javascript\"><code class=\"p5_editor language-javascript\">\/\/Yunfeng Jiang\n\/\/Section E\n\n\/\/setting up variables\nvar myCanvas;\nvar waterN = 10;\nvar water = [];\nvar p;\nvar plat;\nvar waterSlopeProj = []\n\nvar acc = 0.5;\nvar size = 40;\nvar score = 0;\nvar health = 100;\n\nfunction setup() {\n    myCanvas = createCanvas(500, 300);\n    \/\/push water object to water array\n    for(var i = 0; i &lt; waterN; i++){\n        var w = createWater(random(20, width-20), random(-200, -10), 2);\n        water.push(w);\n    }\n\n    \/\/add platform object\n    if(random()&lt;0.5){\n        plat = createPlatform(-125, random(25, 50), random(200, 250), 0);\n    }\n    else{\n        plat = createPlatform(width+125, random(25, 50), random(200, 250), 1);\n    }\n    \n\n    \/\/push waterslopeProj to array\n    for(var i = 0; i &lt; waterN; i++){\n        var k = plat.h \/ plat.w; \/\/slope\n        if(plat.sdir &gt; 0.5){\n        var wsp = 100-tan(k)*(-water[i].x+plat.x+0.5*plat.w)\n        }\n        else{\n        var wsp = 100-tan(k)*(-water[i].x+plat.x-0.5*plat.w)    \n        } \/\/expression of the slope\n        \n        waterSlopeProj.push(wsp);\n    } \n    \n    \/\/add player object\n    p = player();  \n}\n\nfunction draw() {\n    \/\/draw scene\n    background(176, 192, 200);\n    myCanvas.parent('canvasPlaceholder');\n    scene();\n    strokeWeight(1)\n    \n    \n    \n\n    \/\/add rain\n    updateWater();\n    addWater();\n    \n\n    \/\/update player\n    p.display(size); \n    p.moveY();\n    playerMove();\n\n    \/\/add platform\n    updatePlatform();\n    addPlatform();\n\n    \/\/gameplay properties\n    updateHealth();\n    updateProj();\n    checkHitRain();\n    checkHitPlat();\n        \n}\n    \n\n\/\/draw scene\nfunction scene(){\n    rectMode(CORNER)\n    strokeWeight(0);\n    fill(128, 96, 67);\n    rect(0, 250, width, 50)\n    text('score = '+ score, 10, 20);\n    text('health = '+ health, 10, 50)\n}\n\n\/\/jump\nfunction keyPressed(){\n    if (keyCode === 32 & p.y == 230){\n                p.dy = -10\n                }\n            } \n\n\/\/move player left and right\nfunction playerMove(){\n    if (keyIsPressed === true){\n        \n            if (keyIsDown(65) || keyIsDown(LEFT_ARROW)){\n                if(p.x > size\/2){\n                    p.moveX(-5);\n                }\n                \n            }\n            if (keyIsDown(68) || keyIsDown(RIGHT_ARROW)){\n                if(p.x &lt; width-size\/2){\n                    p.moveX(5);\n                }\n            }    \n        }\n}\n\n\/\/update health\nfunction updateHealth(){\n    \n    \/\/deplete health\n    if (frameCount%30 == 0){\n        health = health - 1\n    }\n    \n    \/\/game over\n    if (health &lt; 0){\n        textAlign(CENTER, CENTER)\n        fill(255, 0, 0);\n        textSize(50);\n        text(\"GAME OVER\", width\/2, height\/2)\n        noLoop()\n    }\n}\n\n\/\/check player location to raindrops\nfunction checkHitRain(){\n    \/\/check distance from player to rain\n    for(var i = 0 ; i &lt; waterN; i++){\n        if(dist(p.x, p.y, water[i].x, water[i].y) &lt; size*0.75){\n            score += 1;\n            \/\/water that hits platform are considered bad water that depletes health\n            if(water[i].bad == false){\n                health += 1;\n            }\n            else{\n                health -= 1;\n            }\n            var w = createWater(random(20, width-20), random(-200, -10), 2);\n            water[i] = w;\n\n        }\n    }\n}\n \nfunction updateProj(){\n    for(var i = 0; i &lt; waterN; i++){\n        var k = plat.h \/ plat.w; \/\/slope\n            if(plat.sdir == 1){\n                waterSlopeProj[i] = 100-tan(k)*(-water[i].x+plat.x+0.5*plat.w) \/\/expression of the slope\n            }\n            else{\n                waterSlopeProj[i] = 100+tan(k)*(-water[i].x+plat.x-0.5*plat.w)  \/\/expression of the slope\n            }\n        } \n\n\n}\nfunction checkHitPlat(){\n     for(var i = 0 ; i &lt; waterN; i++){\n        var k = plat.h \/ plat.w\n        if(water[i].x &gt; plat.x-plat.w*0.5 & water[i].x < plat.x + plat.w*0.5 && water[i].y > waterSlopeProj[i] && water[i].y < 100){\n                water[i].bad = true;\n                if(plat.sdir == 1){\n                    water[i].x += 0.25*water[i].v\/k\n                    water[i].y += 0.25*water[i].v\n                }\n                else{\n                    water[i].x -= 0.25*water[i].v\/k\n                    water[i].y += 0.25*water[i].v\n                }\n            }\n        else{\n            water[i].y += water[i].v\n        }\n            \n\n}\n}\n\n\n<\/code><\/pre><\/p><\/body><\/html>\n","protected":false},"excerpt":{"rendered":"<p>This is my final project about collecting raindrops. Your health starts to lose from dehydration and the one way to survive is through collecting raindrops!. Watch out for those black ones, as they deplete your health if you touch them! Get water final \/\/Yunfeng Jiang \/\/Section E \/\/setting up variables var myCanvas; var waterN = &hellip; <a href=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/2022\/12\/14\/collect-water\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Collect water!&#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":[116,121],"tags":[],"_links":{"self":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/posts\/76243"}],"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=76243"}],"version-history":[{"count":7,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/posts\/76243\/revisions"}],"predecessor-version":[{"id":76256,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/posts\/76243\/revisions\/76256"}],"wp:attachment":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/media?parent=76243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/categories?post=76243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/tags?post=76243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}