//用于存储所有的背景
private List<BackGround> allBg = new ArrayList<>();
//用于存储当前的背景
private BackGround nowBg = new BackGround();
//用于双缓存
private Image offScreenImage = null;
//马里奥对象
private Mario mario = new Mario();
//定义一个线程对象,用于实现马里奥的运动
private Thread thread = new Thread(this);
public MyFrame() {
//设置窗口的大小为800 * 600
this.setSize(800, 600);
//设置窗口居中显示
this.setLocationRelativeTo(null);
//设置窗口的可见性
this.setVisible(true);
//设置点击窗口上的关闭键,结束程序
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置窗口大小不可变
this.setResizable(false);
//向窗口对象添加键盘监听器
this.addKeyListener(this);
//设置窗口名称
this.setTitle("超级玛丽");
//初始化图片
StaticValue.init();
//初始化马里奥
mario = new Mario(10, 355);
//创建全部的场景
for (int i = 1; i <= 3; i++) {
allBg.add(new BackGround(i, i == 3 ? true : false));
}
//将第一个场景设置为当前场景
nowBg = allBg.get(0);
mario.setBackGround(nowBg);
//绘制图像
repaint();
thread.start();
try {
new Music();
} catch (Exception e) {
e.printStackTrace();
}
}
具体的运行截图
具体的代码如下:https://gitee.com/Marsfactory/source-byte