글자의 움직임 프로세싱 예제 |
|||||
---|---|---|---|---|---|
작성자 | 김용남 | 등록일 | 22.05.16 | 조회수 | 24 |
PFont f; String message = "마우스를 누르면 춤추는 글자들이 멈춰요"; // An array of Letter objects Letter[] letters;
void setup() { size(600, 200); // Load the font f = createFont("굴림",20,true); textFont(f);
// Create the array the same size as the String letters = new Letter[message.length()]; // Initialize Letters at the correct x location int x = 16; for (int i = 0; i < message.length(); i++) { letters[i] = new Letter(x,100,message.charAt(i)); x += textWidth(message.charAt(i)); } }
void draw() { background(255); for (int i = 0; i < letters.length; i++) { // Display all letters letters[i].display();
// If the mouse is pressed the letters shake // If not, they return to their original location if (mousePressed) { letters[i].home(); } else { letters[i].shake(); } } }
// A class to describe a single Letter class Letter { char letter; // The object knows its original "home" location float homex,homey; // As well as its current location float x,y;
Letter (float x_, float y_, char letter_) { homex = x = x_; homey = y = y_; letter = letter_; }
// Display the letter void display() { fill(0); textAlign(LEFT); text(letter,x,y); }
// Move the letter randomly void shake() { x += random(-2,2); y += random(-2,2); }
// Return the letter home void home() { x = homex; y = homey; } } |
이전글 | 벽돌깨기 기본 이미지 활용 |
---|---|
다음글 | hello world |