ArrayList<Trash> trashItems; // 存储垃圾的列表 Hook claw; // 钩爪对象 boolean isHoldingTrash =…
- User
ArrayList trashItems; // 存储垃圾的列表 Hook claw; // 钩爪对象 boolean isHoldingTrash = false; // 是否已经抓住了垃圾
final int SKY_HEIGHT = 150; // 天空的高度常量
// 设置游戏的初始状态 void setup() { size(800, 600); noCursor(); // 隐藏光标 trashItems = new ArrayList();
// 添加非重叠的垃圾 while (trashItems.size() < 20) { float x = random(0, width); // 在屏幕宽度范围内随机生成 x 坐标 float y = random(SKY_HEIGHT+10, height-30); // 随机生成 y 坐标,确保垃圾不会触碰屏幕边缘 float radius = random(10, 30); // 随机生成半径 color col = color(random(50, 255), random(50, 255), random(50, 255)); // 随机生成颜色 Trash newTrash = new Trash(x, y, radius, col);
// 确保垃圾不会重叠 boolean overlapping = false; for (Trash existingTrash : trashItems) { float d = dist(newTrash.x, newTrash.y, existingTrash.x, existingTrash.y); if (d < newTrash.radius + existingTrash.radius) { overlapping = true; // 有重叠,跳出循环 break; } } if (!overlapping) { trashItems.add(newTrash); }}
claw = new Hook(width/2, SKY_HEIGHT); // 在屏幕顶部中间位置初始化钩爪 }
// 每个帧的游戏画面的更新 void draw() { background(255); drawEnvironment();
for (Trash trash : trashItems) { trash.update(); trash.display(); }
claw.update(); claw.display();
checkCollisions(); // 检查有无碰撞 }
void drawEnvironment() { // 绘制天空 fill(135, 206, 235); // 浅蓝色 noStroke(); rect(0, 0, width, SKY_HEIGHT);
// 绘制海洋 fill(0, 105, 148); // 海洋蓝 rect(0, SKY_HEIGHT, width, height - SKY_HEIGHT); }
// 鼠标点击时触发的钩爪摇摆 void mousePressed() { claw.swing(); }
// 垃圾类 class Trash { float x, y; float radius; // 垃圾半径 color col; // 垃圾颜色 boolean isHooked = false; // 垃圾是否被钩住
Trash(float x_, float y_, float r_, color c_) { x = x_; y = y_; radius = r_; // 初始化半径 col = c_; // 初始化颜色 }
void update() { // 垃圾的移动和碰撞逻辑可以在这里添加 }
void display() { fill(col); noStroke(); ellipse(x, y, radius * 2, radius * 2); // 绘制垃圾 } }
// 钩爪类 class Hook { float x, y; // 钩爪的位置 float length = 0; // 钩爪绳子的长度 float swingDirection = 1; // 钩爪摇摆方向 boolean isDropping = false; // 钩爪是否在下降 float dropSpeed = 5; // 钩爪的下降速度 Trash hookedTrash = null; // 当前挂着的垃圾(如果有的话)
Hook(float x_, float y_) { x = x_; y = y_; }
void update() { // 控制钩爪左右摇摆 if (!isDropping && length == 0) { x += swingDirection; if (x < 0 || x > width) { swingDirection *= -1; // 到达屏幕边缘时反转摇摆方向 } }
// 控制钩爪的下降 else if (isDropping && !isHoldingTrash) { if (y + length < height) { length += dropSpeed; // 绳子伸长,钩爪下降 // 当钩爪进入水面时减缓速度 if (y + length > SKY_HEIGHT) { dropSpeed *= 0.99; } } else { // 钩爪碰到屏幕底部时上升 isDropping = false; } } // 控制钩爪上升 else if (!isDropping || isHoldingTrash) { length -= dropSpeed; // 绳子收回,钩爪上升 dropSpeed /= 0.99; // 上升过程中逐渐恢复速度 if (hookedTrash != null) { hookedTrash.y = y + length; // 将垃圾拉上来 } // 到达顶部后 if (length <= 0) { length = 0; dropSpeed = 5; // 重置速度 isDropping = false; // 停止上升 if (hookedTrash != null) { trashItems.remove(hookedTrash); // 移除垃圾 hookedTrash = null; isHoldingTrash = false; // 重置抓取状态 } swingDirection = (swingDirection == 0) ? 1 : swingDirection; // 恢复摇摆 } } - Assistant
为了添加垃圾之间的完全弹性碰撞,并且在水中可以减速移动,我们需要在垃圾(
Trash)类中增加一些额外的属性和行为。以下是更新后的完整代码(中文注释已添加):ArrayList<Trash> trashItems; // 存储垃圾的列表 Hook claw; // 钩爪对象 boolean isHoldingTrash = false; // 是否已经抓住了垃圾 final int SKY_HEIGHT = 150; // 天空的高度常量 final float WATER_RESISTANCE = 0.05; // 水中的阻力系数 final float GRAVITY = 0.1; // 重力加速度 // 设置游戏的初始状态 void setup() { size(800, 600); noCursor(); // 隐藏光标 trashItems = new ArrayList<Trash>(); // 添加非重叠的垃圾 while (trashItems.size() < 20) { float x = random(0, width); // 在屏幕宽度范围内随机生成 x 坐标 float y = random(SKY_HEIGHT+10, height-30); // 随机生成 y 坐标,确保垃圾不会触碰屏幕边缘 float radius = random(10, 30); // 随机生成半径 color col = color(random(50, 255), random(50, 255), random(50, 255)); // 随机生成颜色 // 随机生成速度向量 PVector velocity = new PVector(random(-2, 2), random(-2, 2)); Trash newTrash = new Trash(x, y, radius, col, velocity); // 确保垃圾不会重叠 boolean overlapping = false; for (Trash existingTrash : trashItems) { float d = dist(newTrash.x, newTrash.y, existingTrash.x, existingTrash.y); if (d < newTrash.radius + existingTrash.radius) { overlapping = true; // 有重叠,跳出循环 break; } } if (!overlapping) { trashItems.add(newTrash); } } claw = new Hook(width/2, SKY_HEIGHT); // 在屏幕顶部中间位置初始化钩爪 } // 每个帧的游戏画面的更新 void draw() { background(255); drawEnvironment(); // 更新垃圾物理属性并显示 for (int i = 0; i < trashItems.size(); i++) { Trash trash = trashItems.get(i); trash.update(); trash.display(); // 添加碰撞检测和处理 for (int j = i + 1; j < trashItems.size(); j++) { Trash other = trashItems.get(j); trash.checkCollision(other); } } claw.update(); // 更新钩爪状态及位置 claw.display(); // 显示钩爪 checkCollisions(); // 检查钩爪与垃圾的碰撞 } void drawEnvironment() { // 绘制天空和海洋 // ... 与之前无变化, 无需重复 } // 鼠标点击事件处理 // ... 与之前无变化, 无需重复 // 垃圾类 class Trash { float x, y; float radius; // 垃圾半径 color col; // 垃圾颜色 boolean isHooked = false; // 垃圾是否被钩住 PVector velocity; // 垃圾当前的速度向量 // 构造函数新增速度参数 - User
继续
- Assistant
Trash(float x_, float y_, float r_, color c_, PVector v_) { x = x_; y = y_; radius = r_; // 初始化半径 col = c_; // 初始化颜色 velocity = v_; // 初始化速度 } void update() { // 如果垃圾在水中,应用阻力 if (y > SKY_HEIGHT) { velocity.mult(1 - WATER_RESISTANCE); } // 如果垃圾没有被钩住,更新位置和应用重力 if (!isHooked) { velocity.y += GRAVITY; // 应用重力 x += velocity.x; y += velocity.y; // 让垃圾在屏幕内弹跳 if (x - radius <= 0 || x + radius >= width) { velocity.x *= -1; x = constrain(x, radius, width - radius); } if (y - radius <= SKY_HEIGHT || y + radius >= height) { velocity.y *= -1; y = constrain(y, SKY_HEIGHT + radius, height - radius); } } } void display() { // 显示垃圾 // ... 与之前无变化, 无需重复 } // 新方法:检查并处理垃圾之间的碰撞 void checkCollision(Trash other) { float d = dist(this.x, this.y, other.x, other.y); if (d < this.radius + other.radius) { // 碰撞检测 // 计算碰撞正法线方向 PVector normal = PVector.sub(other.position(), this.position()); normal.normalize(); // 计算相对速度 PVector relativeVelocity = PVector.sub(this.velocity, other.velocity); // 计算沿碰撞正法线方向的速度分量 float velocityAlongNormal = relativeVelocity.dot(normal); // 重定向速度 PVector velocityChange = PVector.mult(normal, velocityAlongNormal); this.velocity.sub(velocityChange); other.velocity.add(velocityChange); } } // 辅助方法:计算垃圾中心的位置 PVector position() { return new PVector(x, y); } } - User
与之前无变化的也显示出来,给出完整的代码,不要省略,给出中文注释
- Assistant
// 完整版Processing代码示例 import java.util.ArrayList; ArrayList<Trash> trashItems; // 存储垃圾的列表 Hook claw; // 钩爪对象 boolean isHoldingTrash = false; // 是否已经抓住了垃圾 final int SKY_HEIGHT = 150; // 天空的高度常量 final float WATER_RESISTANCE = 0.05; // 水中的阻力系数 final float GRAVITY = 0.1; // 重力加速度 // 设置游戏的初始状态 void setup() { size(800, 600); noCursor(); // 隐藏光标 trashItems = new ArrayList<Trash>(); // 添加非重叠的垃圾 while (trashItems.size() < 20) { float x = random(0, width); // 在屏幕宽度范围内随机生成 x 坐标 float y = random(SKY_HEIGHT + 10, height - 30); // 随机生成 y 坐标,确保垃圾不会触碰屏幕边缘 float radius = random(10, 30); // 随机生成半径 color col = color(random(50, 255), random(50, 255), random(50, 255)); // 随机生成颜色 PVector velocity = new PVector(random(-2, 2), random(-2, 2)); // 随机生成速度向量 Trash newTrash = new Trash(x, y, radius, col, velocity); // 确保垃圾不会重叠 boolean overlapping = false; for (Trash existingTrash : trashItems) { float d = dist(newTrash.x, newTrash.y, existingTrash.x, existingTrash.y); if (d < newTrash.radius + existingTrash.radius) { overlapping = true; // 有重叠,跳出循环 break; } } if (!overlapping) { trashItems.add(newTrash); } } claw = new Hook(width / 2, SKY_HEIGHT); // 在屏幕顶部中间位置初始化钩爪 } // 每个帧的游戏画面更新 void draw() { background(255); drawEnvironment(); // 更新垃圾物理属性并显示 for (int i = 0; i < trashItems.size(); i++) { Trash trash = trashItems.get(i); trash.update(); trash.display(); // 添加碰撞检测和处理 for (int j = i + 1; j < trashItems.size(); j++) { Trash other = trashItems.get(j); trash.checkCollision(other); } } claw.update(); // 更新钩爪状态及位置 claw.display(); // 显示钩爪 checkCollisions(); // 检查钩爪与垃圾的碰撞 } void drawEnvironment() { // 绘制天空 fill(135, 206, 235); noStroke(); rect(0, 0, width, SKY_HEIGHT); // 绘制海洋 fill(0, 105, 148); rect(0, SKY_HEIGHT, width, height - SKY_HEIGHT); } void mousePressed() { claw.swing(); } class Trash { float x, y; float radius; // 垃圾半径 color col; // 垃圾颜色 boolean isHooked = false; // 垃圾是否被钩住 PVector velocity; // 垃圾当前的速度向量 - User
继续
- Assistant