我当前的代码列在最底部。到目前为止,我已经画出了我希望它在最终版中看到的 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();
}
}