JAVA超级马里奥让马里奥陷入差距

时间:2016-05-26 03:03:02

标签: java

我的问题非常简单:我很难找到一个解决办法,让马里奥在走上差距(空地,没有地面)时摔倒。如果您知道这样做的有效方法,请告诉我。另外,如果您不理解,请告诉我您是否需要我的代码说明,谢谢!

public class SuperMario extends JPanel {

 static BufferedImage brick;
 static BufferedImage groundb;
 static BufferedImage one;   static BufferedImage onef; 
 static BufferedImage two;   static BufferedImage twof;
 static BufferedImage three;   static BufferedImage threef;
 static BufferedImage four;   static BufferedImage fourf;
 static BufferedImage five;   static BufferedImage fivef;
 static BufferedImage six;   static BufferedImage sixf;
 static BufferedImage seven;   static BufferedImage sevenf;
 static BufferedImage eight;   static BufferedImage eightf;
 boolean gameover=false;
 boolean movenow=false;
 boolean left=false;
 boolean right=true;
 boolean jump=false;
 int jumpmax=0;
 int step=0;
 int frame=0;
 boolean framebegin=false;
 boolean grounded=true;
 int x1=1;
 public final int map[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
 int size=map.length;
 boolean movemap=false;

public static void main(String[] args) {
    JFrame window = new JFrame("Super Mario"); 
    SuperMario content = new SuperMario();
    window.setContentPane(content);
    window.setSize(1200, 800);
    window.setLocation(100,0);
    window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    window.setResizable(false);  // User can't change the window's size.
    window.setVisible(true);

        try {
            brick = ImageIO.read(new File("SuperMario/brick.png"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        try {
            groundb = ImageIO.read(new File("SuperMario/ground.png"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            one = ImageIO.read(new File("SuperMario/1.jpg"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        try {
            two = ImageIO.read(new File("SuperMario/2.jpg"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        try {
            three = ImageIO.read(new File("SuperMario/3.jpg"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        try {
            four = ImageIO.read(new File("SuperMario/4.jpg"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            five = ImageIO.read(new File("SuperMario/5.jpg"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        try {
            six = ImageIO.read(new File("SuperMario/6.jpg"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        try {
            seven = ImageIO.read(new File("SuperMario/7.jpg"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        try {
            eight = ImageIO.read(new File("SuperMario/8.jpg"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            onef = ImageIO.read(new File("SuperMario/1f.jpg"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        try {
            twof = ImageIO.read(new File("SuperMario/2f.jpg"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        try {
            threef = ImageIO.read(new File("SuperMario/3f.jpg"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        try {
            fourf = ImageIO.read(new File("SuperMario/4f.jpg"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            fivef = ImageIO.read(new File("SuperMario/5f.jpg"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        try {
            sixf = ImageIO.read(new File("SuperMario/6f.jpg"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        try {
            sevenf = ImageIO.read(new File("SuperMario/7f.jpg"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        try {
            eightf = ImageIO.read(new File("SuperMario/8f.jpg"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
}

SuperMario(){

    ActionListener action = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if(framebegin){
                frame++;
                if(frame>=100&&jump){   
                }
                else if(frame>=100&&!jump){
                    frame=0;
                    framebegin=false;
                    step=0;
                }
            }
            if(mario.centerX<0)
                mario.centerX=0;
            repaint();
        }
    };

    ActionListener actionanimation = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if(mario.animation==8){
                mario.animation=1; movenow=false;}
        if(mario.animation<=8&&movenow){
            mario.animation++;
        }
        repaint();
        }
    };

    timer = new Timer( 20, action );
    marioanimation = new Timer (100, actionanimation);

    addMouseListener( new MouseAdapter() {

        public void mousePressed(MouseEvent evt) {
            requestFocus();
        }
    } );

    addFocusListener( new FocusListener() {

        public void focusGained(FocusEvent evt) {
            timer.start();
            marioanimation.start();
            repaint();
        }
        public void focusLost(FocusEvent evt) {
            timer.stop();
            marioanimation.stop();
            repaint();
        }
    } );

    addKeyListener( new KeyAdapter() {

        public void keyPressed(KeyEvent evt) {
            int code = evt.getKeyCode(); 
            if (code == KeyEvent.VK_LEFT&&mario.centerY==555&&!movemap) {
                left=true; right=false; movenow=true;
                    mario.centerX-=30;
                step++; framebegin=true;
            }
            if (code == KeyEvent.VK_RIGHT&&mario.centerY==555&&!movemap) {
                right=true; left=false; movenow=true;
                if(mario.centerX>=600){
                    movemap=true;
                }
                if(!movemap)
                    mario.centerX+=30;
                else if(movemap&&mario.centerX!=600)
                    mario.centerX-=20;
                step++; framebegin=true;;
            }
            if (code == KeyEvent.VK_UP&&mario.centerY==555&&!jump) {
                jump=true;}
        }
    } );
}

public Ground ground;
public Mario mario;
public Timer timer;
public Timer marioanimation;

public void paintComponent(Graphics g){

setBackground(Color.WHITE);
    if(ground==null){
        ground = new Ground();
        mario = new Mario();
    }
    ground.draw(g);
    mario.draw(g);
}

public class Ground implements ImageObserver {
int centerX=0, centerY=0;
int newx=0;
int nbr=1;

void draw(Graphics g){
    for(int y = 1; y<4; y++){
    centerY=y*50+580;
    for(int x=1; x<size; x++){
    centerX=(x-1)*50+newx;
    if(movemap){
        newx-=20;
        movemap=false;
    }
    if(map[x]==1){
    g.drawImage(groundb, centerX, centerY, 50, 50, this);
    }   
    }
    }

}
@Override
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
    // TODO Auto-generated method stub
    return false;
}
}

public class Mario implements ImageObserver {
int centerX=0;
int centerY=555;
int animation=1;

void draw(Graphics g){

    if(jump&&jumpmax!=300&&step<3){
        centerY-=15;
        jumpmax+=15;
    }

    if(centerY==555){
        jumpmax=0;
    }

    if(jump&&jumpmax!=300&&right&&step>=3){
        centerY-=15;
        jumpmax+=15;
        centerX+=15;
    }
    else if(jump&&jumpmax!=300&&left&&step>=3){
        centerY-=15;
        jumpmax+=15;
        centerX-=15;
    }

    if(jumpmax==300&&centerY!=555&&right){
        centerY+=15;
        jump=false;
        step=0;
    }
    if(jumpmax==300&&centerY!=555&&left){
        centerY+=15;
        jump=false;
        step=0;
    }
    if(jumpmax==300&&centerY!=555&&step<3){
        centerY+=15;
        jump=false;
        step=0;
    }
    if(right){
        if(animation==1){
        g.drawImage(one, centerX, centerY, 75, 75, this);}
        if(animation==2){
            g.drawImage(two, centerX, centerY, 75, 75, this);}
        if(animation==3){
            g.drawImage(three, centerX, centerY, 75, 75, this);}
        if(animation==4){
            g.drawImage(four, centerX, centerY, 75, 75, this);}
        if(animation==5){
            g.drawImage(five, centerX, centerY, 75, 75, this);}
        if(animation==6){
            g.drawImage(six, centerX, centerY, 75, 75, this);}
        if(animation==7){
            g.drawImage(seven, centerX, centerY, 75, 75, this);}
        if(animation==8){
            g.drawImage(eight, centerX, centerY, 75, 75, this);}
        }
        else if(left){
            if(animation==1){
                g.drawImage(onef, centerX, centerY, 75, 75, this);}
                if(animation==2){
                    g.drawImage(twof, centerX, centerY, 75, 75, this);}
                if(animation==3){
                    g.drawImage(threef, centerX, centerY, 75, 75, this);}
                if(animation==4){
                    g.drawImage(fourf, centerX, centerY, 75, 75, this);}
                if(animation==5){
                    g.drawImage(fivef, centerX, centerY, 75, 75, this);}
                if(animation==6){
                    g.drawImage(sixf, centerX, centerY, 75, 75, this);}
                if(animation==7){
                    g.drawImage(sevenf, centerX, centerY, 75, 75, this);}
                if(animation==8){
                    g.drawImage(eightf, centerX, centerY, 75, 75, this);}
                }
 }
@Override
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
    // TODO Auto-generated method stub
    return false;
}
}

}

1 个答案:

答案 0 :(得分:0)

首先,请阅读this。我们想帮助你,但至少你不应该让我们更难。

无论如何,现在让我们回答你的问题。

嗯,从我的代码中可以看到,我可以看到map []是基础。我假设1是地块,0是间隙。如果是这样,我会尝试继续解释。

我要做的是将屏幕划分为map []数组中任何元素的列数。例如,如果地图数组中有20个元素,则程序应将屏幕划分为20列。数组中的每个元素对应于屏幕上的一列。

然后,应该检查Mario的x值,程序应该检查他所在的部分。如果他的部分在地图数组中有0,他应该摔倒。如果地图数组为1,那么他就在地面上。