用java写一个飞翔的小鸟的游戏
时间: 2023-12-18 08:05:18 浏览: 31
好的,我可以给您提供一个基本的游戏框架,您可以在这个框架的基础上编写游戏逻辑。
1. 首先,您需要创建一个窗口,可以使用Java的Swing库来实现:
```
import javax.swing.JFrame;
public class BirdGame extends JFrame {
public BirdGame() {
setTitle("Flappy Bird");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setVisible(true);
}
public static void main(String[] args) {
new BirdGame();
}
}
```
2. 接下来,您需要创建一个小鸟的类,这个小鸟可以飞翔并且可以受到重力的影响。您可以使用Java的Graphics2D库来绘制小鸟。
```
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
public class Bird {
private double x, y; //小鸟的坐标
private double speed; //小鸟的速度
public Bird(double x, double y) {
this.x = x;
this.y = y;
speed = 0;
}
public void update() {
// 更新小鸟的位置和速度
y += speed;
speed += 0.5;
}
public void jump() {
// 小鸟跳跃
speed = -10;
}
public void draw(Graphics2D g2) {
// 绘制小鸟
g2.setColor(Color.RED);
g2.fill(new Ellipse2D.Double(x, y, 20, 20));
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getWidth() {
return 20;
}
public double getHeight() {
return 20;
}
}
```
3. 然后,您需要创建一个管道的类,这个管道可以从屏幕的右侧飞来,并且可以改变高度。
```
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.util.Random;
public class Pipe {
private double x, y; // 管道的坐标
private double width, height; // 管道的宽度和高度
public Pipe(double x, double y) {
this.x = x;
this.y = y;
width = 50;
height = new Random().nextInt(200) + 50;
}
public void update() {
// 更新管道的位置
x -= 5;
}
public void draw(Graphics2D g2) {
// 绘制管道
g2.setColor(Color.GREEN);
g2.fill(new Rectangle2D.Double(x, y, width, height));
g2.fill(new Rectangle2D.Double(x, y + height + 100, width, 500));
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
}
```
4. 最后,您需要将小鸟和管道的绘制放到一个循环中,同时检测小鸟和管道是否发生碰撞,如果碰撞则游戏结束。
```
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class BirdGame extends JFrame {
private Bird bird;
private ArrayList<Pipe> pipes;
public BirdGame() {
setTitle("Flappy Bird");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
bird = new Bird(100, 300);
pipes = new ArrayList<Pipe>();
pipes.add(new Pipe(800, 0));
JPanel panel = new JPanel() {
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
bird.draw(g2);
for (Pipe pipe : pipes) {
pipe.draw(g2);
}
}
};
panel.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
bird.jump();
}
}
@Override
public void keyReleased(KeyEvent e) {}
});
add(panel);
setVisible(true);
Timer timer = new Timer(50, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
bird.update();
for (Pipe pipe : pipes) {
pipe.update();
}
if (pipes.get(0).getX() < -50) {
pipes.remove(0);
pipes.add(new Pipe(800, 0));
}
if (bird.getY() > 580) {
System.out.println("Game Over!");
System.exit(0);
}
for (Pipe pipe : pipes) {
if (bird.getX() + bird.getWidth() > pipe.getX()
&& bird.getX() < pipe.getX() + pipe.getWidth()
&& (bird.getY() < pipe.getHeight()
|| bird.getY() + bird.getHeight() > pipe.getHeight() + 100)) {
System.out.println("Game Over!");
System.exit(0);
}
}
panel.repaint();
}
});
timer.start();
}
public static void main(String[] args) {
new BirdGame();
}
}
```
希望这个代码框架可以帮助到您,您可以在此基础上进行扩展和优化。