0

我当前的代码列在最底部。到目前为止,我已经画出了我希望它在最终版中看到的 galaga 船,以及随机出现的星星。现在,唯一的问题或我需要弄清楚的事情是如何创建一个数组列表,例如:

arrayList = new Arraylist()

新的 arrayList 具有分配给它的变量集,以及关于 g.drawLine 方法的位置。基本上分配一个arraylist变量(x1,y1,x2,y2),基本思想是所有给定的行都分配给这个数组。另外,为了让它真正移动,我会使用 if() 循环,其中 if("Up" ispressed),然后 y1+=5; 并且y2+=5;所以它会向上移动,以及其他使用“右”、“左”和“下”箭头的 else if 循环。任何帮助将不胜感激,谢谢。

import java.awt.*;
import java.applet.*;

public class Galaga extends Applet
{
public void paint(Graphics g)
{
    DrawBackground(g);
    DrawGalaga(g);
}
public void DrawBackground(Graphics g)
{
    g.setColor(Color.black);
    g.fillRect(1,2,3000,4000);
    DrawStars(g);
}
public void DrawGalaga(Graphics g)
{       
    g.setColor(Color.red);
    g.drawLine(42,75,42,80); //Far Left Gun
    g.drawLine(43,75,43,80);
    g.drawLine(44,75,44,80);
    g.drawLine(45,75,45,80);

    g.setColor(Color.white);
    g.drawLine(42,80,42,100); //Far Left Piece
    g.drawLine(43,80,43,100);
    g.drawLine(44,80,44,100);
    g.drawLine(45,80,45,100);

    g.drawLine(46,83,46,96); //From Left, 1st one
    g.drawLine(47,83,47,96);
    g.drawLine(48,83,48,96);
    g.drawLine(49,83,49,96);

    g.drawLine(50,79,50,92); //From Left, 2nd one
    g.drawLine(51,79,51,92);
    g.drawLine(52,79,52,92);
    g.drawLine(53,79,53,92);    

    g.drawLine(54,65,54,89); //From Left, 3rd one
    g.drawLine(55,65,55,89);
    g.drawLine(56,65,56,89);
    g.drawLine(57,65,57,89);

    g.drawLine(58,75,58,89); //From Middle 3rd one
    g.drawLine(59,75,59,89);
    g.drawLine(60,75,60,89);
    g.drawLine(61,75,61,89);

    g.drawLine(62,70,62,85); //From Middle, 2nd Left one
    g.drawLine(63,70,63,85);
    g.drawLine(64,70,64,85);
    g.drawLine(65,70,65,85);

    g.drawLine(66,60,66,95); //From Middle, 1st Left one
    g.drawLine(67,60,67,95);
    g.drawLine(68,60,68,95);
    g.drawLine(69,60,69,95);

    g.drawLine(70,45,70,100); //Big Middle Piece
    g.drawLine(71,45,71,100);
    g.drawLine(72,45,72,100);
    g.drawLine(73,45,73,100);
    g.drawLine(74,45,74,100);
    g.drawLine(75,45,75,100);
    g.drawLine(76,45,76,100);

    g.drawLine(77,60,77,95); //From Middle, 1st Right one
    g.drawLine(78,60,78,95);
    g.drawLine(79,60,79,95);
    g.drawLine(80,60,80,95);

    g.drawLine(81,70,81,85); //From Middle, 2nd Right one
    g.drawLine(82,70,82,85);
    g.drawLine(83,70,83,85);
    g.drawLine(84,70,84,85);

    g.drawLine(85,75,85,89); //From Middle 3rd one
    g.drawLine(86,75,86,89);
    g.drawLine(87,75,87,89);
    g.drawLine(88,75,88,89);

    g.drawLine(88,65,88,89); //From Right, 3rd one
    g.drawLine(89,65,89,89);
    g.drawLine(90,65,90,89);
    g.drawLine(91,65,91,89);

    g.drawLine(92,79,92,92); //From Right, 2nd one
    g.drawLine(93,79,93,92);
    g.drawLine(94,79,94,92);
    g.drawLine(95,79,95,92);

    g.drawLine(96,83,96,96); //From Right, 1st one
    g.drawLine(97,83,97,96);
    g.drawLine(98,83,98,96);
    g.drawLine(99,83,99,96);

    g.drawLine(100,80,100,100); //Far Right Piece
    g.drawLine(101,80,101,100);
    g.drawLine(102,80,102,100);
    g.drawLine(103,80,103,100);

    g.setColor(Color.red);
    g.drawLine(100,75,100,80); //Far Right Gun
    g.drawLine(101,75,101,80);
    g.drawLine(102,75,102,80);
    g.drawLine(103,75,103,80);
}

public void DrawStars(Graphics g)
{
    g.setColor(Color.yellow);
    int x, y;
    for (int s = 0; s < 2000; s++) 
        {
            x = (int)(Math.random()*2000);
            y = (int)(Math.random()*2000); 
            g.drawLine(x, y, x, y);
        }
}
public void delay(int n)
{
    long startDelay = System.currentTimeMillis();
    long endDelay = 0;
    while (endDelay - startDelay < n)
        endDelay = System.currentTimeMillis();
}
}  
4

2 回答 2

1

您走在正确的轨道上,但是您需要利用一件事。使用数组存储每行的 X、Y 值是一个好主意。但是,关于更改这些值的第二部分是不正确的。对于 2d 和 3d 图形,您的模型不应该改变,除非它是动画的。这意味着您的数组值不会改变。要移动船,包括位置、旋转和倾斜,您可以在图形对象上使用变换。

查看本教程以了解我在说什么。

http://docs.oracle.com/javase/tutorial/2d/advanced/transforming.html

至于存储您的 x1、x2、y1、y2 值,您可以创建另一个类。

public class MyLine
{
    public final int x1;
    public final int x2;
    public final int y1;
    public final int y2;
    public MyLine(int x1, int x2, int y1, int y2)
    {
        this.x1 = x1;
        this.x2 = x2;
        this.y2 = y1;
        this.y2 = y2;
    }
}

然后你可以使用数组列表如下

List<MyLine> myLineList = new ArrayList<MyLine>();

myLineList.add(new MyLine(1,2,3,4)); //add as many lines as you'd like.
于 2013-02-19T15:18:41.897 回答
1

老实说,这段代码看起来不太好。

首先,使用一些对象而不是数组偏移的隐含角色。

List<Line> lines = new ArrayList<Line>();

现在每个数组元素都是有效的行。您可能应该更多地重构代码,加上它的颜色等。

回到你的问题:不要复制数组,以只读方式存储船的点并操纵 x,y 偏移量。

public void DrawGalaga(Graphics g)
{       
    g.setColor(Color.red);
    g.drawLine(X0+42,Y0+75,X0+42,Y0+80);
    ...
}

whereX0Y0是指向 的偏移量(0,0)。然后编写一个事件处理程序:

void moveUp(int dx, int dy) { X0 += dx; Y0 += dy; }

注意多线程访问X0Y0.

于 2013-02-19T15:21:38.150 回答