2011-06-13 82 views
-5

我的朋友告訴我,我的代碼不是很OO。我將如何使這個更多的OO和可重用? 我試過做一個叫做level的類,它在平面上打印圖像,但是注意到了打印,所以沒有成功。使此代碼更面向對象?

(代碼轉儲到來,我很抱歉)

public class Main extends Applet implements Runnable, KeyListener, 
java.awt.event.MouseListener { 
double x_pos = 300; 
double y_pos = 200; 
int radius = 20; 
int appletsize_x = 640; 
int appletsize_y = 440; 
double speed = 3; 
float speedModifier = 1f; 
Image grass; 
Image hero; 
Image hero45; 
Image hero90; 
Image hero135; 
Image hero180; 
Image hero225; 
Image hero270; 
Image hero315; 
Image sprite; 
Image tree; 
Image path; 

private boolean leftPressed; 
private boolean rightPressed; 
private boolean downPressed; 
private boolean upPressed; 

private Image dbImage; 
private Graphics dbg; 

public void init() { 
    Dimension dim = getMaximumSize(); 
    this.setSize(appletsize_x, appletsize_y); 
    MediaTracker mt = new MediaTracker(this); 
    tree = getImage(getCodeBase(), "tree_64.png"); 
    grass = getImage(getCodeBase(), "grassTile.png"); 
    path = getImage(getCodeBase(), "path.png"); 
    hero = getImage(getCodeBase(), "hero.png"); 
    hero45 = getImage(getCodeBase(), "hero45.png"); 
    hero90 = getImage(getCodeBase(), "hero90.png"); 
    hero135 = getImage(getCodeBase(), "hero135.png"); 
    hero180 = getImage(getCodeBase(), "hero180.png"); 
    hero225 = getImage(getCodeBase(), "hero225.png"); 
    hero270 = getImage(getCodeBase(), "hero270.png"); 
    hero315 = getImage(getCodeBase(), "hero315.png"); 
    sprite = getImage(getCodeBase(), "hero.png"); 
    mt.addImage(hero, 0); 
    mt.addImage(path, 0); 
    mt.addImage(tree, 0); 
    mt.addImage(grass, 0); 
    mt.addImage(hero45, 0); 
    mt.addImage(hero90, 0); 
    mt.addImage(hero135, 0); 
    mt.addImage(hero180, 0); 
    mt.addImage(hero225, 0); 
    mt.addImage(hero270, 0); 

    try { 
     mt.waitForID(0); 
    } catch (InterruptedException ie) { 
    } 
} 

public void start() { 

    this.addKeyListener(this); 
    this.addMouseListener(this); 
    Thread th = new Thread(this); 
    th.start(); 
} 

public void stop() { 

} 

public void destroy() { 

} 

public void run() { 

    // lower ThreadPriority 
    this.requestFocusInWindow(); 
    Thread.currentThread().setPriority(Thread.MIN_PRIORITY); 

    while (true) { 

     if (downPressed && leftPressed) { 
      double y_decr, x_incr; 
      y_decr = Math.sqrt(Math.pow(speed, 2))/1.5; 
      x_incr = Math.sqrt(Math.pow(speed, 2))/1.5; 
      y_pos += y_decr; 
      x_pos -= x_incr; 
     } else if (downPressed && rightPressed) { 
      y_pos += Math.sqrt(Math.pow(speed, 2))/1.5; 
      x_pos += Math.sqrt(Math.pow(speed, 2))/1.5; 
     } else if (upPressed && rightPressed) { 
      y_pos -= Math.sqrt(Math.pow(speed, 2))/1.5; 
      x_pos += Math.sqrt(Math.pow(speed, 2))/1.5; 
     } else if (upPressed && leftPressed) { 
      y_pos -= Math.sqrt(Math.pow(speed, 2))/1.5; 
      x_pos -= Math.sqrt(Math.pow(speed, 2))/1.5; 
     } else { 
      // Hitting (right) 
      if (x_pos > this.getSize().width - radius) { 

       // x_speed = -x_speed; 
      } 
      if (leftPressed == true) { 
       x_pos -= speed * speedModifier; 
      } 

      if (rightPressed == true) { 
       x_pos += speed * speedModifier; 
      } 

      if (upPressed == true) { 
       y_pos -= speed * speedModifier; 
      } 

      if (downPressed == true) { 
       y_pos += speed * speedModifier; 

      } 
     } 

     // Hitting right 

     if (x_pos > this.getSize().width - 32) { 
      x_pos = this.getSize().width - 32; 

     } // Hitting left 
     if (x_pos < 0) { 
      x_pos = 0; 
     } 
     // } // Hitting top 
     if (y_pos < 0) { 
      y_pos = 0; 

     } 

     // Hitting bottom 
     if (y_pos > this.getSize().height - 32) { 
      y_pos = this.getSize().height - 32; 

     } 


     repaint(); 

     try { 
      // Stop thread for 1 milliseconds 
      Thread.sleep(20); 
     } catch (InterruptedException ex) { 
      // do nothing 
     } 

     // set ThreadPriority to maximum value 
     Thread.currentThread().setPriority(Thread.MIN_PRIORITY); 
    } 
} 

public void paint(Graphics g) { 
    int layout = 0; 
    int coll = 0; 
    while (coll <= 440) { 
     drawRows(layout, grass, g, coll); 
     coll = coll + 32; 
    } 
    drawCollums(0, path, g, 180); 
    g.drawImage(tree, 50, 40, this); 
    g.drawImage(tree, 300, 20, this); 
    g.drawImage(tree, 500, 300, this); 
    int x_posI = (int) x_pos; 
    int y_posI = (int) y_pos; 

    if (downPressed && leftPressed) { 
     this.sprite = hero225; 
    } else if (downPressed && rightPressed) { 
     this.sprite = hero135; 
    } else if (upPressed && rightPressed) { 
     this.sprite = hero45; 
    } else if (upPressed && leftPressed) { 
     this.sprite = hero315; 
    } else if (leftPressed == true) { 
     this.sprite = hero270; 
    } else if (rightPressed == true) { 
     this.sprite = hero90; 
    } else if (upPressed == true) { 
     this.sprite = hero; 
    } else if (downPressed == true) { 
     this.sprite = hero180; 
    } 

    // this.sprite will contain value set on last "movement" 
    g.drawImage(this.sprite, x_posI, y_posI, this); 


} 



public void update(Graphics g) { 

    if (dbImage == null) { 
     dbImage = createImage(this.getSize().width, this.getSize().height); 
     dbg = dbImage.getGraphics(); 
    } 

    dbg.setColor(getBackground()); 
    dbg.fillRect(0, 0, this.getSize().width, this.getSize().height); 

    dbg.setColor(getForeground()); 
    paint(dbg); 

    g.drawImage(dbImage, 0, 0, this); 
} 

@Override 
public void keyPressed(KeyEvent e) { 
    if (e.getKeyCode() == KeyEvent.VK_LEFT) { 
     leftPressed = true; 

    } 
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) { 
     rightPressed = true; 

    } 
    if (e.getKeyCode() == KeyEvent.VK_UP) { 
     upPressed = true; 
    } 
    if (e.getKeyCode() == KeyEvent.VK_DOWN) { 
     downPressed = true; 
    } 

} 

@Override 
public void keyReleased(KeyEvent e) { 

    if (e.getKeyCode() == KeyEvent.VK_LEFT) { 
     leftPressed = false; 
    } 
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) { 

     rightPressed = false; 
    } 
    if (e.getKeyCode() == KeyEvent.VK_UP) { 
     upPressed = false; 

    } 
    if (e.getKeyCode() == KeyEvent.VK_DOWN) { 
     downPressed = false; 
    } 
} 

@Override 
public void keyTyped(KeyEvent e) { 

} 

@Override 
public void mouseClicked(MouseEvent e) { 
    System.out.println("HIT!"); 
    // TODO Auto-generated method stub 

} 

@Override 
public void mouseEntered(MouseEvent e) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void mouseExited(MouseEvent e) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void mousePressed(MouseEvent e) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void mouseReleased(MouseEvent e) { 
    // TODO Auto-generated method stub 

} 

public void drawRows(int x, Image image, Graphics g, int coll) { 
    while (x <= appletsize_x) { 
     g.drawImage(image, x, coll, this); 
     x += 32; 
    } 
} 

public void drawCollums(int y, Image image, Graphics g, int coll) { 
    while (y <= appletsize_x) { 
     g.drawImage(image, coll, y, this); 
     y += 32; 
    } 
} 
} 
+6

你可以通過閱讀[神的對象(http://en.wikipedia.org/wiki/God_object)開始,並儘量避免在首位寫他們。這是參考'(代碼轉儲來了,我很抱歉)' – 2011-06-13 17:33:51

+0

我不認爲任何人會重構你的代碼,你甚至沒有說它做了什麼。獲取一本關於面向對象編程的書,從簡單的例子開始嘗試儘可能多地學習。 在這段代碼中實際上沒有/很少的OOP。 – 2011-06-13 17:35:05

+2

要進一步擴展我以前的評論。如果你對每一個不同的職責都有一個單獨的課程,那麼你的代碼就會反映出來,問題也會反映出來。我沒有讀過整個班級,但很明顯,關注的分離會有助於閱讀。 – 2011-06-13 17:38:18

回答

3

好一兩件事,所有這些圖片應該是在數組中。並且它們應該包含一個文件名,以便您可以將它們加載到一個很好的循環中你有超過3頁的複製&粘貼代碼,這是非常可怕的......如果你想改變他們稍微加載的方式怎麼辦?

然後,Math.sqrt(Math.pow(speed, 2))跳出我。這相當於Math.abs(speed),約等於1/200x。

這下一部分也特別好笑。我不會評論是否需要它的(和它最有可能不是,這是你一個設計缺陷),但..亞..

 // Stop thread for 1 milliseconds 
     Thread.sleep(20); 
+0

+1,苛刻的o_O,但是真的... – mre 2011-06-13 17:39:10

+0

我不認爲這是那麼苛刻,我給了他具體的觀點來改進他的代碼。 – Blindy 2011-06-13 17:46:31

+0

我刪除了它,並導致它打破,這有點有趣。 – Derek 2011-06-13 18:14:10

1

新建一個包爲您的應用和將你所有的數據結構分組。爲您的英雄創建一個班級,爲樹木和草地創建班級。看看他們如何共享屬性並創建一個類層次結構,也許有一個抽象類。

這些都只是一些線索,也可能是長期在這裏給你一個面向對象的課程;)

但遵循這一軌跡和讀取。

此外,你應該學會如何停止threads elegantly

而且你可以用內部類的聽衆,甚至使用MouseAdater。

問候, 斯特凡

4

你應該問你的朋友他或她的意思。如果我猜測,我會說你的Applet類正在做很多事情:這是動畫循環;它是關鍵處理程序;它是鼠標處理程序;這是用戶界面。這些可以分解成獨立的對象,旨在一起工作。

另一項改進可能是從0到7,你的四個...Pressed標誌轉化爲一個整數,代表八個可能的方向。例如:

0 1 2 
7 3 
6 5 4 

(您可以指定方向號碼,你喜歡的任何方式。)然後你可以使用方向數索引圖像陣列。 (這與面向對象無關,但它會簡化你的代碼。)

2

我建議你閱讀關於OOP的書籍和文章,在這種特殊情況下你應該看看MVC模式(MVC article in wikipedia) 。對於一個好的OO概念,事件(例如keyPressed(KeyEvent e))不應該與實際的「圖形建築」類在同一類中進行處理。

0

你可能想研究這個game也使用鍵盤移動的玩家周圍的網格。

相關問題