Processing:Примеры/Массив объектов: различия между версиями
Материал из Онлайн справочника
Перейти к навигацииПерейти к поиску
Myagkij (обсуждение | вклад) |
Myagkij (обсуждение | вклад) Нет описания правки |
||
Строка 3: | Строка 3: | ||
{{Перевод от Сubewriter}} | {{Перевод от Сubewriter}} | ||
{{Myagkij-редактор}} | {{Myagkij-редактор}} | ||
=Описание<ref>[https://processing.org/examples/arrayobjects.html processing.org - Array Objects]</ref>= | =Описание<ref>[https://processing.org/examples/arrayobjects.html processing.org - Array Objects]</ref>= | ||
Строка 157: | Строка 155: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
{{#set: | |||
Код примера= | |||
int unit = 40; | |||
int count; | |||
Module[] mods; | |||
void setup() { | |||
size(640, 360); | |||
noStroke(); | |||
int wideCount = width / unit; | |||
int highCount = height / unit; | |||
count = wideCount * highCount; | |||
mods = new Module[count]; | |||
int index = 0; | |||
for (int y = 0; y < highCount; y++) { | |||
for (int x = 0; x < wideCount; x++) { | |||
mods[index++] = new Module(x*unit, y*unit, unit/2, unit/2, random(0.05, 0.8), unit); | |||
} | |||
} | |||
} | |||
void draw() { | |||
background(0); | |||
for (Module mod : mods) { | |||
mod.update(); | |||
mod.display(); | |||
} | |||
} | |||
class Module { | |||
int xOffset; | |||
int yOffset; | |||
float x, y; | |||
int unit; | |||
int xDirection = 1; | |||
int yDirection = 1; | |||
float speed; | |||
// конструктор: | |||
Module(int xOffsetTemp, int yOffsetTemp, int xTemp, int yTemp, float speedTemp, int tempUnit) { | |||
xOffset = xOffsetTemp; | |||
yOffset = yOffsetTemp; | |||
x = xTemp; | |||
y = yTemp; | |||
speed = speedTemp; | |||
unit = tempUnit; | |||
} | |||
// пользовательская функция | |||
// для обновления значений в переменных: | |||
void update() { | |||
x = x + (speed * xDirection); | |||
if (x >= unit || x <= 0) { | |||
xDirection *= -1; | |||
x = x + (1 * xDirection); | |||
y = y + (1 * yDirection); | |||
} | |||
if (y >= unit || y <= 0) { | |||
yDirection *= -1; | |||
y = y + (1 * yDirection); | |||
} | |||
} | |||
// пользовательская функция для рисования объектов: | |||
void display() { | |||
fill(255); | |||
ellipse(xOffset + x, yOffset + y, 6, 6); | |||
} | |||
} | |||
}} | |||
=См.также= | =См.также= | ||
Строка 164: | Строка 232: | ||
<references /> | <references /> | ||
{{Навигационная таблица/Справочник языка Processing}} | {{Навигационная таблица/Справочник языка Processing}} | ||
{{Навигационная таблица/Processing/Примеры}} | {{Навигационная таблица/Processing/Примеры}} | ||
{{Навигационная таблица/Телепорт}} | {{Навигационная таблица/Телепорт}} |
Версия от 09:13, 23 февраля 2023
Содержание | Среда разработки Processing | Справочник языка Processing | Библиотеки | Примеры | Режимы программирования |
Перевод: Максим Кузьмин
Проверка/Оформление/Редактирование: Мякишев Е.А.
Описание[1]
Демонстрирует создание массива, содержащего объекты с нестандартными характеристиками.
Пример
int unit = 40;
int count;
Module[] mods;
void setup() {
size(640, 360);
noStroke();
int wideCount = width / unit;
int highCount = height / unit;
count = wideCount * highCount;
mods = new Module[count];
int index = 0;
for (int y = 0; y < highCount; y++) {
for (int x = 0; x < wideCount; x++) {
mods[index++] = new Module(x*unit, y*unit, unit/2, unit/2, random(0.05, 0.8), unit);
}
}
}
void draw() {
background(0);
for (Module mod : mods) {
mod.update();
mod.display();
}
}
class Module {
int xOffset;
int yOffset;
float x, y;
int unit;
int xDirection = 1;
int yDirection = 1;
float speed;
// конструктор:
Module(int xOffsetTemp, int yOffsetTemp, int xTemp, int yTemp, float speedTemp, int tempUnit) {
xOffset = xOffsetTemp;
yOffset = yOffsetTemp;
x = xTemp;
y = yTemp;
speed = speedTemp;
unit = tempUnit;
}
// пользовательская функция
// для обновления значений в переменных:
void update() {
x = x + (speed * xDirection);
if (x >= unit || x <= 0) {
xDirection *= -1;
x = x + (1 * xDirection);
y = y + (1 * yDirection);
}
if (y >= unit || y <= 0) {
yDirection *= -1;
y = y + (1 * yDirection);
}
}
// пользовательская функция для рисования объектов:
void display() {
fill(255);
ellipse(xOffset + x, yOffset + y, 6, 6);
}
}
{{#set: Код примера= int unit = 40; int count; Module[] mods;
void setup() {
size(640, 360); noStroke(); int wideCount = width / unit; int highCount = height / unit; count = wideCount * highCount; mods = new Module[count];
int index = 0; for (int y = 0; y < highCount; y++) { for (int x = 0; x < wideCount; x++) { mods[index++] = new Module(x*unit, y*unit, unit/2, unit/2, random(0.05, 0.8), unit); } }
}
void draw() {
background(0); for (Module mod : mods) { mod.update(); mod.display(); }
} class Module {
int xOffset; int yOffset; float x, y; int unit; int xDirection = 1; int yDirection = 1; float speed; // конструктор: Module(int xOffsetTemp, int yOffsetTemp, int xTemp, int yTemp, float speedTemp, int tempUnit) { xOffset = xOffsetTemp; yOffset = yOffsetTemp; x = xTemp; y = yTemp; speed = speedTemp; unit = tempUnit; } // пользовательская функция // для обновления значений в переменных: void update() { x = x + (speed * xDirection); if (x >= unit || x <= 0) { xDirection *= -1; x = x + (1 * xDirection); y = y + (1 * yDirection); } if (y >= unit || y <= 0) { yDirection *= -1; y = y + (1 * yDirection); } } // пользовательская функция для рисования объектов: void display() { fill(255); ellipse(xOffset + x, yOffset + y, 6, 6); }
} }}
См.также
Внешние ссылки
Примеры на Processing | |
---|---|
Основы |
|
Продвинутые графические эффекты |
|
Примеры из сторонних библиотек |
Категории:
- Страницы с устаревшим атрибутом подсветки синтаксиса «enclose»
- Страницы, использующие повторяющиеся аргументы в вызовах шаблонов
- Processing
- Перевод от Сubewriter
- Проверка:myagkij
- Оформление:myagkij
- Редактирование:myagkij
- Страницы, где используется шаблон "Навигационная таблица/Телепорт"
- Страницы с телепортом