int cityWidth = 800;
int buildingWidth = 40; int buildingHeightMin = 50; int buildingHeightMax = 200; int roadHeight = 20; int carSize = 30; float carSpeed = 2.0; float carX = 0; void setup() { size(800, 400); } void draw() { background(220); // 绘制建筑物 drawBuildings(); // 绘制道路 drawRoad(); // 移动车辆 moveCar(); // 绘制车辆 drawCar(carX, height - roadHeight - carSize); } void drawBuildings() { fill(150); for (int x = 0; x < cityWidth; x += buildingWidth + 20) { int buildingHeight = (int)random(buildingHeightMin, buildingHeightMax); rect(x, height - buildingHeight - roadHeight, buildingWidth, buildingHeight); } } void drawRoad() { fill(100); rect(0, height - roadHeight, cityWidth, roadHeight); } void drawCar(float x, float y) { fill(255, 0, 0); ellipse(x, y, carSize, carSize); } void moveCar() { carX += carSpeed; if (carX > width) { carX = -carSize; } }
沒有留言:
張貼留言