package com.arvin;
import javazoom.jl.decoder.JavaLayerException;
import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List;
public class WindowsFrame extends JFrame implements KeyListener,Runnable {
private List<BackGround> AllBackground = new ArrayList<>();
private BackGround NowBackground = new BackGround();
private Image OffScreenImage = null;
private Mario mario = new Mario();
private Thread thread = new Thread(this);
public WindowsFrame(){
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++) { AllBackground.add(new BackGround(i, i == 3 ? true : false)); }
NowBackground = AllBackground.get(0); mario.setBackGround(NowBackground);
repaint();
thread.start();
try { new Music(); } catch (FileNotFoundException exception) { exception.printStackTrace(); } catch (JavaLayerException exception) { exception.printStackTrace(); }
}
public void paint(Graphics GraphicsPaint) { if (OffScreenImage == null){ OffScreenImage = createImage(800,600); }
Graphics graphics = OffScreenImage.getGraphics(); graphics.fillRect(0,0,800,600); graphics.drawImage(NowBackground.getBgImage(),0,0,this); for (Enemy enemy : NowBackground.getEnemyList()){ graphics.drawImage(enemy.getShow(),enemy.getX(),enemy.getY(),this); }
for (Obstacle obstacle:NowBackground.getObstacleList()) { graphics.drawImage(obstacle.getShow(),obstacle.getX(),obstacle.getY(),this); }
graphics.drawImage(NowBackground.getTower(),620,270,this);
graphics.drawImage(NowBackground.getGan(),500,220,this);
graphics.drawImage(mario.getShow(),mario.getX(),mario.getY(),this);
Color color = graphics.getColor(); graphics.setColor(Color.BLACK); graphics.setFont(new Font("宋体",Font.BOLD,25)); graphics.drawString("当前分数:"+ mario.getScore(),300,100); graphics.setColor(color);
GraphicsPaint.drawImage(OffScreenImage,0,0,this); }
public static void main(String[] args){
WindowsFrame windowsFrame = new WindowsFrame(); }
@Override public void keyTyped(KeyEvent e) {
}
@Override public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 39) { mario.rightMove(); }
if (e.getKeyCode() == 37) { mario.leftMove(); }
if (e.getKeyCode() == 32){ mario.jump(); }
}
@Override public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == 37){ mario.leftStop(); }
if (e.getKeyCode() == 39){ mario.rightStop(); }
}
@Override public void run() { while (true){ repaint(); try { Thread.sleep(50);
if (mario.getX() >= 775){ NowBackground = AllBackground.get(NowBackground.getSort()); mario.setBackGround(NowBackground); mario.setX(10); mario.setY(395); }
if (mario.isDeath()){ JOptionPane.showMessageDialog(this,"马里奥死亡演示结束 - Arvin"); System.exit(0); }
if (mario.isOK()) { JOptionPane.showMessageDialog(this,"Demo演示结束 - Arvin"); System.exit(0); }
} catch (InterruptedException exception) { exception.printStackTrace(); } } } }
|