用Java写一个超级玛丽小游戏代码
时间: 2024-01-28 17:03:57 浏览: 94
下面是一个简单的Java写的超级玛丽小游戏代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MarioGame extends JFrame implements ActionListener {
private JPanel panel;
private JButton startButton, resetButton;
private JLabel scoreLabel;
private int score;
public MarioGame() {
super("Mario Game");
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(null);
startButton = new JButton("Start");
startButton.setBounds(150, 20, 100, 30);
startButton.addActionListener(this);
panel.add(startButton);
resetButton = new JButton("Reset");
resetButton.setBounds(250, 20, 100, 30);
resetButton.addActionListener(this);
panel.add(resetButton);
scoreLabel = new JLabel("Score: 0");
scoreLabel.setBounds(20, 20, 100, 30);
panel.add(scoreLabel);
add(panel);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startButton) {
startGame();
} else if (e.getSource() == resetButton) {
resetGame();
}
}
public void startGame() {
score = 0;
scoreLabel.setText("Score: " + score);
// 开始游戏的代码
}
public void resetGame() {
score = 0;
scoreLabel.setText("Score: " + score);
// 重置游戏的代码
}
public static void main(String[] args) {
new MarioGame();
}
}
```
这只是一个框架,需要自己添加游戏逻辑和实现。
阅读全文