Interface 18
Sun Jan 07 2024 18:34:08 GMT+0000 (Coordinated Universal Time)
Saved by @马丽
int playerX, playerY; // Player position int targetX, targetY; // Winning posssition int box1X, box1Y; // Box 1 posssition int box2X, box2Y; // Box 2 posssition int box3X, box3Y; // Box 3 posssition int box4X, box4Y; // Box 4 posssition int box5X, box5Y; // Box 5 posssition int box6X, box6Y; // Box 6 posssition int box7X, box7Y; // Box 7 posssition int box8X, box8Y; // Box 8 posssition int box9X, box9Y; // Box 8 posssition int box10X, box10Y; // Box 8 posssition int tileSize = 40; // Size of each box in the maze color playerColor = color(255, 0, 0); // Initial player color (red) color[] boxColors = {#FF0000, #0000FF, #FFFF00, #FFC0CB, #FFA500}; // colors // Maze layout int[][] maze = { {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1}, {1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1}, {1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1}, {1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1}, {1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1}, {1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1}, {1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1}, {1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1}, {1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} }; boolean reachedBox1 = false; void setup() { size(520, 520); playerX = 1 * tileSize + tileSize / 2; // Initial player X position playerY = 1 * tileSize + tileSize / 2; // Initial player Y position targetX = 11 * tileSize + tileSize / 2; // winning X position targetY = 11 * tileSize + tileSize / 2; // Winning Y position box1X = 2 * tileSize + tileSize / 2; // Yellow Box 1 X position box1Y = 4 * tileSize + tileSize / 2; // Yellow Box 1 Y position box2X = 9 * tileSize + tileSize / 2; // Yellow Box 2 X position box2Y = 5 * tileSize + tileSize / 2; // Yellow Box 2 Y position box3X = 9 * tileSize + tileSize / 2; // Pink Box 3 X position box3Y = 7 * tileSize + tileSize / 2; // Pink Box 3 Y position box4X = 11* tileSize + tileSize / 2; // Pink Box 4 X position box4Y = 1 * tileSize + tileSize / 2; // Pink Box 4 Y position box5X = 11 * tileSize + tileSize / 2; // Blue Box 5 X position box5Y = 4 * tileSize + tileSize / 2; // Blue Box 5 Y position box6X = 8 * tileSize + tileSize / 2; // Blue Box 6 X position box6Y = 11 * tileSize + tileSize / 2; // Blue Box 6 Y position box7X = 8 * tileSize + tileSize / 2; // Red Box 7 X position box7Y = 3 * tileSize + tileSize / 2; // Red Box 7 Y position box8X = 7 * tileSize + tileSize / 2; // Red Box 8 X position box8Y = 9 * tileSize + tileSize / 2; // Red Box 8 Y position box9X = 6 * tileSize + tileSize / 2; // Red Box 8 X position box9Y = 3 * tileSize + tileSize / 2; // Red Box 8 Y position box10X = 7 * tileSize + tileSize / 2; // Red Box 8 X position box10Y = 1 * tileSize + tileSize / 2; // Red Box 8 Y position } void draw() { background(255); drawMaze(); drawPlayer(); drawTarget(); drawBox(box1X, box1Y, boxColors[2]); // Draw Box with yellow drawBox(box2X, box2Y, boxColors[2]); // Draw Box with yellow drawBox(box3X, box3Y, boxColors[3]); // Draw Box with pink drawBox(box4X, box4Y, boxColors[3]); // Draw Box with pink drawBox(box5X, box5Y, boxColors[1]); // Draw Box with blue drawBox(box6X, box6Y, boxColors[1]); // Draw Box with blue drawBox(box7X, box7Y, boxColors[0]); // Draw Box with red drawBox(box8X, box8Y, boxColors[0]); // Draw Box with red drawBox(box9X, box9Y, boxColors[4]); // Draw Box with red drawBox(box10X, box10Y, boxColors[4]); // Draw Box with red if (playerX == targetX && playerY == targetY) { fill(0); textSize(80); textAlign(CENTER, CENTER); text("You Win!", width / 2, height / 2); // win message } // Check if the player reaches Box 1 if (playerX == box1X && playerY == box1Y) { playerX = box2X; // Respawn the player at Box 2 X position playerY = box2Y; // Respawn the player at Box 2 Y position reachedBox1 = true; } if (playerX == box4X && playerY == box4Y) { playerX = box3X; // Respawn the player at Box 3 X position playerY = box3Y; // Respawn the player at Box 3 Y position reachedBox1 = true; } if (playerX == box7X && playerY == box7Y) { playerX = box8X; // Respawn the player at Box 8 X position playerY = box8Y; // Respawn the player at Box 8Y position reachedBox1 = true; } if (playerX == box6X && playerY == box6Y) { playerX = box5X; // Respawn the player at Box 5 X position playerY = box5Y; // Respawn the player at Box 5 Y position reachedBox1 = true; } if (playerX == box9X && playerY == box9Y) { playerX = box10X; // Respawn the player at Box 5 X position playerY = box10Y; // Respawn the player at Box 5 Y position reachedBox1 = true; } } void drawMaze() { for (int i = 0; i < maze.length; i++) { for (int j = 0; j < maze[i].length; j++) { if (maze[i][j] == 1) { fill(100); rect(j * tileSize, i * tileSize, tileSize, tileSize); } } } } void drawPlayer() { fill(playerColor); ellipse(playerX, playerY, tileSize * 0.8, tileSize * 0.8); } void drawTarget() { fill(0, 255, 0); rect(targetX - tileSize / 2, targetY - tileSize / 2, tileSize, tileSize); } void drawBox(int x, int y, color boxColor) { fill(boxColor); // Use the color rect(x - tileSize / 2, y - tileSize / 2, tileSize, tileSize); } void keyPressed() { int nextX = playerX; int nextY = playerY; if (keyCode == LEFT && maze[playerY / tileSize][(playerX - tileSize) / tileSize] != 1) { nextX -= tileSize; } else if (keyCode == RIGHT && maze[playerY / tileSize][(playerX + tileSize) / tileSize] != 1) { nextX += tileSize; } else if (keyCode == UP && maze[(playerY - tileSize) / tileSize][playerX / tileSize] != 1) { nextY -= tileSize; } else if (keyCode == DOWN && maze[(playerY + tileSize) / tileSize][playerX / tileSize] != 1) { nextY += tileSize; } if (nextX >= 0 && nextX < width && nextY >= 0 && nextY < height) { playerX = nextX; playerY = nextY; } } void mouseClicked() { playerColor = color(random(255), random(255), random(255)); }
Comments