新手改写mario的时候发现障碍物无法打印出来
最近正在根据一个很老的教程写mario,但写到障碍物这里发现障碍物无法打印出来,我是根据视频一点一点打出来的,可就是不知道为什么障碍物出不来,恳请大家帮忙看一下现在是:但应该是: 1.背景:
package org.liky.mario;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
public class BackGround {
//当前场景的显示图片
private BufferedImage bgImage = null;
public BufferedImage getBgImage() {
return bgImage;
}
//场景的顺序
private int sort;
//当前是否为最后一个场景
private boolean flag;
//通过集合来保存
//全部的敌人
private List allEnemy = new ArrayList();
//全部的障碍物
private List<Obstruction> allObstruction = new ArrayList<Obstruction>();
public List<Obstruction> getAllObstruction() {
return allObstruction;
}
//被消灭的敌人
private List removedEnemy = new ArrayList();
//被消灭的障碍物
private List removedObstruction = new ArrayList();
//构造方法
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));
}
}
//重置方法,将所有的障碍物和敌人返回原有坐标,并将其状态也修改
public void reset() {
}
}
2.框体:
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 {
private List<BackGround> allBG = new ArrayList<BackGround>();
private BackGround nowBG = null;
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(0);
this.repaint();
this.addKeyListener(this);
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<Obstruction> iter = this.nowBG.getAllObstruction().iterator();
while(iter.hasNext()) {
Obstruction ob = iter.next();
g2.drawImage(ob.getShowimage(), ob.getX(), ob.getY(), this);
}
//把缓冲图片绘制到窗体中
g.drawImage(image, 0, 0, this);
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
System.out.println(arg0.getKeyCode());
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
3.障碍物:
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;
//初始的类型
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);
}
}
4.不知道叫啥...
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();
}
}
}
没有任何运行错误但就是不显示{:10_266:} 本帖最后由 alltolove 于 2017-9-4 15:12 编辑
逻辑好像没错,是不是图片路径错了 alltolove 发表于 2017-9-4 15:02
逻辑好像没错,是不是图片路径错了
路径应该没问题,背景都可以显示 在你不知道叫啥那个类里public static void init() 这个函数好像没调用 alltolove 发表于 2017-9-5 13:26
在你不知道叫啥那个类里public static void init() 这个函数好像没调用
谢谢,我刚刚接触java没几天,还不懂这些,能否提供一下解决办法呢?{:5_111:} 在框体那个类里第40行改成this.nowBG = this.allBG.get(1);这个就行了 alltolove 发表于 2017-9-5 14:59
在框体那个类里第40行改成this.nowBG = this.allBG.get(1);这个就行了
天呐,太感谢你了!{:9_221:}
页:
[1]