使这段代码更加面向对象?

时间:2011-06-13 17:30:15

标签: java applet

我的朋友告诉我,我的代码不是很好。我如何使这更多的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;
    }
}
}

5 个答案:

答案 0 :(得分:4)

你应该问你的朋友他或她的意思。如果我猜,我会说你的Applet类做了很多事情:它是动画循环;这是关键的处理程序;这是鼠标处理程序;这是用户界面。这些可以分解为旨在协同工作的单独对象。

另一项改进可能是将您的四个...Pressed标记转换为0到7之间的整数,表示八个可能的方向。例如:

0 1 2
7   3
6 5 4

(您可以按照自己喜欢的方式指定方向编号。)然后您可以使用方向编号索引图像数组。 (这与面向对象无关,但它会简化您的代码。)

答案 1 :(得分:3)

一方面,所有这些图像都应该在一个数组中。它们应该包含一个文件名,以便您可以在一个很好的循环中加载它们。你有超过3页的复制和粘贴代码,这绝对是可怕的...如果你想改变他们稍微加载的方式怎么办?

然后,Math.sqrt(Math.pow(speed, 2))跳了出来。这相当于Math.abs(speed),大约快1 / 200x。

下一部分也特别有趣。我不会评论它是否需要(而且很可能不是,这是你的设计缺陷),但是......你...

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

答案 2 :(得分:2)

我建议您阅读有关OOP的书籍和文章,在这种特殊情况下,您应该看一下MVC模式(MVC article in wikipedia)。对于一个好的OO概念,事件(例如keyPressed(KeyEvent e))不应该与实际的“图形构建”类在同一个类中处理。

答案 3 :(得分:1)

为您的应用程序构建一个新包,并将所有数据结构分组。为你的英雄创建一个类,为树木和草创建类。了解它们如何共享属性并创建类层次结构,可能使用抽象类。

这些只是一些提示,可能需要很长时间才能为您提供面向对象的课程;)

但是跟着这些曲目阅读。

此外,您应该学习如何停止threads elegantly

你可以为你的听众使用内部类,甚至使用MouseAdater。

此致  斯特凡

答案 4 :(得分:0)

您可能希望研究这个game,它还会使用键盘将玩家移动到网格中。