gpt4 book ai didi

java - 我将如何在垂直堆栈中来回移动绘制 4 个方 block ?

转载 作者:行者123 更新时间:2023-12-04 09:57:51 25 4
gpt4 key购买 nike

我将如何在垂直堆栈中绘制 4 个正方形,使用循环来回移动以避免在绘制 4 个正方形时重复相同的代码 4 次。方块应该作为一个移动,当它们中的任何一个碰到窗口的边缘时,它们都会改变方向。

float location;
float sizeWidth;
float sizeHeight;
float direction;
boolean moving;

void setup() {
size (1280, 720);
location = 0;
direction = height/720;
sizeWidth = 90;
sizeHeight = 90;
moving = true;
}

void draw() {

background(255);

stroke(0);
strokeWeight(1);

for (int i = 0; i < width; i = i + 90) {
line(0, i, 1280, i);
line(i, 0, i, 720);
}

fill (255, 147, 79);
int steppedPos = (int)(location/sizeHeight+0.5);
rect(0, steppedPos*sizeHeight, sizeWidth, sizeHeight);


if (moving) {
location = location + direction;
if (location + sizeHeight > height || location < 0){
direction = direction * -1;
location = location + direction;
}
}
}



void keyPressed() {
if (key == ' ') {
moving = !moving;
}
}

最佳答案

创建一个 class Square而不是单个变量 location , sizeWidth , sizeHeightdirection :

class Square {
float locationX;
float locationY;
float sizeWidth;
float sizeHeight;
float direction;
color col;

Square(float x, float y, float w, float h, float d, color c) {
locationX = x;
locationY = y;
sizeWidth = w;
sizeHeight = h;
direction = d;
col = c;
}

// [...]
}

为正方形添加一个数组变量:

Square squares[];

for -loop 中创建正方形:

void setup() {
// [...]

squares = new Square[4];
for (int i = 0; i < squares.length; ++i ) {
squares[i] = new Square(i*90, 0, 90, 90, height/720, color(255, 147, 79));
}
}

添加一个可以绘制 Square 对象的方法:

class Square {
// [...]
void Draw() {
fill(col);
int steppedPosX = (int)(locationX/sizeWidth+0.5);
int steppedPosY = (int)(locationY/sizeHeight+0.5);
rect(steppedPosX*sizeWidth, steppedPosY*sizeHeight, sizeWidth, sizeHeight);
}
}

在循环中绘制正方形:

void draw() {
// [...]

for (int i = 0; i < squares.length; ++i ) {
squares[i].Draw();
}

// [...]
}

添加一个移动 Square 对象的方法:

class Square {
// [...]
void Move() {
locationY = locationY + direction;
if (locationY + sizeHeight > height || locationY < 0){
direction = direction * -1;
locationY = locationY + direction;
}
}
}

循环移动方块:

void draw() {
// [...]

if (moving) {
for (int i = 0; i < squares.length; ++i ) {
squares[i].Move();
}
}
}

看例子:

class Square {
float locationX;
float locationY;
float sizeWidth;
float sizeHeight;
float direction;
color col;

Square(float x, float y, float w, float h, float d, color c) {
locationX = x;
locationY = y;
sizeWidth = w;
sizeHeight = h;
direction = d;
col = c;
}

void Draw() {
fill(col);
int steppedPosX = (int)(locationX/sizeWidth+0.5);
int steppedPosY = (int)(locationY/sizeHeight+0.5);
rect(steppedPosX*sizeWidth, steppedPosY*sizeHeight, sizeWidth, sizeHeight);
}

void Move() {
locationY = locationY + direction;
if (locationY + sizeHeight > height || locationY < 0){
direction = direction * -1;
locationY = locationY + direction;
}
}
}

boolean moving;
Square squares[];

void setup() {
size (1280, 720);

squares = new Square[4];
for (int i = 0; i < squares.length; ++i ) {
squares[i] = new Square(i*90, 0, 90, 90, height/720, color(255, 147, 79));
}
moving = true;
}

void draw() {

background(255);

stroke(0);
strokeWeight(1);

for (int i = 0; i < width; i = i + 90) {
line(0, i, 1280, i);
line(i, 0, i, 720);
}

for (int i = 0; i < squares.length; ++i ) {
squares[i].Draw();
}

if (moving) {
for (int i = 0; i < squares.length; ++i ) {
squares[i].Move();
}
}
}

void keyPressed() {
if (key == ' ') {
moving = !moving;
}
}

关于java - 我将如何在垂直堆栈中来回移动绘制 4 个方 block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61883587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com