-2

私は少し前に学校でコンピュータサイエンスの課題のためにこのギャラガプログラムを始めましたが、今日は一生懸命取り組んでいました. (はい、私はそれが最善ではないことを知っていますが、今のところは動作します。) ゲームの次の問題は衝突です. 私の現在の考えは、敵船ごとに 1 つずつ、複数の配列を持つことです。配列には x と y の調整が含まれるため、レーザーが配列にリストされているポイントである場合、「Ship1Explosion」は true になり (元の false から変更)、ship1 の爆発も描画されます (方法はわかっています)。ブール値の 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();
}

}

4

1 に答える 1

2

あなたが参照するこの行:

int h = ship1area.charAt(l);

intクラスレベルの値を参照しています。おそらく、メソッドのループでArrayList同じ名前 (!) が必要です。その場合、次のものが必要になります。fordrawLaser

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

を表示するには、メソッドのスコープの外にArrayList移動する必要があります。forすなわち

public void drawLaser(Graphics g) {

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

余談: Java の命名規則では、変数とメソッドの名前は通常、小文字で始まります。

于 2013-03-15T03:01:01.903 に答える