鱼C论坛

 找回密码
 立即注册
查看: 4167|回复: 8

[已解决]新手改写mario的时候发现障碍物无法打印出来

[复制链接]
发表于 2017-9-4 11:59:10 | 显示全部楼层 |阅读模式

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

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

x
最近正在根据一个很老的教程写mario,但写到障碍物这里发现障碍物无法打印出来,我是根据视频一点一点打出来的,可就是不知道为什么障碍物出不来,恳请大家帮忙看一下
MLDN Mario.zip (548.81 KB, 下载次数: 1)
现在是: 捕获.PNG 但应该是: 捕获.jpg
最佳答案
2017-9-5 14:59:05
在框体那个类里第40行改成this.nowBG = this.allBG.get(1);这个就行了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-9-4 12:02:56 | 显示全部楼层
1.背景:
  1. package org.liky.mario;

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

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

  11.         //场景的顺序
  12.         private int sort;
  13.         //当前是否为最后一个场景
  14.         private boolean flag;
  15.        
  16.         //通过集合来保存
  17.         //全部的敌人
  18.         private List allEnemy = new ArrayList();
  19.         //全部的障碍物
  20.         private List<Obstruction> allObstruction = new ArrayList<Obstruction>();

  21.         public List<Obstruction> getAllObstruction() {
  22.                 return allObstruction;
  23.         }

  24.         //被消灭的敌人
  25.         private List removedEnemy = new ArrayList();
  26.         //被消灭的障碍物
  27.         private List removedObstruction = new ArrayList();
  28.        
  29.         //构造方法
  30.         public BackGround(int sort,boolean flag) {
  31.                 this.sort = sort;
  32.                 this.flag = flag;
  33.                 if (flag) {
  34.                         bgImage = StaticValue.endImage;                       
  35.                 }else {
  36.                         bgImage = StaticValue.bgImage;       
  37.                 }
  38.                 if (sort == 1) {
  39.                         //绘制地面
  40.                         for(int i = 0; i < 15;i++) {
  41.                                 this.allObstruction.add(new Obstruction(i*60, 540, 9));
  42.                         }
  43.                         //绘制砖块和问号
  44.                         this.allObstruction.add(new Obstruction(120, 360, 4));
  45.                         this.allObstruction.add(new Obstruction(300, 360, 0));
  46.                         this.allObstruction.add(new Obstruction(360, 360, 4));
  47.                         this.allObstruction.add(new Obstruction(420, 360, 0));
  48.                         this.allObstruction.add(new Obstruction(480, 360, 4));
  49.                         this.allObstruction.add(new Obstruction(540, 360, 0));
  50.                         this.allObstruction.add(new Obstruction(420, 180, 4));
  51.                         //绘制水管
  52.                         this.allObstruction.add(new Obstruction(660, 540, 6));
  53.                         this.allObstruction.add(new Obstruction(720, 540, 5));
  54.                         this.allObstruction.add(new Obstruction(660, 480, 8));
  55.                         this.allObstruction.add(new Obstruction(720, 480, 7));
  56.                        
  57.                 }
  58.         }
  59.        
  60.         //重置方法,将所有的障碍物和敌人返回原有坐标,并将其状态也修改
  61.         public void reset() {
  62.                
  63.                
  64.         }
  65.        
  66. }
复制代码


2.框体:
  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 {
  12.        
  13.         private List<BackGround> allBG = new ArrayList<BackGround>();
  14.         private BackGround nowBG = null;
  15.        
  16.         public static void main(String[] args) {
  17.                 new MyFrame();
  18.         }
  19.        
  20.         public MyFrame() {
  21.                 this.setTitle("马里奥游戏程序");
  22.                 this.setSize(900, 600);
  23.                 int width = Toolkit.getDefaultToolkit().getScreenSize().width;
  24.                 int height = Toolkit.getDefaultToolkit().getScreenSize().height;
  25.                 this.setLocation((width - 900)/2, (height - 600)/2);
  26.                 this.setResizable(false);
  27.                
  28.                 //初始化全部的图片
  29.                 StaticValue.init();
  30.                
  31.                 //创建全部的场景
  32.                 for(int i = 0;i <= 3;i++) {
  33.                         this.allBG.add(new BackGround(i, i==3?true:false));
  34.                 }
  35.                 //将第一个场景设置为当前场景
  36.                 this.nowBG = this.allBG.get(0);
  37.                
  38.                 this.repaint();
  39.                
  40.                 this.addKeyListener(this);
  41.                
  42.                 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  43.                 this.setVisible(true);
  44.         }
  45.        
  46.        


  47.         public void paint(Graphics g) {
  48.                 // TODO Auto-generated method stub
  49.                 //建立临时的缓冲图片
  50.                 BufferedImage image = new BufferedImage(900, 600, BufferedImage.TYPE_3BYTE_BGR);
  51.                 Graphics g2 = image.getGraphics();
  52.                 //绘制背景
  53.                 g2.drawImage(this.nowBG.getBgImage(), 0, 0, this);
  54.                 //绘制障碍物
  55.                 Iterator<Obstruction> iter = this.nowBG.getAllObstruction().iterator();
  56.                 while(iter.hasNext()) {
  57.                         Obstruction ob = iter.next();
  58.                         g2.drawImage(ob.getShowimage(), ob.getX(), ob.getY(), this);
  59.                 }
  60.                 //把缓冲图片绘制到窗体中
  61.                 g.drawImage(image, 0, 0, this);
  62.                
  63.         }

  64.         @Override
  65.         public void keyPressed(KeyEvent arg0) {
  66.                 // TODO Auto-generated method stub
  67.                 System.out.println(arg0.getKeyCode());

  68.         }

  69.         @Override
  70.         public void keyReleased(KeyEvent arg0) {
  71.                 // TODO Auto-generated method stub

  72.         }

  73.         @Override
  74.         public void keyTyped(KeyEvent arg0) {
  75.                 // TODO Auto-generated method stub

  76.         }

  77. }
复制代码


3.障碍物:
  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.         //初始的类型
  17.         private int startType;
  18.         //显示图片
  19.         private BufferedImage showimage = null;
  20.        
  21.         //构造方法
  22.        
  23.         public BufferedImage getShowimage() {
  24.                 return showimage;
  25.         }

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


4.不知道叫啥...
  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-4 12:04:50 | 显示全部楼层
没有任何运行错误但就是不显示
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-4 15:02:52 | 显示全部楼层
本帖最后由 alltolove 于 2017-9-4 15:12 编辑

逻辑好像没错,是不是图片路径错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-4 15:21:05 | 显示全部楼层
alltolove 发表于 2017-9-4 15:02
逻辑好像没错,是不是图片路径错了

路径应该没问题,背景都可以显示
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-5 13:26:08 | 显示全部楼层
在你不知道叫啥那个类里public static void init() 这个函数好像没调用
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-5 13:46:40 | 显示全部楼层
alltolove 发表于 2017-9-5 13:26
在你不知道叫啥那个类里public static void init() 这个函数好像没调用

谢谢,我刚刚接触java没几天,还不懂这些,能否提供一下解决办法呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-5 14:59:05 | 显示全部楼层    本楼为最佳答案   
在框体那个类里第40行改成this.nowBG = this.allBG.get(1);这个就行了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-5 20:00:58 | 显示全部楼层
alltolove 发表于 2017-9-5 14:59
在框体那个类里第40行改成this.nowBG = this.allBG.get(1);这个就行了

天呐,太感谢你了!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 10:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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