帮我用java写一个飞翔的小鸟游戏并且要有登陆界面
时间: 2024-03-11 11:48:40 浏览: 120
好的,下面是一个简单的用Java实现的飞翔小鸟游戏,包括登陆界面和游戏界面:
登陆界面(LoginForm.java):
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LoginForm extends JFrame implements ActionListener {
private JLabel titleLabel, usernameLabel, passwordLabel;
private JTextField usernameField;
private JPasswordField passwordField;
private JButton loginButton, exitButton;
public LoginForm() {
super("飞翔小鸟登陆");
setLayout(new GridLayout(4, 2, 5, 5));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
titleLabel = new JLabel("飞翔小鸟登陆", JLabel.CENTER);
titleLabel.setFont(new Font("宋体", Font.BOLD, 24));
add(titleLabel);
usernameLabel = new JLabel("用户名:", JLabel.RIGHT);
add(usernameLabel);
usernameField = new JTextField();
add(usernameField);
passwordLabel = new JLabel("密码:", JLabel.RIGHT);
add(passwordLabel);
passwordField = new JPasswordField();
add(passwordField);
loginButton = new JButton("登陆");
loginButton.addActionListener(this);
add(loginButton);
exitButton = new JButton("退出");
exitButton.addActionListener(this);
add(exitButton);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if (username.equals("admin") && password.equals("123456")) {
new BirdGame();
dispose();
} else {
JOptionPane.showMessageDialog(this, "用户名或密码错误!", "错误", JOptionPane.ERROR_MESSAGE);
}
} else if (e.getSource() == exitButton) {
System.exit(0);
}
}
public static void main(String[] args) {
new LoginForm();
}
}
```
游戏界面(BirdGame.java):
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class BirdGame extends JFrame implements ActionListener {
private JPanel gamePanel;
private JLabel scoreLabel;
private Timer timer;
private Random rand;
private int score;
private boolean gameOver;
private Bird bird;
private Obstacle obstacle1, obstacle2;
public BirdGame() {
super("飞翔小鸟");
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gamePanel = new JPanel();
gamePanel.setLayout(null);
gamePanel.setPreferredSize(new Dimension(400, 600));
gamePanel.setBackground(Color.WHITE);
add(gamePanel, BorderLayout.CENTER);
bird = new Bird();
gamePanel.add(bird);
obstacle1 = new Obstacle(400, 0, 80, rand.nextInt(320) + 80);
gamePanel.add(obstacle1);
obstacle2 = new Obstacle(600, 0, 80, rand.nextInt(320) + 80);
gamePanel.add(obstacle2);
scoreLabel = new JLabel("得分:0", JLabel.CENTER);
scoreLabel.setFont(new Font("宋体", Font.BOLD, 20));
add(scoreLabel, BorderLayout.NORTH);
timer = new Timer(30, this);
rand = new Random();
score = 0;
gameOver = false;
timer.start();
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == timer) {
if (!gameOver) {
bird.move();
obstacle1.move();
obstacle2.move();
if (bird.getRectangle().intersects(obstacle1.getRectangle())
|| bird.getRectangle().intersects(obstacle2.getRectangle())
|| bird.getY() < 0
|| bird.getY() + bird.getHeight() > gamePanel.getHeight()) {
gameOver = true;
JOptionPane.showMessageDialog(this, "游戏结束,得分:" + score, "提示", JOptionPane.INFORMATION_MESSAGE);
} else {
if (obstacle1.getX() + obstacle1.getWidth() < 0) {
obstacle1.setX(400);
obstacle1.setHeight(rand.nextInt(320) + 80);
score++;
scoreLabel.setText("得分:" + score);
}
if (obstacle2.getX() + obstacle2.getWidth() < 0) {
obstacle2.setX(400);
obstacle2.setHeight(rand.nextInt(320) + 80);
score++;
scoreLabel.setText("得分:" + score);
}
}
}
}
}
private class Bird extends JLabel {
private int x, y, width, height;
private int speed;
public Bird() {
super(new ImageIcon("bird.png"));
x = 50;
y = 200;
width = 50;
height = 40;
speed = 0;
setBounds(x, y, width, height);
}
public void move() {
speed += 1;
y += speed;
setBounds(x, y, width, height);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public Rectangle getRectangle() {
return new Rectangle(x, y, width, height);
}
}
private class Obstacle extends JLabel {
private int x, y, width, height;
public Obstacle(int x, int y, int width, int height) {
super(new ImageIcon("obstacle.png"));
this.x = x;
this.y = y;
this.width = width;
this.height = height;
setBounds(x, y, width, height);
}
public void move() {
x -= 5;
setBounds(x, y, width, height);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
setBounds(x, y, width, height);
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public Rectangle getRectangle() {
return new Rectangle(x, y, width, height);
}
}
public static void main(String[] args) {
new LoginForm();
}
}
```
请注意,上述代码中使用了本地的"bird.png"和"obstacle.png"两个图片文件,您需要将其放置在与代码相同的目录下才能正常运行。此外,该代码仅作为示例,还有许多优化和改进的空间,您可以根据自己的需求进行修改和扩展。
阅读全文