我认为您的问题是您没有为球员和敌人使用好的OO设计。创建两个类:
public class Player{ int X; int Y; int Width; int Height; // Getters and Setters}public class Enemy{ int X; int Y; int Width; int Height; // Getters and Setters}您的播放器应具有X,Y,Width和Height变量。
您的敌人也应该如此。
在游戏循环中,执行以下操作(C#):
foreach (Enemy e in EnemyCollection){ Rectangle r = new Rectangle(e.X,e.Y,e.Width,e.Height); Rectangle p = new Rectangle(player.X,player.Y,player.Width,player.Height); // Assuming there is an intersect method, otherwise just handcompare the values if (r.Intersects(p)) { // A Collision! // we know which enemy (e), so we can call e.DoCollision(); e.DoCollision(); }}为了加快速度,请不要费心检查敌人的坐标是否在屏幕外。



