鱼C论坛

 找回密码
 立即注册
查看: 3231|回复: 3

为毛我的游戏突然变成白板了=。=

[复制链接]
发表于 2017-9-15 13:06:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
还是之前跟着教程一点一点打代码的那个马里奥游戏,今天运行后突然变成了白框,什么图片什么内容都没有了

不知道到底哪一步出现了问题,也没有看到eclipse报错 MLDN Mario.zip (557.3 KB, 下载次数: 6)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-9-15 13:09:59 | 显示全部楼层
1.Background.java
  1. package org.liky.mario;

  2. import java.awt.image.BufferedImage;
  3. import java.util.ArrayList;
  4. import java.util.List;

  5. import com.sun.org.apache.bcel.internal.generic.NEW;

  6. public class BackGround {
  7.         //当前场景的显示图片
  8.         private BufferedImage bgImage = null;
  9.         public BufferedImage getBgImage() {
  10.                 return bgImage;
  11.         }

  12.         //场景的顺序
  13.         private int sort;
  14.         public int getSort() {
  15.                 return sort;
  16.         }

  17.         //当前是否为最后一个场景
  18.         private boolean flag;
  19.        
  20.         //通过集合来保存
  21.         //全部的敌人
  22.         private List<Enemy> allEnemy = new ArrayList<Enemy>();
  23.         //全部的障碍物
  24.         private List<Obstruction> allObstruction = new ArrayList<Obstruction>();

  25.         public List<Obstruction> getAllObstruction() {
  26.                 return allObstruction;
  27.         }

  28.         //被消灭的敌人
  29.         private List<Enemy> removedEnemy = new ArrayList<Enemy>();
  30.         //被消灭的障碍物
  31.         private List<Obstruction> removedObstruction = new ArrayList<Obstruction>();
  32.        
  33.         public List<Obstruction> getRemovedObstruction() {
  34.                 return removedObstruction;
  35.         }

  36.         //构造方法
  37.         public BackGround(int sort,boolean flag) {
  38.                 this.sort = sort;
  39.                 this.flag = flag;
  40.                 if (flag) {
  41.                         bgImage = StaticValue.endImage;                       
  42.                 }else {
  43.                         bgImage = StaticValue.bgImage;       
  44.                 }
  45.                 if (sort == 1) {
  46.                         //绘制地面
  47.                         for(int i = 0; i < 15;i++) {
  48.                                 this.allObstruction.add(new Obstruction(i*60, 540, 9));
  49.                         }
  50.                         //绘制砖块和问号
  51.                         this.allObstruction.add(new Obstruction(120, 360, 4));
  52.                         this.allObstruction.add(new Obstruction(300, 360, 0));
  53.                         this.allObstruction.add(new Obstruction(360, 360, 4));
  54.                         this.allObstruction.add(new Obstruction(420, 360, 0));
  55.                         this.allObstruction.add(new Obstruction(480, 360, 4));
  56.                         this.allObstruction.add(new Obstruction(540, 360, 0));
  57.                         this.allObstruction.add(new Obstruction(420, 180, 4));
  58.                         //绘制水管
  59.                         this.allObstruction.add(new Obstruction(660, 540, 6));
  60.                         this.allObstruction.add(new Obstruction(720, 540, 5));
  61.                         this.allObstruction.add(new Obstruction(660, 480, 8));
  62.                         this.allObstruction.add(new Obstruction(720, 480, 7));
  63.                         //绘制敌人
  64.                         this.allEnemy.add(new Enemy(600,480,true,1));
  65.                         this.allEnemy.add(new Enemy(690,540,true,2,420,540));
  66.                 }
  67.                 //绘制第二个场景
  68.                 if (sort == 2) {
  69.                         for(int i = 0; i < 15;i++) {
  70.                                 if(i != 10) {
  71.                                         this.allObstruction.add(new Obstruction(i*60, 540, 9));
  72.                                 }
  73.                         }
  74.                         //第一根水管
  75.                         this.allObstruction.add(new Obstruction(60, 540, 6));
  76.                         this.allObstruction.add(new Obstruction(120, 540, 5));
  77.                         this.allObstruction.add(new Obstruction(60, 480, 6));
  78.                         this.allObstruction.add(new Obstruction(120, 480, 5));
  79.                         this.allObstruction.add(new Obstruction(60, 420, 8));
  80.                         this.allObstruction.add(new Obstruction(120, 420, 7));
  81.                         //第二根水管
  82.                         this.allObstruction.add(new Obstruction(240, 540, 6));
  83.                         this.allObstruction.add(new Obstruction(300, 540, 5));
  84.                         this.allObstruction.add(new Obstruction(240, 480, 6));
  85.                         this.allObstruction.add(new Obstruction(300, 480, 5));
  86.                         this.allObstruction.add(new Obstruction(240, 420, 6));
  87.                         this.allObstruction.add(new Obstruction(300, 420, 5));
  88.                         this.allObstruction.add(new Obstruction(240, 360, 8));
  89.                         this.allObstruction.add(new Obstruction(300, 360, 7));
  90.                        
  91.                 }
  92.                
  93.         }
  94.        
  95.         public List<Enemy> getAllEnemy() {
  96.                 return allEnemy;
  97.         }

  98.         public List<Enemy> getRemovedEnemy() {
  99.                 return removedEnemy;
  100.         }

  101.         //重置方法,将所有的障碍物和敌人返回原有坐标,并将其状态也修改
  102.         public void reset() {
  103.                
  104.                
  105.         }
  106.        
  107. }
复制代码


2.Enemy.java
  1. package org.liky.mario;

  2. import java.awt.image.BufferedImage;

  3. public class Enemy implements Runnable {
  4.         //坐标
  5.         private int x;
  6.         private int y;
  7.         //初始坐标
  8.         private int startX;
  9.         private int startY;
  10.         //类型
  11.         private int type;
  12.         public int getType() {
  13.                 return type;
  14.         }
  15.         public void setType(int type) {
  16.                 this.type = type;
  17.         }
  18.         public int getX() {
  19.                 return x;
  20.         }
  21.         public int getY() {
  22.                 return y;
  23.         }
  24.         public BufferedImage getShowImage() {
  25.                 return showImage;
  26.         }

  27.         //显示图片
  28.         private BufferedImage showImage;
  29.         //移动方向
  30.         private boolean isLeftOrUp = true;
  31.         //移动的范围
  32.         private int upMax = 0;
  33.         private int downMax = 0;
  34.         //定义线程
  35.         private Thread t = new Thread(this);
  36.         //定义图片状态
  37.         private int imageType = 0;
  38.         //创建普通敌人
  39.         public Enemy(int x,int y,boolean isLeft,int type) {
  40.                 this.x = x;
  41.                 this.y = y;
  42.                 this.isLeftOrUp = isLeft;
  43.                 this.type = type;
  44.                 if (type == 1) {
  45.                         this.showImage = StaticValue.allTriangleImage.get(0);
  46.                 }
  47.                 t.start();
  48.         }
  49.         //创建食人花
  50.         public Enemy(int x,int y,boolean isUp,int type,int upMax,int downMax) {
  51.                 this.x = x;
  52.                 this.y = y;
  53.                 this.isLeftOrUp = isUp;
  54.                 this.type = type;
  55.                 this.upMax = upMax;
  56.                 this.downMax = downMax;
  57.                
  58.                 if (type == 2) {
  59.                         this.showImage = StaticValue.allFlowerImage.get(0);
  60.                 }
  61.                 t.start();
  62.         }
  63.                        
  64.        
  65.        
  66.                        
  67.         @Override
  68.         public void run() {
  69.                 // TODO Auto-generated method stub
  70.                 while(true) {
  71.                         //对于不同的类型要进行不同的处理
  72.                         if(type == 1) {
  73.                                 if (this.isLeftOrUp) {
  74.                                         this.x -= 2;
  75.                                 }else {
  76.                                         this.x += 2;
  77.                                 }
  78.                                 if (imageType == 0) {
  79.                                         imageType = 1;
  80.                                 }else {
  81.                                         imageType = 0;
  82.                                 }
  83.                                         this.showImage = StaticValue.allTriangleImage.get(imageType);
  84.                         }
  85.                         if(type == 2) {
  86.                                 if (this.isLeftOrUp) {
  87.                                         this.y -= 2;
  88.                                 }else {
  89.                                         this.y += 2;
  90.                                 }
  91.                                 if (imageType == 0) {
  92.                                         imageType = 1;
  93.                                 }else {
  94.                                         imageType = 0;
  95.                                 }
  96.                                 if(this.isLeftOrUp && this.x == this.upMax) {
  97.                                         this.isLeftOrUp = false;
  98.                                 }
  99.                                 if(!this.isLeftOrUp && this.x == this.downMax) {
  100.                                         this.isLeftOrUp = true;
  101.                                 }
  102.                                
  103.                                 this.showImage = StaticValue.allFlowerImage.get(imageType);
  104.                        
  105.                         }
  106.                         try {
  107.                                 Thread.sleep(50);
  108.                         } catch (InterruptedException e) {
  109.                                 // TODO Auto-generated catch block
  110.                                 e.printStackTrace();
  111.                         }

  112.                 }
  113.                
  114.         }
  115.        
  116.         public void reset() {
  117.                
  118.         }
  119.        
  120.         public void dead() {
  121.                
  122.         }
  123. }
复制代码


3.Mario.java
  1. package org.liky.mario;

  2. import java.awt.image.BufferedImage;

  3. import org.omg.PortableServer.THREAD_POLICY_ID;

  4. import com.sun.org.apache.xalan.internal.xsltc.compiler.Template;

  5. public class Mario implements Runnable{
  6.         //坐标
  7.         private int x;
  8.        
  9.         public void setY(int y) {
  10.                 this.y = y;
  11.         }


  12.         public void setX(int x) {
  13.                 this.x = x;
  14.         }


  15.         private int y;
  16.        
  17.         //定义一个场景对象,来保存Mario所在的场景
  18.         private BackGround bg ;
  19.        
  20.         public void setBg(BackGround bg) {
  21.                 this.bg = bg;
  22.         }


  23.         //加入线程
  24.         private Thread t = null;
  25.        
  26.         //定义速度属性
  27.         private int xmove = 0;       
  28.         //定义一个垂直方向的速度
  29.         private int ymove = 0;       
  30.        
  31.         //状态
  32.         private String status;
  33.         //显示的图片
  34.         private BufferedImage showImage;
  35.         //生命数和分数
  36.         private int score;
  37.         private int life;
  38.        
  39.         //当前移动中显示的图片索引
  40.         private int moving = 0;
  41.        
  42.         //上升的时间
  43.         private int upTime = 0;
  44.        
  45.         //构造方法
  46.         public Mario(int x,int y) {
  47.                 this.x = x;
  48.                 this.y = y;
  49.                 //初始化Mario图片
  50.                 this.showImage = StaticValue.allMarioImage.get(0);
  51.                 this.score = 0;
  52.                 this.life = 3;
  53.                
  54.                 t = new Thread(this);
  55.                
  56.                 t.start();
  57.                
  58.                 this.status  = "right--standing";
  59.                
  60.                
  61.         }
  62.        
  63.        
  64.         public int getX() {
  65.                 return x;
  66.         }


  67.         public int getY() {
  68.                 return y;
  69.         }


  70.         public BufferedImage getShowImage() {
  71.                 return showImage;
  72.         }


  73.         public void leftMove() {
  74.                 //改变速度
  75.                 xmove = -5;
  76.                 //改变状态
  77.                 //如果当前已经是跳跃的状态,应该保留原有状态,而不能修改为移动状态
  78.                 if(this.status.indexOf("jumping") != -1) {
  79.                         this.status = "left--jumping";
  80.                 }else {
  81.                         this.status = "left--moving";
  82.                 }               
  83.         }
  84.        
  85.         public void rightMove() {
  86.                 xmove = 5;
  87.                 if(this.status.indexOf("jumping") != -1) {
  88.                         this.status = "right--jumping";
  89.                 }else {
  90.                         this.status = "right--moving";
  91.                 }               
  92.         }
  93.        
  94.         public void leftStop() {
  95.                 this.xmove = 0;
  96.                 if(this.status.indexOf("jumping") != -1) {
  97.                         this.status = "left--jumping";
  98.                 }else {
  99.                         this.status = "left--standing";
  100.                 }       
  101.         }
  102.        
  103.         public void rightStop() {
  104.                 this.xmove = 0;
  105.                 if(this.status.indexOf("jumping") != -1) {
  106.                         this.status = "right--jumping";
  107.                 }else {
  108.                         this.status = "right--standing";
  109.                 }       
  110.         }
  111.        
  112.         public void jump() {
  113.                 if (this.status.indexOf("jumping") == -1) {
  114.                         if (this.status.indexOf("left") != -1) {
  115.                                 this.status = "left--jumping";
  116.                         }else {
  117.                                 this.status = "right--jumping";
  118.                         }
  119.                         ymove = -5;
  120.                         upTime = 36;
  121.                 }
  122.         }
  123.         //加入下落的方法
  124.         public void down() {               
  125.                 if (this.status.indexOf("left") != -1) {
  126.                         this.status = "left--jumping";
  127.                 }else {
  128.                         this.status = "right--jumping";
  129.                 }
  130.                 ymove = 5;               
  131.         }

  132.         @Override
  133.         public void run() {
  134.                 // TODO Auto-generated method stub
  135.                 while(true) {
  136.                         //判断当前mario是否与障碍物碰撞
  137.                         //定义标记
  138.                         boolean canLeft = true;
  139.                         boolean canRight = true;
  140.                         //定义一个当前Mario是否处在障碍物上的标记
  141.                         boolean onLand = false;
  142.                         for (int i = 0; i < this.bg.getAllObstruction().size();i++) {
  143.                                 Obstruction ob = this.bg.getAllObstruction().get(i);
  144.                                 //不允许继续向右移动
  145.                                 if (ob.getX() == this.x + 60 && (ob.getY() + 50 > this.y && ob.getY() - 50 < this.y)) {
  146.                                         canRight = false;
  147.                                 }
  148.                                 //不允许继续向左移动               
  149.                                 if (ob.getX() == this.x - 60 && (ob.getY() + 50 > this.y && ob.getY() - 50 < this.y)) {
  150.                                         canLeft = false;
  151.                                 }
  152.                                 //如果符合条件,则表示Mario当前处在一个障碍物上
  153.                                 if (ob.getY() == this.y + 60 && (ob.getX() + 60 > this.x && ob.getX() - 60 < this.x)) {
  154.                                         onLand = true;
  155.                                 }
  156.                                 //判断当前mario是否在跳跃中顶到了一个障碍物
  157.                                 if (ob.getY() == this.y - 60 && (ob.getX() + 50 > this.x && ob.getX() - 50 < this.x)) {
  158.                                         //对于砖块的处理                                       
  159.                                         if (ob.getType()==0) {
  160.                                                 //将该砖块从场景中移除
  161.                                                 this.bg.getAllObstruction().remove(ob);
  162.                                                 //将被移除的砖块保存到一个集合中
  163.                                                 this.bg.getRemovedObstruction().add(ob);
  164.                                                
  165.                                         }
  166.                                         //对于?的处理
  167.                                         if (ob.getType()==4) {
  168.                                                 ob.setType(2);
  169.                                                 ob.setImage();
  170.                                         }
  171.                                         upTime = 0;
  172.                                        
  173.                                 }
  174.                                
  175.                         }
  176.                        
  177.                         if(onLand && upTime == 0) {
  178.                                 if (this.status.indexOf("left") != -1) {
  179.                                                 if(xmove != 0) {
  180.                                                         this.status = "left--moving";
  181.                                                 }else {
  182.                                                         this.status = "left--standing";
  183.                                                 }
  184.                                 }else {
  185.                                         if(xmove != 0) {
  186.                                                 this.status = "right--moving";
  187.                                         }else {
  188.                                                 this.status = "right--standing";
  189.                                         }
  190.                                 }
  191.                         }else {
  192.                                 //表示当前为上升的状态
  193.                                 if (upTime != 0) {
  194.                                         upTime --;
  195.                                 }else {
  196.                                         this.down();
  197.                                 }
  198.                                 y += ymove;
  199.                         }
  200.                        
  201.                        
  202.                         if (canLeft && xmove < 0 || (canRight && xmove > 0)) {               
  203.                                 //改变坐标
  204.                                 x += xmove;
  205.                                 if (x < 0) {
  206.                                         x = 0;
  207.                                 }
  208.                         }

  209.                         //定义一个图片取得的初始索引数
  210.                         int temp = 0;
  211.                         //当前为面向左
  212.                         if(this.status.indexOf("left") != -1) {
  213.                                 temp += 5;
  214.                         }
  215.                        
  216.                         //判断当前是否为移动
  217.                         if(this.status.indexOf("moving") != -1) {
  218.                                 temp += this.moving;
  219.                                 moving++;
  220.                                 if (moving == 4) {
  221.                                         moving = 0;
  222.                                 }
  223.                         }
  224.                         if (this.status.indexOf("jumping") != -1) {
  225.                                 temp += 4;
  226.                         }
  227.                        
  228.                        
  229.                         //改变显示图片
  230.                         this.showImage = StaticValue.allMarioImage.get(temp);
  231.                        
  232.                        
  233.                         try {
  234.                                 Thread.sleep(50);
  235.                         } catch (InterruptedException e) {
  236.                                 // TODO Auto-generated catch block
  237.                                 e.printStackTrace();
  238.                         }
  239.                 }
  240.         }
  241. }
复制代码


4.MyFrame.java
  1. package org.liky.mario;

  2. import java.awt.event.KeyEvent;
  3. import java.awt.event.KeyListener;
  4. import java.awt.image.BufferedImage;
  5. import java.util.ArrayList;
  6. import java.util.Iterator;
  7. import java.util.List;
  8. import java.awt.Graphics;
  9. import java.awt.Toolkit;

  10. import javax.swing.JFrame;


  11. public class MyFrame extends JFrame implements KeyListener,Runnable {
  12.        
  13.         private List<BackGround> allBG = new ArrayList<BackGround>();
  14.        
  15.         private Mario mario = null;
  16.        
  17.         private BackGround nowBG = null;
  18.        
  19.         private Thread t = new Thread(this);
  20.        
  21.         public static void main(String[] args) {
  22.                 new MyFrame();
  23.         }
  24.        
  25.         public MyFrame() {
  26.                 this.setTitle("马里奥游戏程序");
  27.                 this.setSize(900, 600);
  28.                 int width = Toolkit.getDefaultToolkit().getScreenSize().width;
  29.                 int height = Toolkit.getDefaultToolkit().getScreenSize().height;
  30.                 this.setLocation((width - 900)/2, (height - 600)/2);
  31.                 this.setResizable(false);
  32.                
  33.                 //初始化全部的图片
  34.                 StaticValue.init();
  35.                
  36.                 //创建全部的场景
  37.                 for(int i = 0;i <= 3;i++) {
  38.                         this.allBG.add(new BackGround(i, i==3?true:false));
  39.                 }
  40.                 //将第一个场景设置为当前场景
  41.                 this.nowBG = this.allBG.get(1);
  42.                 //初始化Mario对象
  43.                 this.mario = new Mario(0, 480);
  44.                 //将场景放入mario对象场景的属性中
  45.                 this.mario.setBg(nowBG);
  46.                
  47.                 this.repaint();
  48.                
  49.                 this.addKeyListener(this);
  50.                
  51.                 t.start();
  52.                
  53.                 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  54.                 this.setVisible(true);
  55.         }
  56.        
  57.        


  58.         public void paint(Graphics g) {
  59.                 // TODO Auto-generated method stub
  60.                 //建立临时的缓冲图片
  61.                 BufferedImage image = new BufferedImage(900, 600, BufferedImage.TYPE_3BYTE_BGR);
  62.                 Graphics g2 = image.getGraphics();
  63.                 //绘制背景
  64.                 g2.drawImage(this.nowBG.getBgImage(), 0, 0, this);
  65.                
  66.                 //绘制敌人
  67.                
  68.                 Iterator<Enemy> iterEnemy = this.nowBG.getAllEnemy().iterator();
  69.                 while(iterEnemy.hasNext()) {
  70.                         Enemy e = iterEnemy.next();                       
  71.                         g2.drawImage(e.getShowImage(), e.getX(), e.getY(), this);
  72.                 }
  73.                
  74.                 //绘制障碍物
  75.                 Iterator<Obstruction> iter = this.nowBG.getAllObstruction().iterator();
  76.                 while(iter.hasNext()) {
  77.                         Obstruction ob = iter.next();
  78.                         g2.drawImage(ob.getShowimage(), ob.getX(), ob.getY(), this);
  79.                 }
  80.                 g2.drawImage(this.mario.getShowImage(), this.mario.getX(), this.mario.getY(), this);

  81.                
  82.                 //把缓冲图片绘制到窗体中
  83.                 g2.drawImage(image, 0, 0, this);
  84.                
  85.         }

  86.         @Override
  87.         public void keyPressed(KeyEvent ke) {
  88.                 // TODO Auto-generated method stub
  89.                 //当按下39时(-->),mario向右移动
  90.                 if(ke.getKeyCode() == 39) {
  91.                         this.mario.rightMove();
  92.                 }
  93.                 //当按下37时(<--),mario向左移动
  94.                 if(ke.getKeyCode() == 37) {
  95.                         this.mario.leftMove();
  96.                 }
  97.                 //当按下32(空格键)时,mario开始跳跃
  98.                 if(ke.getKeyCode() == 32) {
  99.                         this.mario.jump();
  100.                 }
  101.                
  102.                 System.out.println(ke.getKeyCode());
  103.         }

  104.         @Override
  105.         public void keyReleased(KeyEvent ke) {
  106.                 // TODO Auto-generated method stub
  107.                 //当抬起39时(-->),mario停止向右移动
  108.                 if(ke.getKeyCode() == 39) {
  109.                         this.mario.rightStop();
  110.                 }
  111.                 //当抬起37时(<--),mario停止向左移动
  112.                 if(ke.getKeyCode() == 37) {
  113.                         this.mario.leftStop();
  114.                 }
  115.                 System.out.println(ke.getKeyCode());
  116.         }
  117.        

  118.         @Override
  119.         public void keyTyped(KeyEvent arg0) {
  120.                 // TODO Auto-generated method stub

  121.         }

  122.         @Override
  123.         public void run() {
  124.                 // TODO Auto-generated method stub
  125.                 while(true) {
  126.                         this.repaint();
  127.                         try {
  128.                                 Thread.sleep(50);
  129.                                 if(this.mario.getX() >= 840) {
  130.                                         //切换场景
  131.                                         this.nowBG = this.allBG.get(this.nowBG.getSort()+1);
  132.                                         //将当前场景设置mario中
  133.                                         this.mario.setBg(this.nowBG);
  134.                                         //修改mario的坐标
  135.                                         this.mario.setX(0);
  136.                                 }
  137.                         } catch (InterruptedException e) {
  138.                                 // TODO Auto-generated catch block
  139.                                 e.printStackTrace();
  140.                         }
  141.                 }
  142.         }

  143. }
复制代码


5.Obstruction.java
  1. package org.liky.mario;

  2. import java.awt.image.BufferedImage;

  3. public class Obstruction {
  4.         // 保存的坐标
  5.         private int x;
  6.         private int y;
  7.        
  8.         public int getX() {
  9.                 return x;
  10.         }

  11.         public int getY() {
  12.                 return y;
  13.         }
  14.         //类型
  15.         private int type;
  16.         public void setType(int type) {
  17.                 this.type = type;
  18.         }

  19.         public int getType() {
  20.                 return type;
  21.         }
  22.         //初始的类型
  23.         private int startType;
  24.         //显示图片
  25.         private BufferedImage showimage = null;
  26.        
  27.         //构造方法
  28.        
  29.         public BufferedImage getShowimage() {
  30.                 return showimage;
  31.         }

  32.         public Obstruction(int x,int y,int type) {
  33.                 this.x = x;
  34.                 this.y = y;
  35.                 this.type = type;
  36.                 setImage();
  37.                
  38.         }
  39.        
  40.         //重置方法
  41.         public void reset() {
  42.                 //修改类型为初始类型
  43.                 this.type = startType;
  44.                 //改变显示图片
  45.                 this.setImage();
  46.         }
  47.         //根据类型改变显示图片
  48.         public void setImage() {
  49.                 showimage = StaticValue.allObstructionImage.get(type);
  50.         }
  51.        
  52. }
复制代码


6.StaticValue.java
  1. package org.liky.mario;

  2. import java.awt.image.BufferedImage;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.List;

  7. import javax.imageio.ImageIO;

  8. import com.sun.org.apache.bcel.internal.generic.IfInstruction;

  9. public class StaticValue {
  10.        
  11.         public static List<BufferedImage> allMarioImage = new ArrayList<BufferedImage>();
  12.        
  13.         public static BufferedImage startImage = null;
  14.        
  15.         public static BufferedImage endImage = null;
  16.        
  17.         public static BufferedImage bgImage = null;
  18.        
  19.         public static List<BufferedImage> allFlowerImage = new ArrayList<BufferedImage>();
  20.        
  21.         public static List<BufferedImage> allTriangleImage = new ArrayList<BufferedImage>();
  22.        
  23.         public static List<BufferedImage> allTurtleImage = new ArrayList<BufferedImage>();
  24.        
  25.         public static List<BufferedImage> allObstructionImage = new ArrayList<BufferedImage>();
  26.        
  27.         public static BufferedImage marioDeadImage = null;
  28.        
  29.         public static String imagePath = System.getProperty("user.dir")+"/bin/";
  30.        
  31.         //将全部图片初始化
  32.         public static void init() {
  33.                 //将所有mario的图片保存到静态属性中
  34.                 for(int i = 1;i <= 10; i++) {
  35.                         try {
  36.                                 allMarioImage.add(ImageIO.read(new File(imagePath + i + ".gif")));
  37.                         } catch (IOException e) {
  38.                                 // TODO Auto-generated catch block
  39.                                 e.printStackTrace();
  40.                         }
  41.                 }
  42.                 //导入全部背景图片
  43.                 try {
  44.                         startImage = ImageIO.read(new File(imagePath + "start.gif"));
  45.                         bgImage = ImageIO.read(new File(imagePath + "firststage.gif"));
  46.                         endImage = ImageIO.read(new File(imagePath + "firststageend.gif"));
  47.                 } catch (IOException e) {
  48.                         // TODO Auto-generated catch block
  49.                         e.printStackTrace();
  50.                 }
  51.                 //导入所有敌人的图片
  52.                 for (int i = 1;i <= 5; i++) {
  53.                         try {
  54.                                 if (i <= 2) {
  55.                                         allFlowerImage.add(ImageIO.read(new File(imagePath + "flower"+i+".gif")));
  56.                                 }
  57.                                 if (i <=3) {
  58.                                         allTriangleImage.add(ImageIO.read(new File(imagePath + "triangle"+i+".gif")));
  59.                                 }
  60.                                         allTurtleImage.add(ImageIO.read(new File(imagePath + "Turtle"+i+".gif")));
  61.                         } catch (IOException e) {
  62.                                 // TODO: handle exception
  63.                                 e.printStackTrace();
  64.                         }
  65.                 }
  66.                 //导入障碍物的图片
  67.                 for(int i = 1;i <= 12;i++) {
  68.                         try {
  69.                                 allObstructionImage.add(ImageIO.read(new File(imagePath + "ob"+i+".gif")));
  70.                         } catch (IOException e) {
  71.                                 // TODO Auto-generated catch block
  72.                                 e.printStackTrace();
  73.                         }
  74.                        
  75.                 }
  76.                
  77.                 //导入Mario死亡的图片
  78.                 try {
  79.                         marioDeadImage = ImageIO.read(new File(imagePath + "over.gif"));
  80.                 } catch (IOException e) {
  81.                         // TODO Auto-generated catch block
  82.                         e.printStackTrace();
  83.                 }
  84.                
  85.                
  86.         }

  87. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-15 17:06:08 | 显示全部楼层
g2改成g就好了,因为这边要绘制到JFrame
QQ截图20170915170532.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2017-9-15 21:36:49 | 显示全部楼层
本帖最后由 星空下的舞者 于 2017-9-15 21:47 编辑
zlj19931010 发表于 2017-9-15 17:06
g2改成g就好了,因为这边要绘制到JFrame


非常感谢!原来这里写错了
成功运行后我发现这里的食人花居然没有上下移动,而是这样一直往上走了
3.PNG
这又是怎么回事呢,代码里是写了判断的啊
  1. if(this.isLeftOrUp && this.x == this.upMax) {
  2.                                         this.isLeftOrUp = false;
  3.                                 }
  4.                                 if(!this.isLeftOrUp && this.x == this.downMax) {
  5.                                         this.isLeftOrUp = true;
  6.                                 }
复制代码


没看明白这个upMax、downMax是如何起作用的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 01:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表