有人可以帮助我进一步参与这个Galaga游戏吗?

时间:2013-03-15 02:50:04

标签: java arrays variables recursion arraylist

我开始在学校的计算机科学任务中开始这个galaga程序,但是今天只需要努力工作,基本上我已经完成了所有的工作(是的,我要添加更多的Enemy船,但是其他一切都设置了,即使代码很乱(是的,我知道它不是最好的,但是现在,它有效)。游戏中的下一个问题是碰撞。我目前的想法是有几个数组,每个敌人一个船,阵列将包括x和y协调,所以如果激光他的阵列中列出一个点,'Ship1Explosion'将变为真(从原来的假更改),这将绘制ship1的爆炸(我知道如何做到这一点) )以及将boolean Ship1更改为false,以便船不会继续重复绘制。

错误我

   error: int cannot be dereferenced
   int h = Ship1area.charAt(l);

如果有人能帮助我,我会喜欢帮助设置激光碰撞。如果我需要解释其他任何内容,请告诉我,谢谢。

代码:

import java.awt.*;
import java.applet.*;
import java.util.Iterator;
import java.util.ArrayList;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Galaga extends Applet implements KeyListener
{   
public static int x1 = 500;
public static int y1 = 400;
public static int bx = x1+73;
public static int by = y1+45;
public static int bx1 = x1+73;
public static int by1 = y1+100;
public static int by2 = by-40;
public static int by3 = by-50;
public static int x = 200;
public static int y = 0;
public static int j = 262;
public static int p;
public static int h;
public static int Ship1area;
public boolean keyUp;
public boolean keyDown;
public boolean keyLeft;
public boolean keyRight;
public boolean keySpace;
public boolean Laser = false;
public boolean Ship1Explosion = false;
public boolean Ship1 = true;

public void paint(Graphics g)
{
    DrawBackground(g);
    addKeyListener(this);
    this.DrawGalaga(g,x1,y1);
    DrawShip1(g);
    DrawLaser(g);
    DrawShip1Explosion(g);
}
public void DrawBackground(Graphics g)
{
    g.setColor(Color.black);
    g.fillRect(1,2,3000,4000);
    DrawStars(g);
}
public void DrawGalaga(Graphics g, int x1, int y1)
{       
        {
        g.setColor(Color.red);
        g.drawLine(x1+42,y1+75,x1+42,y1+80); //Far Left Gun
        g.drawLine(x1+43,y1+75,x1+43,y1+80);
        g.drawLine(x1+44,y1+75,x1+44,y1+80);
        g.drawLine(x1+45,y1+75,x1+45,y1+80);

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

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

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

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

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

        g.drawLine(x1+62,y1+70,x1+62,y1+85); //From Middle, 2nd Left one
        g.drawLine(x1+63,y1+70,x1+63,y1+85);
        g.drawLine(x1+64,y1+70,x1+64,y1+85);
        g.drawLine(x1+65,y1+70,x1+65,y1+85);
        g.drawLine(x1+66,y1+60,x1+66,y1+95); //From Middle, 1st Left one
        g.drawLine(x1+67,y1+60,x1+67,y1+95);
        g.drawLine(x1+68,y1+60,x1+68,y1+95);
        g.drawLine(x1+69,y1+60,x1+69,y1+95);

        g.drawLine(x1+70,y1+45,x1+70,y1+100); //Big Middle Piece
        g.drawLine(x1+71,y1+45,x1+71,y1+100);
        g.drawLine(x1+72,y1+45,x1+72,y1+100);
        g.drawLine(bx,by,bx1,by1);
        g.drawLine(x1+74,y1+45,x1+74,y1+100);
        g.drawLine(x1+75,y1+45,x1+75,y1+100);
        g.drawLine(x1+76,y1+45,x1+76,y1+100);

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

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

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

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

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

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

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

        g.setColor(Color.red);
        g.drawLine(x1+100,y1+75,x1+100,y1+80); //Far Right Gun
        g.drawLine(x1+101,y1+75,x1+101,y1+80);
        g.drawLine(x1+102,y1+75,x1+102,y1+80);
        g.drawLine(x1+103,y1+75,x1+103,y1+80);
    }
}
public void DrawShip1(Graphics g)
{
    int x = 0;
    int y = 0;
    if(Ship1)
    {
        g.setColor(Color.blue);
        Polygon Ship1 = new Polygon();
        Ship1.addPoint(x+268,y+190); //1
        Ship1.addPoint(x+270,y+190);
        Ship1.addPoint(x+270,y+185);
        Ship1.addPoint(x+275,y+185);
        Ship1.addPoint(x+275,y+155); //5
        Ship1.addPoint(x+278,y+155);
        Ship1.addPoint(x+278,y+150);
        Ship1.addPoint(x+285,y+150);
        Ship1.addPoint(x+285,y+165);
        Ship1.addPoint(x+293,y+165); //10
        Ship1.addPoint(x+293,y+150);
        Ship1.addPoint(x+301,y+150);
        Ship1.addPoint(x+301,y+165); 
        Ship1.addPoint(x+309,y+165);
        Ship1.addPoint(x+309,y+150); //15
        Ship1.addPoint(x+317,y+150);
        Ship1.addPoint(x+317,y+155);
        Ship1.addPoint(x+319,y+155);
        Ship1.addPoint(x+319,y+185);
        Ship1.addPoint(x+325,y+185); //20
        Ship1.addPoint(x+325,y+190);
        Ship1.addPoint(x+323,y+190);
        Ship1.addPoint(x+323,y+185);
        Ship1.addPoint(x+330,y+185);
        Ship1.addPoint(x+330,y+155); //25
        Ship1.addPoint(x+325,y+155);
        Ship1.addPoint(x+325,y+145);
        Ship1.addPoint(x+330,y+145);
        Ship1.addPoint(x+330,y+140);
        Ship1.addPoint(x+316,y+140); //30
        Ship1.addPoint(x+316,y+135);
        Ship1.addPoint(x+311,y+135);
        Ship1.addPoint(x+311,y+130);
        Ship1.addPoint(x+306,y+130);
        Ship1.addPoint(x+306,y+125); //35
        Ship1.addPoint(x+311,y+125);
        Ship1.addPoint(x+311,y+120);
        Ship1.addPoint(x+316,y+120);
        Ship1.addPoint(x+316,y+115);
        Ship1.addPoint(x+306,y+115); //40
        Ship1.addPoint(x+306,y+100);
        Ship1.addPoint(x+298,y+100);
        Ship1.addPoint(x+298,y+115);
        Ship1.addPoint(x+290,y+115);
        Ship1.addPoint(x+290,y+100); //45
        Ship1.addPoint(x+282,y+100);
        Ship1.addPoint(x+282,y+115);
        Ship1.addPoint(x+272,y+115);
        Ship1.addPoint(x+272,y+120);
        Ship1.addPoint(x+277,y+120); //50
        Ship1.addPoint(x+277,y+125);
        Ship1.addPoint(x+282,y+125);
        Ship1.addPoint(x+282,y+130);
        Ship1.addPoint(x+277,y+130);
        Ship1.addPoint(x+277,y+135); //55
        Ship1.addPoint(x+272,y+135);
        Ship1.addPoint(x+272,y+140);
        Ship1.addPoint(x+262,y+140);
        Ship1.addPoint(x+262,y+145);
        Ship1.addPoint(x+267,y+145); //60
        Ship1.addPoint(x+267,y+145);
        Ship1.addPoint(x+272,y+145);
        Ship1.addPoint(x+272,y+155);
        Ship1.addPoint(x+265,y+155);
        Ship1.addPoint(x+265,y+185); //61
        Ship1.addPoint(x+268,y+185);
        g.fillPolygon(Ship1);
        Polygon Ship1parts1 = new Polygon();
        g.setColor(Color.pink);
        Ship1parts1.addPoint(x+268,y+180);
        Ship1parts1.addPoint(x+272,y+180);
        Ship1parts1.addPoint(x+272,y+170);
        Ship1parts1.addPoint(x+270,y+170);
        Ship1parts1.addPoint(x+270,y+165);
        Ship1parts1.addPoint(x+268,y+165);
        Ship1parts1.addPoint(x+268,y+180);
        g.fillPolygon(Ship1parts1);
        Polygon Ship1parts2 = new Polygon();
        Ship1parts2.addPoint(x+330,y+180);
        Ship1parts2.addPoint(x+324,y+180);
        Ship1parts2.addPoint(x+324,y+170);
        Ship1parts2.addPoint(x+326,y+170);
        Ship1parts2.addPoint(x+326,y+165);
        Ship1parts2.addPoint(x+328,y+165);
        Ship1parts2.addPoint(x+328,y+180);
        g.fillPolygon(Ship1parts2);
        }
}
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 keyPressed(KeyEvent e)
{
    if(e.getKeyCode() == KeyEvent.VK_LEFT)
    {
        x1--;
        bx--;
        bx1--;
    }
    if(e.getKeyCode() == KeyEvent.VK_RIGHT)
    {
        x1++;
        bx++;
        bx1++;
    }
    if(e.getKeyCode() == KeyEvent.VK_UP)
    {
        y1--;
        by--;
        by1--;
    }
    if(e.getKeyCode() == KeyEvent.VK_DOWN)
    {
        y1++;
        by++;
        by1++;
    }
    if(e.getKeyCode() == KeyEvent.VK_SPACE)
    {
        Laser = true;
    }
        repaint();
}
public void DrawLaser(Graphics g)
{
    if (Laser)
    {
        g.setColor(Color.yellow);
        outerloop:
            for(int i=0;i<=1000; i+=5)
            {
                for(int j=262;j<=330;j+=1)
                {
                    ArrayList Ship1area = new ArrayList();
                    Ship1area.add(j);
                }
                g.drawLine(bx,by2,bx,by3);
                delay(1);
                repaint();
                g.setColor(Color.black);
                g.drawLine(bx,by2,bx,by3);
                by2--;
                by3--;
                by2--;
                by3--;
                by2--;
                by3--;
                g.setColor(Color.yellow);
                g.drawLine(bx,by2,bx,by3);
                if(by2 <= 0)
                    break outerloop;
                for (int l=0;l<=p;l++)
                    {
                    int h = Ship1area.charAt(l);
                    if(h == bx)
                        {   
                            Ship1Explosion = true;
                        }
                    }
        }
        by2 = by-60;
        by3 = by-70;
    }
}
public void DrawShip1Explosion(Graphics g)
{
    if (Ship1Explosion)
        {
            g.setColor(Color.red);
            g.drawLine(100,100,200,200);
        }
}
public void keyReleased(KeyEvent e)
{   
    if(e.getKeyCode() == KeyEvent.VK_SPACE)
    {
        Laser = false;
    }
    keyUp = keyDown = keyLeft = keyRight = keySpace = false;
    repaint();
}
public void keyTyped(KeyEvent e)
{
}   
public void delay (int n)
{
    long startDelay = System.currentTimeMillis();
    long endDelay=0;
    while (endDelay - startDelay < n)
    endDelay = System.currentTimeMillis();
}

}

1 个答案:

答案 0 :(得分:2)

您引用的这一行:

int h = ship1area.charAt(l);

指的是类级别的int值。您可能希望ArrayList方法中的for循环中具有相同名称(!)的drawLaser。在这种情况下,您需要:

int h = (int) ship1area.get(l);

ArrayList必须移出for到方法范围才能看到。即。

public void drawLaser(Graphics g) {

   ArrayList<String> ship1area = new ArrayList<>();
   ...

除此之外:鉴于Java命名约定,变量&amp;方法名称通常以小写字母开头。