用java写
时间: 2023-12-30 12:04:37 浏览: 25
Sure! Here's a possible implementation of the first stage of the Balloon Pop game in Java:
```
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BalloonPop extends JPanel implements Runnable {
private static final int WIDTH = 500;
private static final int HEIGHT = 500;
private static final int MAX_RADIUS = 40;
private static final int MIN_RADIUS = 20;
private static final int GREEN_BALLOON = 0;
private static final int RED_BALLOON = 1;
private static final int MAX_SPEED = 100;
private static final double MIN_TIME = 0.0;
private static final double MAX_TIME = 2.0;
private ArrayList<Balloon> balloons;
private int score;
private boolean running;
public BalloonPop() {
balloons = new ArrayList<Balloon>();
score = 0;
running = true;
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
for (int i = 0; i < balloons.size(); i++) {
if (balloons.get(i).contains(e.getX(), e.getY())) {
if (balloons.get(i).getColor() == GREEN_BALLOON) {
score++;
} else {
score--;
}
balloons.remove(i);
break;
}
}
}
});
}
public void start() {
Thread thread = new Thread(this);
thread.start();
}
public void stop() {
running = false;
}
@Override
public void run() {
long startTime = System.currentTimeMillis();
Random random = new Random();
double timeSinceLastBalloon = 0.0;
while (running) {
long currentTime = System.currentTimeMillis();
double deltaTime = (currentTime - startTime) / 1000.0;
double speed = deltaTime / 10.0 + 1.0;
double timeBetweenBalloons = random.nextDouble() * (MAX_TIME - MIN_TIME) / speed;
if (timeSinceLastBalloon >= timeBetweenBalloons) {
int radius = random.nextInt(MAX_RADIUS - MIN_RADIUS + 1) + MIN_RADIUS;
int x = random.nextInt(WIDTH - 2 * radius) + radius;
int y = -radius;
int color = random.nextInt(2);
int velocity = random.nextInt(MAX_SPEED + (int)(speed * 50)) + 1;
balloons.add(new Balloon(x, y, radius, color, velocity));
timeSinceLastBalloon = 0.0;
} else {
timeSinceLastBalloon += deltaTime;
}
for (int i = 0; i < balloons.size(); i++) {
balloons.get(i).move(deltaTime);
if (balloons.get(i).getY() > HEIGHT + balloons.get(i).getRadius()) {
balloons.remove(i);
i--;
}
}
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
for (int i = 0; i < balloons.size(); i++) {
balloons.get(i).draw(g);
}
g.setColor(Color.WHITE);
g.drawString("Score: " + score, 10, 20);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Balloon Pop");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
BalloonPop game = new BalloonPop();
frame.add(game);
frame.setVisible(true);
game.start();
}
}
class Balloon {
private int x;
private int y;
private int radius;
private int color;
private int velocity;
public Balloon(int x, int y, int radius, int color, int velocity) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
this.velocity = velocity;
}
public void move(double deltaTime) {
y += (int)(velocity * deltaTime);
}
public boolean contains(int x, int y) {
int dx = this.x - x;
int dy = this.y - y;
return dx * dx + dy * dy <= radius * radius;
}
public void draw(Graphics g) {
if (color == BalloonPop.GREEN_BALLOON) {
g.setColor(Color.GREEN);
} else {
g.setColor(Color.RED);
}
g.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getRadius() {
return radius;
}
public int getColor() {
return color;
}
public int getVelocity() {
return velocity;
}
}
```
This code creates a JPanel that represents the Balloon Pop game. The game window is 500x500 pixels with a black background. Balloons have a random radius of between 20 and 40 pixels and are either green or red. Balloons start with their center at a random x position between their radius and the width of the window minus the radius and a y position of minus the radius. The speed of the game is determined by a variable speed calculated as follows: speed = (time since game has started in seconds) / 10.0 + 1.0. Balloons have a constant random y velocity between 0 and 100 + (speed * 50) pixels per second. The time (in seconds) between balloons appearing is a random double precision number between 0 and 2 divided by the speed.
The game logic is implemented in the `run()` method, which generates new balloons, moves existing balloons, updates the score, and repaints the screen. The `Balloon` class represents a single balloon and contains its position, size, color, and velocity. The `contains()` method is used to check if the mouse click is inside a balloon. The `paintComponent()` method is used to draw the balloons and the score on the screen. The `main()` method creates a JFrame and adds the BalloonPop panel to it, then starts the game.
I hope that helps! Let me know if you have any further questions.