2
我正在開發一個太空入侵者遊戲,並且我是這個遊戲編程的新手。如何從左向右移動入侵者
我需要侵略者從左向右移動。
我有4行pictureboxes,每行10個入侵者。
我遇到的問題是隻有1行正在移動。
請幫忙!
謝謝
private void Form1_Load(object sender, EventArgs e)
{
Cursor.Dispose();
objsp.gsImage = Image.FromFile(@"C:\Users\kuven\Desktop\SpaceInvader\SpaceInvader\Space Shooter.png");
objsp.gsImage = Resized(objsp.gsImage, 2);
objsp.gsPos = new Point(660, 650);
Cursor.Hide();
Cursor.Position = new Point(660 + objsp.gsImage.Width/2, 650 + objsp.gsImage.Height/2);
Cursor.Clip = new Rectangle(this.Location, this.Size);
objsp.invader = new PictureBox[invaderrow,invadercol];
for(int r = 0; r <= invaderrow-1; r++)
{
for(int c = 0; c<=invadercol-1; c++)
{
objsp.invader[r,c] = new PictureBox();
objsp.invader[r,c].Image= pictureBox2.Image;
objsp.invader[r,c].Image = Resized(pictureBox2.Image, 2);
objsp.invader[r,c].BackColor = Color.Transparent;
objsp.invader[r,c].Location= new Point((c * 100) + 10, 10 + r * 50);
this.Controls.Add(objsp.invader[r,c]);
}
}
invadermove.Enabled = true;
}
//移動侵略者
private void invadermove_Tick(object sender, EventArgs e)
{
for (int r = 0; r <= invaderrow-1 ; r++)
{
for (int c = 0; c <= invadercol - 1; c++)
{
if (level == 0)
dir = "r";
if (objsp.invader[r,9].Left >= ClientSize.Width)
dir = "l";
if (dir == "r")
{
if (c < 9)
{
objsp.invader[r,c].Location = new Point(objsp.invader[r,c].Left + 10, level * 5 + 25);
}
if (c > 8)
{
objsp.invader[r,c].Location = new Point(objsp.invader[r,c].Left + 10, level * 5 + 61);
if (objsp.invader[r,9].Left >= ClientSize.Width)
{
level += 1;
}
}
}
if (dir == "l")
{
if (c < 9)
{
objsp.invader[r,c].Location = new Point(objsp.invader[r,c].Left - 10, level * 5 + 25);
}
if (c > 8)
{
objsp.invader[r,c].Location = new Point(objsp.invader[r,c].Left - 10, level * 5 + 61);
if (objsp.invader[r,0].Left <=0)
{
dir = "r";
level += 1;
}
}
}
}
}
}
一個二維數組是這樣的:'objsp.invader [r,c]' –
@kendfrey感謝您指出這一點,實際上我之前從未在C#中使用過多維數組,所以對語法不太確定。我會在我的回答中糾正。 – akoso
我已經按照您的建議編輯了代碼,但它的確做了同樣的事情。只有現在它刪除了所有其他侵入者行,並且只移動最後一行。 –