我当前的代码列在最底层。到目前为止,我已经开始绘制galaga船,因为我希望它能够在最终版本中看到,以及随机出现的星星。现在,我唯一需要弄清楚的问题是如何创建数组列表,例如:
arrayList = new Arraylist()
新的arrayList具有分配给它的变量集,以及g.drawLine方法。基本上分配一个arraylist变量(x1,y1,x2,y2),基本思路是所有给定的行都分配给这个数组。另外,为了使它实际移动,我会使用if()循环,其中if(按下“Up”),则y1 + = 5;和y2 + = 5;所以它会向上移动,以及其他如果使用“右”,“左”和“向下”弓的循环。 非常感谢任何帮助,谢谢。
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();
}
}
答案 0 :(得分: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.
答案 1 :(得分: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);
...
}
其中X0
和Y0
是指向(0,0)
的偏移量。然后编写一个事件处理程序:
void moveUp(int dx, int dy) { X0 += dx; Y0 += dy; }
注意对X0
和Y0
的多线程访问。