|

楼主 |
发表于 2017-9-15 13:09:59
|
显示全部楼层
1.Background.java
- package org.liky.mario;
- import java.awt.image.BufferedImage;
- import java.util.ArrayList;
- import java.util.List;
- import com.sun.org.apache.bcel.internal.generic.NEW;
- public class BackGround {
- //当前场景的显示图片
- private BufferedImage bgImage = null;
- public BufferedImage getBgImage() {
- return bgImage;
- }
- //场景的顺序
- private int sort;
- public int getSort() {
- return sort;
- }
- //当前是否为最后一个场景
- private boolean flag;
-
- //通过集合来保存
- //全部的敌人
- private List<Enemy> allEnemy = new ArrayList<Enemy>();
- //全部的障碍物
- private List<Obstruction> allObstruction = new ArrayList<Obstruction>();
- public List<Obstruction> getAllObstruction() {
- return allObstruction;
- }
- //被消灭的敌人
- private List<Enemy> removedEnemy = new ArrayList<Enemy>();
- //被消灭的障碍物
- private List<Obstruction> removedObstruction = new ArrayList<Obstruction>();
-
- public List<Obstruction> getRemovedObstruction() {
- return removedObstruction;
- }
- //构造方法
- public BackGround(int sort,boolean flag) {
- this.sort = sort;
- this.flag = flag;
- if (flag) {
- bgImage = StaticValue.endImage;
- }else {
- bgImage = StaticValue.bgImage;
- }
- if (sort == 1) {
- //绘制地面
- for(int i = 0; i < 15;i++) {
- this.allObstruction.add(new Obstruction(i*60, 540, 9));
- }
- //绘制砖块和问号
- this.allObstruction.add(new Obstruction(120, 360, 4));
- this.allObstruction.add(new Obstruction(300, 360, 0));
- this.allObstruction.add(new Obstruction(360, 360, 4));
- this.allObstruction.add(new Obstruction(420, 360, 0));
- this.allObstruction.add(new Obstruction(480, 360, 4));
- this.allObstruction.add(new Obstruction(540, 360, 0));
- this.allObstruction.add(new Obstruction(420, 180, 4));
- //绘制水管
- this.allObstruction.add(new Obstruction(660, 540, 6));
- this.allObstruction.add(new Obstruction(720, 540, 5));
- this.allObstruction.add(new Obstruction(660, 480, 8));
- this.allObstruction.add(new Obstruction(720, 480, 7));
- //绘制敌人
- this.allEnemy.add(new Enemy(600,480,true,1));
- this.allEnemy.add(new Enemy(690,540,true,2,420,540));
- }
- //绘制第二个场景
- if (sort == 2) {
- for(int i = 0; i < 15;i++) {
- if(i != 10) {
- this.allObstruction.add(new Obstruction(i*60, 540, 9));
- }
- }
- //第一根水管
- this.allObstruction.add(new Obstruction(60, 540, 6));
- this.allObstruction.add(new Obstruction(120, 540, 5));
- this.allObstruction.add(new Obstruction(60, 480, 6));
- this.allObstruction.add(new Obstruction(120, 480, 5));
- this.allObstruction.add(new Obstruction(60, 420, 8));
- this.allObstruction.add(new Obstruction(120, 420, 7));
- //第二根水管
- this.allObstruction.add(new Obstruction(240, 540, 6));
- this.allObstruction.add(new Obstruction(300, 540, 5));
- this.allObstruction.add(new Obstruction(240, 480, 6));
- this.allObstruction.add(new Obstruction(300, 480, 5));
- this.allObstruction.add(new Obstruction(240, 420, 6));
- this.allObstruction.add(new Obstruction(300, 420, 5));
- this.allObstruction.add(new Obstruction(240, 360, 8));
- this.allObstruction.add(new Obstruction(300, 360, 7));
-
- }
-
- }
-
- public List<Enemy> getAllEnemy() {
- return allEnemy;
- }
- public List<Enemy> getRemovedEnemy() {
- return removedEnemy;
- }
- //重置方法,将所有的障碍物和敌人返回原有坐标,并将其状态也修改
- public void reset() {
-
-
- }
-
- }
复制代码
2.Enemy.java
3.Mario.java
4.MyFrame.java
- package org.liky.mario;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.image.BufferedImage;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.awt.Graphics;
- import java.awt.Toolkit;
- import javax.swing.JFrame;
- public class MyFrame extends JFrame implements KeyListener,Runnable {
-
- private List<BackGround> allBG = new ArrayList<BackGround>();
-
- private Mario mario = null;
-
- private BackGround nowBG = null;
-
- private Thread t = new Thread(this);
-
- public static void main(String[] args) {
- new MyFrame();
- }
-
- public MyFrame() {
- this.setTitle("马里奥游戏程序");
- this.setSize(900, 600);
- int width = Toolkit.getDefaultToolkit().getScreenSize().width;
- int height = Toolkit.getDefaultToolkit().getScreenSize().height;
- this.setLocation((width - 900)/2, (height - 600)/2);
- this.setResizable(false);
-
- //初始化全部的图片
- StaticValue.init();
-
- //创建全部的场景
- for(int i = 0;i <= 3;i++) {
- this.allBG.add(new BackGround(i, i==3?true:false));
- }
- //将第一个场景设置为当前场景
- this.nowBG = this.allBG.get(1);
- //初始化Mario对象
- this.mario = new Mario(0, 480);
- //将场景放入mario对象场景的属性中
- this.mario.setBg(nowBG);
-
- this.repaint();
-
- this.addKeyListener(this);
-
- t.start();
-
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setVisible(true);
- }
-
-
- public void paint(Graphics g) {
- // TODO Auto-generated method stub
- //建立临时的缓冲图片
- BufferedImage image = new BufferedImage(900, 600, BufferedImage.TYPE_3BYTE_BGR);
- Graphics g2 = image.getGraphics();
- //绘制背景
- g2.drawImage(this.nowBG.getBgImage(), 0, 0, this);
-
- //绘制敌人
-
- Iterator<Enemy> iterEnemy = this.nowBG.getAllEnemy().iterator();
- while(iterEnemy.hasNext()) {
- Enemy e = iterEnemy.next();
- g2.drawImage(e.getShowImage(), e.getX(), e.getY(), this);
- }
-
- //绘制障碍物
- Iterator<Obstruction> iter = this.nowBG.getAllObstruction().iterator();
- while(iter.hasNext()) {
- Obstruction ob = iter.next();
- g2.drawImage(ob.getShowimage(), ob.getX(), ob.getY(), this);
- }
- g2.drawImage(this.mario.getShowImage(), this.mario.getX(), this.mario.getY(), this);
-
- //把缓冲图片绘制到窗体中
- g2.drawImage(image, 0, 0, this);
-
- }
- @Override
- public void keyPressed(KeyEvent ke) {
- // TODO Auto-generated method stub
- //当按下39时(-->),mario向右移动
- if(ke.getKeyCode() == 39) {
- this.mario.rightMove();
- }
- //当按下37时(<--),mario向左移动
- if(ke.getKeyCode() == 37) {
- this.mario.leftMove();
- }
- //当按下32(空格键)时,mario开始跳跃
- if(ke.getKeyCode() == 32) {
- this.mario.jump();
- }
-
- System.out.println(ke.getKeyCode());
- }
- @Override
- public void keyReleased(KeyEvent ke) {
- // TODO Auto-generated method stub
- //当抬起39时(-->),mario停止向右移动
- if(ke.getKeyCode() == 39) {
- this.mario.rightStop();
- }
- //当抬起37时(<--),mario停止向左移动
- if(ke.getKeyCode() == 37) {
- this.mario.leftStop();
- }
- System.out.println(ke.getKeyCode());
- }
-
- @Override
- public void keyTyped(KeyEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- while(true) {
- this.repaint();
- try {
- Thread.sleep(50);
- if(this.mario.getX() >= 840) {
- //切换场景
- this.nowBG = this.allBG.get(this.nowBG.getSort()+1);
- //将当前场景设置mario中
- this.mario.setBg(this.nowBG);
- //修改mario的坐标
- this.mario.setX(0);
- }
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
复制代码
5.Obstruction.java
- package org.liky.mario;
- import java.awt.image.BufferedImage;
- public class Obstruction {
- // 保存的坐标
- private int x;
- private int y;
-
- public int getX() {
- return x;
- }
- public int getY() {
- return y;
- }
- //类型
- private int type;
- public void setType(int type) {
- this.type = type;
- }
- public int getType() {
- return type;
- }
- //初始的类型
- private int startType;
- //显示图片
- private BufferedImage showimage = null;
-
- //构造方法
-
- public BufferedImage getShowimage() {
- return showimage;
- }
- public Obstruction(int x,int y,int type) {
- this.x = x;
- this.y = y;
- this.type = type;
- setImage();
-
- }
-
- //重置方法
- public void reset() {
- //修改类型为初始类型
- this.type = startType;
- //改变显示图片
- this.setImage();
- }
- //根据类型改变显示图片
- public void setImage() {
- showimage = StaticValue.allObstructionImage.get(type);
- }
-
- }
复制代码
6.StaticValue.java
- package org.liky.mario;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import javax.imageio.ImageIO;
- import com.sun.org.apache.bcel.internal.generic.IfInstruction;
- public class StaticValue {
-
- public static List<BufferedImage> allMarioImage = new ArrayList<BufferedImage>();
-
- public static BufferedImage startImage = null;
-
- public static BufferedImage endImage = null;
-
- public static BufferedImage bgImage = null;
-
- public static List<BufferedImage> allFlowerImage = new ArrayList<BufferedImage>();
-
- public static List<BufferedImage> allTriangleImage = new ArrayList<BufferedImage>();
-
- public static List<BufferedImage> allTurtleImage = new ArrayList<BufferedImage>();
-
- public static List<BufferedImage> allObstructionImage = new ArrayList<BufferedImage>();
-
- public static BufferedImage marioDeadImage = null;
-
- public static String imagePath = System.getProperty("user.dir")+"/bin/";
-
- //将全部图片初始化
- public static void init() {
- //将所有mario的图片保存到静态属性中
- for(int i = 1;i <= 10; i++) {
- try {
- allMarioImage.add(ImageIO.read(new File(imagePath + i + ".gif")));
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- //导入全部背景图片
- try {
- startImage = ImageIO.read(new File(imagePath + "start.gif"));
- bgImage = ImageIO.read(new File(imagePath + "firststage.gif"));
- endImage = ImageIO.read(new File(imagePath + "firststageend.gif"));
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- //导入所有敌人的图片
- for (int i = 1;i <= 5; i++) {
- try {
- if (i <= 2) {
- allFlowerImage.add(ImageIO.read(new File(imagePath + "flower"+i+".gif")));
- }
- if (i <=3) {
- allTriangleImage.add(ImageIO.read(new File(imagePath + "triangle"+i+".gif")));
- }
- allTurtleImage.add(ImageIO.read(new File(imagePath + "Turtle"+i+".gif")));
- } catch (IOException e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- //导入障碍物的图片
- for(int i = 1;i <= 12;i++) {
- try {
- allObstructionImage.add(ImageIO.read(new File(imagePath + "ob"+i+".gif")));
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- }
-
- //导入Mario死亡的图片
- try {
- marioDeadImage = ImageIO.read(new File(imagePath + "over.gif"));
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
-
- }
- }
复制代码 |
|