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 = 50; 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() { noStroke(); fill(293,224,225); rect(x, y, width, height); } } void setup() { size(1000, 500); colorMode(HSB); rectMode(CORNER); generateObstacle(); } void draw() { background(114,65,73); 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 15:03:40 浏览: 167
这段代码看起来没有明显的问题,但如果你需要修改,可以根据你的需求进行以下修改:
1. 修改障碍物的颜色:在 Obstacle 类的 draw() 方法中修改 fill() 函数的参数即可,例如 fill(255, 0, 0) 表示红色。
2. 修改障碍物的移动速度:修改 obstacle_speed 变量即可,例如将其设置为 5 表示速度更快。
3. 修改障碍物的间距:修改 obstacle_gap 变量即可,例如将其设置为 200 表示间距更大。
4. 修改障碍物的最小高度和宽度:修改 obstacle_min_height 和 obstacle_min_width 变量即可,例如将其设置为 100 和 30 表示最小高度为 100,最小宽度为 30。
5. 修改障碍物的最大高度和宽度:修改 obstacle_max_height 和 obstacle_max_width 变量即可,例如将其设置为 400 和 80 表示最大高度为 400,最大宽度为 80。
注意:修改变量之后,需要重新运行程序才能看到效果。
相关问题
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)); }
这段代码看起来像是一个游戏的代码,使用 Processing 语言编写,其中定义了一个障碍物类 Obstacle,包含坐标、大小和速度等属性,以及更新和绘制方法。在 setup 函数中设置了窗口大小和背景颜色,然后调用 generateObstacle 函数生成障碍物,并在 draw 函数中更新和绘制障碍物,同时根据需要删除和生成新的障碍物。
有一个包含1000X1000个格子的地面,上面有n个由程序控制的人物,每个格子有四种可能 1)工作地点 2)普通路面 3)障碍物 4)休息地点 游戏规则 规则1:人物可在普通路面移动,可进入工作地点工作,可进入休息地点休息,但不可移动穿过工作地点和休息地点,不可移动到障碍物 规则2:人物移动一个格子需要的时间为1,进入工作地点工作需要时间为10,进入休息地点休息需要时间为5 规则3:人物在工作地点工作一次,获得1点收入,工作一次后必须进行一次休息才可以继续工作 规则4:每个人物的休息地点是固定的,工作地点可任意 规则5:每个工作地点同时只能容纳一个人物进行工作 程序逻辑 1、随机生成格子,其中工作地点为100个,障碍物为500个,休息地点,人物均为100个(人物需要与休息地点绑定),人物初始状态为可进行工作,人物初始位置为在对应休息地点内,其他为路面 2、使用伪随机数,将9999设置为伪随机数的初始值,需要每次同样的随机值生成的地图为一样的 3、执行1000000个工作时间,计算出获得总能量数,输出执行时间和获得总能量 写出java代码
```java
import java.util.*;
public class Game {
static final int GRID_SIZE = 1000;
static final int NUM_WORKPLACES = 100;
static final int NUM_OBSTACLES = 500;
static final int NUM_RESTAREAS = 100;
static final int NUM_PEOPLE = 100;
static final int WORK_DURATION = 10;
static final int REST_DURATION = 5;
static final int WORK_REWARD = 1;
static final int[] dx = { 0, 0, 1, -1 };
static final int[] dy = { 1, -1, 0, 0 };
static final int WORKPLACE = 1;
static final int ROAD = 2;
static final int OBSTACLE = 3;
static final int RESTAREA = 4;
static final int PERSON = 5;
static int[][] grid = new int[GRID_SIZE][GRID_SIZE];
static int[] restAreas = new int[NUM_PEOPLE];
static Person[] people = new Person[NUM_PEOPLE];
static Random rand = new Random(9999);
public static void main(String[] args) {
// 初始化地图
initMap();
// 执行工作时间
int totalEnergy = 0;
for (int i = 0; i < 1000000; i++) {
movePeople();
totalEnergy += work();
}
System.out.println("执行时间:" + 1000000);
System.out.println("获得总能量:" + totalEnergy);
}
// 初始化地图
public static void initMap() {
// 生成工作地点
for (int i = 0; i < NUM_WORKPLACES; i++) {
int x = rand.nextInt(GRID_SIZE);
int y = rand.nextInt(GRID_SIZE);
while (grid[x][y] != 0) {
x = rand.nextInt(GRID_SIZE);
y = rand.nextInt(GRID_SIZE);
}
grid[x][y] = WORKPLACE;
}
// 生成障碍物
for (int i = 0; i < NUM_OBSTACLES; i++) {
int x = rand.nextInt(GRID_SIZE);
int y = rand.nextInt(GRID_SIZE);
while (grid[x][y] != 0) {
x = rand.nextInt(GRID_SIZE);
y = rand.nextInt(GRID_SIZE);
}
grid[x][y] = OBSTACLE;
}
// 生成休息地点和人物,并将人物与休息地点绑定
for (int i = 0; i < NUM_RESTAREAS; i++) {
int x = rand.nextInt(GRID_SIZE);
int y = rand.nextInt(GRID_SIZE);
while (grid[x][y] != 0) {
x = rand.nextInt(GRID_SIZE);
y = rand.nextInt(GRID_SIZE);
}
grid[x][y] = RESTAREA;
restAreas[i] = x * GRID_SIZE + y;
int px = rand.nextInt(GRID_SIZE);
int py = rand.nextInt(GRID_SIZE);
while (grid[px][py] != 0 || hasPersonAt(px, py)) {
px = rand.nextInt(GRID_SIZE);
py = rand.nextInt(GRID_SIZE);
}
grid[px][py] = PERSON;
people[i] = new Person(px, py, i);
}
}
// 移动人物
public static void movePeople() {
for (Person p : people) {
if (p.status == Person.WORKING) {
// 工作状态不移动
continue;
}
int rx = restAreas[p.id] / GRID_SIZE;
int ry = restAreas[p.id] % GRID_SIZE;
if (p.x == rx && p.y == ry) {
// 在休息地点内,不移动
continue;
}
int minDist = Integer.MAX_VALUE;
int[] nearestPos = new int[2];
for (int i = 0; i < dx.length; i++) {
int nx = p.x + dx[i];
int ny = p.y + dy[i];
if (isValidPos(nx, ny)) {
int dist = Math.abs(nx - rx) + Math.abs(ny - ry);
if (dist < minDist) {
minDist = dist;
nearestPos[0] = nx;
nearestPos[1] = ny;
}
}
}
p.x = nearestPos[0];
p.y = nearestPos[1];
grid[nearestPos[0]][nearestPos[1]] = PERSON;
}
}
// 执行工作操作
public static int work() {
int energy = 0;
List<Person> workingPeople = new ArrayList<>();
for (Person p : people) {
if (p.status == Person.WORKING) {
workingPeople.add(p);
}
}
for (Person p : workingPeople) {
int wx = getWorkplaceX();
int wy = getWorkplaceY();
p.work(wx, wy);
energy += WORK_REWARD;
}
return energy;
}
// 获取工作地点的 x 坐标
public static int getWorkplaceX() {
int wx = rand.nextInt(GRID_SIZE);
int wy = rand.nextInt(GRID_SIZE);
while (grid[wx][wy] != WORKPLACE) {
wx = rand.nextInt(GRID_SIZE);
wy = rand.nextInt(GRID_SIZE);
}
grid[wx][wy] = 0;
return wx;
}
// 获取工作地点的 y 坐标
public static int getWorkplaceY() {
int wx = rand.nextInt(GRID_SIZE);
int wy = rand.nextInt(GRID_SIZE);
while (grid[wx][wy] != 0) {
wx = rand.nextInt(GRID_SIZE);
wy = rand.nextInt(GRID_SIZE);
}
grid[wx][wy] = WORKPLACE;
return wy;
}
// 判断指定位置是否为空位置
public static boolean isValidPos(int x, int y) {
if (x < 0 || x >= GRID_SIZE || y < 0 || y >= GRID_SIZE) {
return false;
}
if (grid[x][y] == OBSTACLE || grid[x][y] == WORKPLACE) {
return false;
}
return true;
}
// 判断指定位置是否已有人物
public static boolean hasPersonAt(int x, int y) {
return grid[x][y] == PERSON;
}
}
class Person {
static final int IDLE = 0;
static final int WORKING = 1;
int x;
int y;
int id;
int status;
int restCount;
public Person(int x, int y, int id) {
this.x = x;
this.y = y;
this.id = id;
this.status = IDLE;
this.restCount = REST_DURATION;
}
// 与指定坐标的工作地点匹配,开始工作
public void work(int wx, int wy) {
if (x == wx && y == wy) {
status = WORKING;
restCount = 0;
}
}
// 工作一段时间,返回是否工作完成
public boolean working() {
restCount--;
if (restCount == 0) {
status = Person.IDLE;
restCount = Person.REST_DURATION;
return true;
}
return false;
}
}
```
阅读全文