2016-05-26 5 views
1

Mein Problem ist sehr einfach: Ich habe Schwierigkeiten eine Lösung zu finden, um Mario zu Fall zu bringen, wenn er auf Lücke geht (leerer Platz, kein Boden). Wenn Sie einen effektiven Weg kennen, lassen Sie es mich wissen. Bitte lassen Sie mich wissen, wenn Sie meine Erklärung meines Codes benötigen, wenn Sie nicht verstehen, danke!JAVA Super Mario lässt Mario auf Lücke fallen

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; 
} 
} 

} 
+3

Sie haben ziemlich viel Code geschrieben, das meiste davon ist ziemlich redundant, das meiste davon völlig unabhängig von dem Problem - Ihre Frage. Bitte seien Sie freundlich zu uns und versuchen Sie sich daran zu erinnern, dass wir alle Freiwillige sind - zeigen Sie bitte nur den für Ihr Problem relevanten Code, am besten, wenn Sie ein gültiges [mcve] erstellen und veröffentlichen können - bitte lesen Sie den Link. –

+1

Beachten Sie, dass wenn Ihr Programm in einer MVC-Mode erstellt wurde und das Modell deutlich von der Ansicht getrennt ist, es viel einfacher wäre, [mcve] zu erstellen, da Ihr Programm bereits refaktoriert wäre und bereits eine viel höhere Kohäsion hätte und niedrigere Kopplung. –

Antwort

0

Zuerst lesen Sie bitte this. Wir wollen dir helfen, aber du solltest es uns zumindest nicht schwerer machen.

Wie auch immer, lass uns jetzt zu deiner Frage gehen.

Nun, von dem, was ich aus Ihrem Code lesen kann, kann ich sehen, dass die Karte [] der Boden ist. Ich nehme an, dass 1 ein Block des Bodens und 0 eine Lücke ist. Wenn das so ist, werde ich versuchen, mit meiner Erklärung fortzufahren.

Was ich tun würde, ist den Bildschirm in so viele Spalten wie wie alle Elemente in der map [] -Array zu teilen. Wenn Sie beispielsweise 20 Elemente im Karten-Array haben, sollte das Programm den Bildschirm in 20 Spalten unterteilen. Jedes Element im Array entspricht einer Spalte auf dem Bildschirm.

Dann sollte Marios x-Wert überprüft werden, das Programm sollte überprüfen, in welchem ​​Bereich er sich befindet. Wenn sein Abschnitt eine 0 im Kartenfeld hat, sollte er fallen. Wenn das Kartenfeld eine 1 hat, dann ist er auf dem Boden.

Verwandte Themen