ArrayList<Obstacle> obstacles = new ArrayList<Obstacle>(); float obstacle_speed = 3; float obstacle_gap = 100; float obstacle_min_height = 50; float obstacle_max_height = 300; float obstacle_min_width = 20; float obstacle_max_width = 30; class Obstacle { float x, y, width, height, speed; Obstacle(float x_, float y_, float w_, float h_, float s_) { x = x_; y = y_; width = w_; height = h_; speed = s_; } void update() { x -= speed; } void draw() { rect(x, y, width, height); } } void setup() { size(1000, 500); colorMode(HSB); rectMode(CORNER); generateObstacle(); } void draw() { background(#635C81); for (Obstacle o : obstacles) { o.update(); o.draw(); } if (obstacles.size() > 0 && obstacles.get(0).x + obstacles.get(0).width < 0) { obstacles.remove(0); } if (obstacles.size() == 0 || obstacles.get(obstacles.size()-1).x + obstacles.get(obstacles.size()-1).width < width - obstacle_gap) { generateObstacle(); } } void generateObstacle() { float obstacle_height = random(obstacle_min_height, obstacle_max_height); float obstacle_width = random(obstacle_min_width, obstacle_max_width); float obstacle_y = random(0, height - obstacle_height); float obstacle_x = width; obstacles.add(new Obstacle(obstacle_x, obstacle_y, obstacle_width, obstacle_height, obstacle_speed)); }
时间: 2024-01-13 11:03:39 浏览: 145
intent传递ArrayList<T>
这段代码看起来像是一个游戏的代码,使用 Processing 语言编写,其中定义了一个障碍物类 Obstacle,包含坐标、大小和速度等属性,以及更新和绘制方法。在 setup 函数中设置了窗口大小和背景颜色,然后调用 generateObstacle 函数生成障碍物,并在 draw 函数中更新和绘制障碍物,同时根据需要删除和生成新的障碍物。
阅读全文