{"id":68752,"date":"2021-11-07T13:52:24","date_gmt":"2021-11-07T18:52:24","guid":{"rendered":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/?p=68752"},"modified":"2021-11-07T13:52:24","modified_gmt":"2021-11-07T18:52:24","slug":"4-project-10-sonic-story","status":"publish","type":"post","link":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/2021\/11\/07\/4-project-10-sonic-story\/","title":{"rendered":"4. Project 10: Sonic Story"},"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><\/p>\n\n\n\n<div class=\"wp-block-file\"><a class=\"p5_sketch_link\" data-width=\"480\" data-height=\"300\" href=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/sketch-Copy.js\">sketch &ndash; Copy<\/a><a href=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/sketch-Copy.js\" class=\"wp-block-file__button\" download>Download<\/a><iframe src=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/plugins\/p5-embedder\/p5_iframe.html\" class=\"p5_exampleFrame\" width=\"480\" height=\"300\"><\/iframe><pre class=\"language-javascript\"><code class=\"p5_editor language-javascript\">\/\/ This program displays a game of cup pong.\n\/\/ The 'characters' are the ball, cups, table, and water in the cups.\n\/\/ When the ball bounces on different surfaces (on the floor, into a cup, etc.),\n\/\/ it produces different sounds.\n\/\/ One side wins when all of the cups are empty\n\/\/(or just a few if you change the 'win' variable in the draw function).\n\n\/\/ sounds:\nvar watercup;\nvar emptycup;\nvar bouncefloor;\nvar throwing;\nvar gameover;\n\n\n\/\/ objects:\nvar cup; \/\/ will have fields for cx, cy, and boolean isFull\nvar ball; \/\/ will have fields for x, y, dx, dy, and functions\n\n\/\/ arrays:\nvar leftCups = [];\nvar rightCups = [];\n\nfunction preload() {\n    watercup = loadSound(\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/pong-sounds-with-liquid.wav\");\n    bouncefloor = loadSound(\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/bouncing-ping-pong-ball.wav\");\n    throwing = loadSound(\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/throwing-paper-in-trash.wav\");\n    gameover = loadSound(\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/ending.wav\");\n}\n\n\/\/ makes a cup object, always full to begin\nfunction makeCup(cx, cy) {\n    cup = {x:cx, y:cy, isFull: true, drawc: drawCup};\n    return(cup);\n}\n\n\/\/ makes a ball object\nfunction makeBall(cx, cy, dirx, diry) {\n    ball = {x:cx, y:cy,\n            dx:dirx, dy:diry,\n            drawb: drawBall,\n            throwb: throwBall,\n            resetb: resetBall,\n            leftright: true};\n    return(ball);\n}\n\nfunction setup() {\n    createCanvas(480, 300);\n    frameRate(35);\t\/\/ high framerate is better for this program\n    useSound();\n    rectMode(CENTER);\n\n    \/\/ fill cup arrays:\n    \/\/ set x and y positions to create pyramid shape.\n    \/\/ the if statements create the rows for the pyramid\n    for (var i=0; i&lt;10; i++) {\n        if (i&lt;4) {\n\t    lcup = makeCup(width\/10+10,\t(i*20)+120);\n            rcup = makeCup(9*width\/10-10, (i*20)+120);\n        }\n        else if (i&lt;7) {\n\t    lcup = makeCup(width\/10+30,\t(i*20)+50);\n            rcup = makeCup(9*width\/10-30, (i*20)+50);\n        }\n        else if (i&lt;9) {\n\t    lcup = makeCup(width\/10+50,\ti*20);\n            rcup = makeCup(9*width\/10-50, i*20);\n        }\n        else {\n\t    lcup = makeCup(width\/10+70,\theight\/2);\n            rcup = makeCup(9*width\/10-70, height\/2);\n        }\n        leftCups.push(lcup);\n        rightCups.push(rcup);\n    }\n\n    \/\/ create ping pong ball\n    let dy = random(-4, 4);\n    ball = makeBall(5, height\/2, 5, dy);\n}\n\nfunction soundSetup() { \/\/ setup for audio generation\n    watercup.setVolume(0.5);\n    bouncefloor.setVolume(0.5);\n    throwing.setVolume(0.5);\n    gameover.setVolume(0.5);\n}\n\n\nfunction draw() {\n    background(133, 94, 66);\n\n    \/\/ draw the table\n    drawTable();\n\n    \/\/ draw the cups\n    for (var c=0; c&lt;leftCups.length; c++) {\n        leftCups[c].drawc();\n        rightCups[c].drawc();\n    }\n\n    \/\/ draw the ball\n    ball.drawb();\n\n    \/\/ throw the ball\n    ball.throwb();\n\n    \/\/ check for winner\n    let checkl = 0;\n    let checkr = 0;\n    for (l=0; l&lt;leftCups.length; l++) {\n        if (leftCups[l].isFull == false) { checkl+=1 }\n    }\n    for (r=0; r&lt;rightCups.length; r++) {\n        if (rightCups[r].isFull == false) { checkr+=1 }\n    }\n    \/\/ when one side wins, display text and play sound\n    \/\/ change 'win' value to see game over sooner:\n    let win = 10;\n    if (checkl == win|| checkr == win) {\n        gameover.play();\n        textAlign(CENTER, CENTER);\n        textSize(25);\n        fill(0);\n        rect(width\/2, height\/2, width\/2, 50, 10);\n        fill(255);\n        text('Game Finished!', width\/2, height\/2);\n        noLoop();\n    }\n\n\n}\n\n\/\/ draws a green table with offwhite diagonal lines:\nfunction drawTable() {\n    let linescol = color(250, 250, 225);\n    let tcol = color(0, 100, 100);\n    strokeWeight(7);\n    stroke(linescol);\n    fill(tcol);\n    rect(width\/2, height\/2, 4*width\/5, 4*height\/5);\n    line(width\/10, height\/10, 9*width\/10, 9*height\/10);\n    line(width\/10, 9*height\/10, 9*width\/10, height\/10);\n}\n\n\/\/ draws individual cups based on x and y fields:\nfunction drawCup() {\n    let diam = 20;\n    fill(200, 0, 0);\t\/\/ red cup\n    stroke(255);\n    strokeWeight(2);\n    circle(this.x, this.y, diam);\n    \/\/ if the cup hasnt been hit, it has water in it:\n    if (this.isFull == true) {\n        noStroke();\n        fill(0, 0, 200);\t\/\/ blue water\n        circle(this.x, this.y, diam\/2);\n    }\n}\n\n\/\/ draws the ball\nfunction drawBall() {\n    stroke(0);\n    strokeWeight(.5);\n    fill(250);\n    circle(this.x, this.y, 10);\n}\n\n\/\/throws the ball\nfunction throwBall() {\n    if (this.leftright==true) {\t\t\/\/ going left to right\n        if (this.x &lt; width\/2) {\n            this.x += this.dx;\n            this.y -= this.dy;\n        } else {\n            this.x += this.dx;\n            this.y += (1.5*this.dy);\n        }\n        \/\/ remove the water if the ball goes into a full cup and reset ball placement\n\t\/\/ play water cup noise if cup is hit\n        for (var j=0; j&lt;rightCups.length; j++) {\n            if (dist(rightCups[j].x, rightCups[j].y, this.x, this.y)&lt;=7 &\n\t\trightCups[j].isFull==true) {\n\t   \twatercup.play();\n                rightCups[j].isFull = false;\n                this.resetb();\n            }\n        }\n    } else {\t\t\t\t\/\/ going right to left\n        if (this.x &gt; width\/2) {\n            this.x -= this.dx;\n            this.y -= this.dy;\n        }\n        else {\n            this.x -= this.dx;\n            this.y += (1.5*this.dy);\n        }\n\t\/\/ same code to remove water from hit cup\n        for (var j=0; j&lt;leftCups.length; j++) {\n            if (dist(leftCups[j].x, leftCups[j].y, this.x, this.y)&lt;=7 &\n\t\tleftCups[j].isFull==true) {\n\t   \twatercup.play();\n                leftCups[j].isFull = false;\n                this.resetb();\n            }\n        }\n    }\n    \/\/ no cups are hit, reset the ball position & play floor bounce track\n    if (this.x > width || this.x < 0 ) {\n       \tthis.resetb();\n        bouncefloor.play();\n    }\n}\n\n\/\/resets the ball to the next players starting position\nfunction resetBall() {\n    this.leftright = -this.leftright;\n    if (this.leftright == true) {this.x=10}\n    else {this.x =width-10}\n    this.y = height\/2;\n    this.dy = random(-4, 4);\n    \/\/ plays a 'whoosh' track for each throw\n    throwing.play();\n}\n<\/code><\/pre><\/div>\n\n\n\n<p>I chose to make a program that displays a game of water pong. The sounds I use are a &lsquo;whoosh&rsquo; noise when a ball is thrown, a &lsquo;clink&rsquo; sound if the ball goes into a full cup, a bouncing pingpong ball noise if the ball does not go into any full cups, and a bell noise if there is a winner. My code is randomized in a way that every game will be slightly different. <\/p><\/body><\/html>\n","protected":false},"excerpt":{"rendered":"<p>sketch &ndash; CopyDownload \/\/ This program displays a game of cup pong. \/\/ The &#8216;characters&#8217; are the ball, cups, table, and water in the cups. \/\/ When the ball bounces on different surfaces (on the floor, into a cup, etc.), \/\/ it produces different sounds. \/\/ One side wins when all of the cups are &hellip; <a href=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/2021\/11\/07\/4-project-10-sonic-story\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;4. Project 10: Sonic Story&#8221;<\/span><\/a><\/p>\n","protected":false},"author":673,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[113,56,1],"tags":[],"_links":{"self":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/posts\/68752"}],"collection":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/users\/673"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/comments?post=68752"}],"version-history":[{"count":5,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/posts\/68752\/revisions"}],"predecessor-version":[{"id":69553,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/posts\/68752\/revisions\/69553"}],"wp:attachment":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/media?parent=68752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/categories?post=68752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/tags?post=68752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}