java如何编写超级玛丽,用main启动窗口在宽1400,高1000
1条回答 默认 最新
关注
完整代码如下,你试试看:(如若有用,还请题主采纳!)
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class SuperMario extends JPanel implements ActionListener { private Timer timer; public SuperMario() { // 设置定时器,每隔 20 毫秒调用一次 actionPerformed 方法 timer = new Timer(20, this); timer.start(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); // 在这里绘制游戏元素,例如背景、角色等 g.setColor(Color.CYAN); g.fillRect(0, 0, getWidth(), getHeight()); // 示例:绘制一个简单的玛丽角色 g.setColor(Color.RED); g.fillRect(100, 800, 50, 50); } @Override public void actionPerformed(ActionEvent e) { // 更新游戏状态,例如角色移动、碰撞检测等 repaint(); } public static void main(String[] args) { JFrame frame = new JFrame("超级玛丽"); SuperMario gamePanel = new SuperMario(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(1400, 1000); frame.add(gamePanel); frame.setVisible(true); frame.setResizable(false); } }
解决评论 打赏 举报无用 1