깔끔한 코드 작성 – 주석으로 명확하게 하기

출처: 주석으로 명확하게 하기 size(400, 400); /* This program simulates a ball bouncing, by repeatedly drawing the ball at the calculated new position, according to the arc of a parabola. */ noStroke(); // Time, starting at 0 var t = 0; // The height of the ball, where 0 is on the ground var y = 0; draw = function() { // Clear previous drawings and fill in a sky color background(214, 254, 255); // Draw the ground fill(255, 255, 255); rect(0, 250, 400, 150); // Calculate y according to the path of a parabola with respect to “t”: // y = (velocity)*t – (acceleration)*t^2; // We can tweak the […]

중첩된 for 문

출처: 중첩된 for 문 size(400, 400); for (var j = 0; j < 13; j++) { fill(255, 0, 0); text(j, 13, j*30+30); for (var i = 0; i < 12; i++) { fill(130, 56, 56); text(i, i*36, j*30+30); //image(getImage("cute/GemOrange"), i*36, j*30, 40, 60); } for (var col = 0; col < 10; col++) { for (var row = 0; row < 15; row++) { rect(col*20, row*20, 20, 20); } } } size(400, 400); for (var j = 0; j < 13; j++) { fill(255, 0, 0); text(j, 13, j*30+30); for (var i = 0; i < 12; i++) { fill(130, 56, 56); text(i, i*36, j*30+30); //image(getImage("cute/GemOrange"), i*36, j*30, 40, 60); […]

for 루프

출처: for 루프 size(400, 400); // For Loops! fill(120, 9, 148); var y = 63; while (y < 313) { text("Pineapple pizza is the best!", 69, y); y += 41; } /* // for (start; how long; change) for (var y = 63; y < 313; y += 41) { text("Pineapple pizza is the best!", 69, y); } */ size(400, 400); // For Loops! fill(120, 9, 148); var y = 63; while (y < 313) { text("Pineapple pizza is the best!", 69, y); y += 41; } /* // for (start; how long; change) for (var y = 63; y < 313; y += 41) { text("Pineapple pizza is the […]

While 루프 2: 풍선 호퍼

출처: 더 많은 반복문: 풍선 호퍼 image는 칸아카데미의 보안상 이유로 제대로 작동하지 않습니다. size(400, 400); background(99, 200, 255); stroke(116, 57, 204); fill(221, 0, 255); var x = 100; while (x < 329) { line(x, 94, 224, 300); ellipse(x, 94, 49, 68); x += 60; } /* The Three Loop Questions: 1. What do I want to repeat? 2. What do I want to change each time? 3. How long should we repeat? */ image(getImage("https://lifea.co.kr/Hopper-Jumping.png"), 223, 232); size(400, 400); background(99, 200, 255); stroke(116, 57, 204); fill(221, 0, 255); var x = 100; while (x < 329) { line(x, 94, 224, 300); ellipse(x, 94, 49, 68); x += 60; } /* The […]

While 루프

size(400, 400); fill(120, 9, 148); var message = “y is now “; var y = 40; while(y < 400) { text(message + y, 30, y); y += 20; // Go back to the start of the loop! } /* The Three Loop Questions: 1. What do I want to repeat? -> The text function with the message! 2. What do I want to change each time? -> The y position, increasing by 20 each time. 3. How long should we repeat? -> As long as y is less than 400, all the way down. */ size(400, 400); fill(120, 9, 148); var message = “y is now “; var y = […]

println()으로 디버깅하기

출처: println()로 디버깅하기 size(400, 400); background(255, 145, 0); text(“Click inside the rectangle:”, 100, 95); rect(100, 100, 150, 150); mouseClicked = function() { //println(“clicked!”); println(mouseX); println(mouseY); println(mouseX > 100 && mouseX < 250); println(mouseY > 100 && mouseY < 250); if (mouseX > 100 && mouseX < 250) { println("inside the if! wooo!"); rect(mouseX, mouseY, 5, 5); } }; 참조: println(), print() size(400, 400); background(255, 145, 0); text("Click inside the rectangle:", 100, 95); rect(100, 100, 150, 150); mouseClicked = function() { //println("clicked!"); println(mouseX); println(mouseY); println(mouseX > 100 && mouseX < 250); println(mouseY > 100 && mouseY < 250); if (mouseX > 100 && mouseX < 250 && mouseY > 100 && […]

If / Else

출처: If / Else – 파트 1 size(400, 400); // generate a random number between 0 and 1 var number = random(0, 1); var integer = round(number); // display the text fill(0, 0, 0); textSize(100); text(number, 100, 110); text(integer, 160, 350); if (integer === 0) { fill(132, 0, 255); } else { fill(217, 255, 0); } ellipse(200, 200, 100, 100); 참조: random, round size(400, 400); // generate a random number between 0 and 1 var number = random(0, 1); var integer = round(number); // display the text fill(0, 0, 0); textSize(100); text(number, 100, 110); text(integer, 160, 350); if (integer === 0) { fill(132, 0, 255); } else { fill(217, 255, […]

논리연산자

출처: 논리연산자 size(400, 400); // let’s play 4 square! draw = function() { fill(204, 204, 204); rect(0, 0, 200, 200); // top left rect(200, 0, 200, 200); // top right rect(0, 200, 200, 200); // bottom left rect(200, 200, 200, 200); // bottom right if (mouseX < 200) { fill(255, 255, 255); rect(0, 0, 200, 200); } if (mouseX > 200) { fill(255, 255, 255); rect(200, 0, 200, 200); } if (mouseX === 200 || mouseY === 200 || mouseX < 3 || mouseX > 397) { fill(255, 0, 0); text(“EDGE BALL!!!!”, 200, 200); } fill(0, 0, 0); ellipse(mouseX, mouseY, 30, 30); }; size(400, 400); // let’s play 4 square! […]

불리언

출처: 불리언 size(400, 400); var number = 48; if (number = 48) { // top right fill(0, 225, 255); ellipse(300, 100, 100, 100); } if (number === 48) { // bottom left fill(255, 25, 117); ellipse(100, 300, 100, 100); } if (number !== 48) { // bottom right fill(0, 255, 68); ellipse(300, 300, 100, 100); } size(400, 400); var number = 48; if (number = 48) { // top right fill(0, 225, 255); ellipse(300, 100, 100, 100); } if (number === 48) { // bottom left fill(255, 25, 117); ellipse(100, 300, 100, 100); } if (number !== 48) { // bottom right fill(0, 255, 68); ellipse(300, 300, 100, 100); } […]

더 많은 마우스 인터랙션

출처: 더 많은 마우스 인터랙션 size(400, 400); var position = 20; var speed = 3; draw = function() { background(202, 255, 97); fill(66, 66, 66); position = position + speed; // mouseX and mouseY // mouseIsPressed ellipse(position, mouseY, 50, 50); if (mousePressed) { ellipse(mouseX, position, 50, 50); } if (position > 375) { speed = -5; } if (position < 25) { speed = 5; } }; 마우스를 누르고 있으면 추가적인 볼 움직임을 실행합니다. 참조: mousePressed size(400, 400); var position = 20; var speed = 3; draw = function() { background(202, 255, 97); fill(66, 66, 66); position = position + speed; // mouseX and mouseY // mouseIsPressed ellipse(position, mouseY, 50, […]