Form1.cs的
namespace SpaceInvadersV3
{
public partial class Form1 : Form
{
public bool isPressed;
Shooter player;
List<Missile> bullet;
List<Enemy> pirate;
Boundary bottom;
Boundary top;
Boundary left;
Boundary right;
public Form1()
{
InitializeComponent();
player = new Shooter(450,460);
bullet = new List<Missile>();
pirate = new List<Enemy>();
for (int i = 0; i < 10; i++)
{
Enemy temp = new Enemy();
pirate.Add(temp);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
player.Move();
foreach (Missile b in bullet)
{
b.Move();
}
foreach (Enemy p in pirate)
{
p.Move();
}
pictureBox1.Invalidate();
if (IsColliding(player, pirate) == true)
{
gameOver();
}
}
"pirate"
中的 错误表示无法从'System.Collections.Generic.List<SpaceInvadersV3.Enemy>'
转换为'SpaceInvadersV3.Enemy'
我尝试更改'IsColliding'
下面的(Enemy b)
函数} (List<Enemy> b)
然后它无法识别b.Bottom
,并说List<Enemy>
不包含Bottom的定义。
// Keybinds
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
{
player.goleft = true;
}
if (e.KeyCode == Keys.D)
{
player.goright = true;
}
if (e.KeyCode == Keys.W)
{
player.goup = true;
}
if (e.KeyCode == Keys.S)
{
player.godown = true;
}
if (e.KeyCode == Keys.Space)
{
Missile temp = new Missile(player.x, player.y);
bullet.Add(temp);
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
{
player.goleft = false;
}
if (e.KeyCode == Keys.D)
{
player.goright = false;
}
if (e.KeyCode == Keys.W)
{
player.goup = false;
}
if (e.KeyCode == Keys.S)
{
player.godown = false;
}
}
// keybinds
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
player.Draw(e.Graphics);
foreach (Missile b in bullet)
{
b.Draw(e.Graphics);
}
foreach (Enemy p in pirate)
{
p.Draw(e.Graphics);
}
}
private bool IsColliding(Shooter a, Enemy b)
{
bool colliding = true; // presume collision
if (a.Top() > b.Bottom())
{
colliding = false;
}
return colliding;
}
private void gameOver()
{
timer1.Stop();
MessageBox.Show("you died");
}
}
}
Box.cs,其中Enemy和Shooter类都继承自
using System.Drawing;
namespace SpaceInvadersV3
{
class Box
{
public Image pic;
public float x;
public float y;
public float speed;
public Box()
{
x = 0;
y = 0;
speed = 0;
}
// Image Resizing Code
public static Image resizeImage(Image imgToResize, Size size)
{
return (Image)(new Bitmap(imgToResize, size));
}
// image resizing code
public void Draw(Graphics g)
{
g.DrawImage(pic, x, y);
}
public float Width()
{
return pic.Width;
}
public float Height()
{
return pic.Height;
}
public float Left()
{
return x;
}
public float Right()
{
return x + Width();
}
public float Top()
{
return y;
}
public float Bottom()
{
return y + Height();
}
}
}
我不认为Shooter和Enemy课程是否真的相关,但如果你需要它们,我会发布它们。谢谢你的帮助。
答案 0 :(得分:4)
if (IsColliding(player, pirate) == true)
首先,永远不要写那个。如果确实这些正在发生碰撞&#34;那看起来很业余。说&#34;如果这些事情发生冲突&#34;:
if (IsColliding(player, pirate))
同样,更喜欢if (!whatever)
到if (whatever == false)
。
其次,请使用复数名词收集。那应该是pirates
,而不是pirate
。你想强调它有一组给读者。
&#34;盗版错误&#34;说它无法转换为&#39; List&#39;到了敌人&#39;
你的IsColliding
需要一名射手和一名敌人,但是你正在给它一个射手和敌人名单。 IsColliding
并不知道如何处理。
您已经知道如何修复它。你想要移动每个敌人,所以你写道:
foreach (Enemy p in pirate)
{
p.Move();
}
现在你要检查每个敌人的碰撞,所以做同样的事情:
foreach (Enemy p in pirate)
{
if (IsColliding(player, p)) { ... }
}
您最终将学习的高级技术是在序列上使用查询理解:
var collisions = from p in pirate
where IsColliding(player, p)
select p;
foreach (Enemy p in collisions)
{
... handle the collision...
}
但是在你试图跑步之前学会走路。