利用Java语句写出一个捕鱼达人游戏
时间: 2024-02-03 17:03:38 浏览: 24
很抱歉,我是一个自然语言处理的AI,无法提供图形化游戏开发相关的回答。不过,您可以在Java中使用Swing或JavaFX库创建图形化游戏。以下是一个使用JavaFX创建的简单的捕鱼达人游戏的示例代码:
```java
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.List;
public class FishingGame extends Application {
private static final int WIDTH = 640;
private static final int HEIGHT = 480;
private List<Fish> fishes;
private Image background;
private Image hook;
private int score;
private double hookX;
private double hookY;
private double hookSpeed;
private boolean isHooking;
@Override
public void start(Stage primaryStage) throws Exception {
StackPane root = new StackPane();
Canvas canvas = new Canvas(WIDTH, HEIGHT);
root.getChildren().add(canvas);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.WHITE);
gc.fillRect(0, 0, WIDTH, HEIGHT);
background = new Image("background.jpg");
hook = new Image("hook.png");
score = 0;
hookX = WIDTH / 2;
hookY = HEIGHT - 50;
hookSpeed = 0;
isHooking = false;
fishes = new ArrayList<>();
for (int i = 0; i < 10; i++) {
fishes.add(new Fish());
}
Scene scene = new Scene(root, WIDTH, HEIGHT);
scene.setOnMousePressed(this::handleMousePressed);
scene.setOnMouseReleased(this::handleMouseReleased);
primaryStage.setTitle("Fishing game");
primaryStage.setScene(scene);
primaryStage.show();
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
gc.drawImage(background, 0, 0, WIDTH, HEIGHT);
for (Fish fish : fishes) {
gc.drawImage(fish.getImage(), fish.getX(), fish.getY(), fish.getWidth(), fish.getHeight());
fish.update();
}
if (isHooking) {
hookY += hookSpeed;
hookSpeed += 0.5;
gc.drawImage(hook, hookX, hookY);
if (hookY >= HEIGHT) {
isHooking = false;
hookY = HEIGHT - 50;
hookSpeed = 0;
} else {
for (Fish fish : fishes) {
if (fish.intersects(hookX, hookY)) {
score += fish.getScore();
isHooking = false;
hookY = HEIGHT - 50;
hookSpeed = 0;
break;
}
}
}
} else {
gc.drawImage(hook, hookX, hookY);
}
gc.setFill(Color.BLACK);
gc.fillText("Score: " + score, 10, 20);
}
};
timer.start();
}
private void handleMousePressed(MouseEvent event) {
if (!isHooking) {
isHooking = true;
hookSpeed = -10;
}
}
private void handleMouseReleased(MouseEvent event) {
// do nothing
}
private static class Fish {
private static final int WIDTH = 50;
private static final int HEIGHT = 50;
private Image image;
private double x;
private double y;
private double speed;
private int score;
public Fish() {
int rand = (int) (Math.random() * 3);
switch (rand) {
case 0:
image = new Image("fish1.png");
score = 10;
break;
case 1:
image = new Image("fish2.png");
score = 20;
break;
case 2:
image = new Image("fish3.png");
score = 30;
break;
}
x = Math.random() * (WIDTH + 1);
y = Math.random() * (HEIGHT + 1);
speed = Math.random() * 4 + 1;
}
public Image getImage() {
return image;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public int getWidth() {
return WIDTH;
}
public int getHeight() {
return HEIGHT;
}
public int getScore() {
return score;
}
public void update() {
if (x >= WIDTH) {
speed = -Math.random() * 4 - 1;
} else if (x <= 0) {
speed = Math.random() * 4 + 1;
}
x += speed;
}
public boolean intersects(double x, double y) {
return x >= this.x && x <= this.x + WIDTH && y >= this.y && y <= this.y + HEIGHT;
}
}
public static void main(String[] args) {
launch(args);
}
}
```
您可以根据自己的需求对代码进行修改和扩展。