コード例 #1
0
ファイル: BallEntity.cs プロジェクト: Qub1/Pong
        public override void Collision(SpriteEntity entity)
        {
            if (entity is PaddleEntity) {
                if(((PaddleEntity)entity).IsAlive()) {
                    //Rectangle overlap = Rectangle.Intersect(new Rectangle((int)Location.X, (int)Location.Y, Sprite.Width, Sprite.Height), new Rectangle((int)entity.Location.X, (int)entity.Location.Y, entity.Sprite.Width, entity.Sprite.Height));

                    Velocity = new Vector2(-1 * Velocity.X, Velocity.Y);
                    float relativeIntersect;
                    float normalizedIntersect;
                    float angle = 0F;
                    if (entity == ((PongWorld)World).Players[0] || entity == ((PongWorld)World).Players[1]) {
                        relativeIntersect = (Location.Y + Sprite.Height / 2) - (entity.Location.Y - Sprite.Height / 2 + 1);
                        normalizedIntersect = relativeIntersect / (entity.Sprite.Height + Sprite.Height - 2);

                        if (entity == ((PongWorld)World).Players[0]) {
                            angle = 20 + normalizedIntersect * 140;
                        } else if (entity == ((PongWorld)World).Players[1]) {
                            angle = 340 - normalizedIntersect * 140;
                        }
                    } else {
                        relativeIntersect = (Location.X + Sprite.Width / 2) - (entity.Location.X - Sprite.Width / 2 + 1);
                        normalizedIntersect = relativeIntersect / (entity.Sprite.Width + Sprite.Width - 2);

                        if (entity == ((PongWorld)World).Players[2]) {
                            angle = 230 - normalizedIntersect * 140;
                        } else if (entity == ((PongWorld)World).Players[3]) {
                            angle = 290 + normalizedIntersect * 140;
                        }
                    }
                    SetVelocityAtAngle((int)angle, VelocityMultiplier * 0.6F);

                    Tint = entity.Tint;

                    VelocityMultiplier = MathHelper.Clamp(VelocityMultiplier * 1.05F, -2.2F, 2.2F);
                }
            }
        }
コード例 #2
0
ファイル: SpriteEntity.cs プロジェクト: Qub1/Pong
 public virtual void Collision(SpriteEntity entity)
 {
 }
コード例 #3
0
ファイル: PongWorld.cs プロジェクト: Qub1/Pong
        public PongWorld(Game game, Rectangle size, ContentManager contentManager)
            : base(game, null, size)
        {
            // Create FPS counter
            FPSCounter = new TextEntity(this, new Vector2(0, Size.Height - 15), "0", contentManager.Load<SpriteFont>("LucidaConsole"), Color.Lime);
            Entities.Add(FPSCounter);

            // Net
            ball = contentManager.Load<Texture2D>("ball");
            int netHeight = 5;
            while (netHeight < Size.Height) {
                SpriteEntity dot = new SpriteEntity(this, new Vector2((Size.Width - ball.Width) / 2, netHeight), ball);
                Entities.Add(dot);
                netHeight += dot.Sprite.Height + 5;
            }

            // Create ball
            Ball = new BallEntity(this, Vector2.Zero, ball);
            Ball.Velocity = new Vector2(0.5F, 0.5F);
            Entities.Add(Ball);
            ResetBall();

            // Player 1
            Texture2D paddle = contentManager.Load<Texture2D>("paddle");
            PaddleEntity player1 = new PaddleEntity(this, Vector2.Zero, paddle, Keys.W, Keys.S, Keys.None, Keys.None);
            player1.Tint = Color.Blue;
            Entities.Add(player1);
            Players.Add(player1);

            // Player 2
            PaddleEntity player2 = new PaddleEntity(this, Vector2.Zero, paddle, Keys.Up, Keys.Down, Keys.None, Keys.None);
            player2.Tint = Color.Red;
            Entities.Add(player2);
            Players.Add(player2);
            player2.IsAI = true;

            // Player 3
            Texture2D paddle2 = contentManager.Load<Texture2D>("paddle2");
            PaddleEntity player3 = new PaddleEntity(this, Vector2.Zero, paddle2, Keys.None, Keys.None, Keys.H, Keys.K);
            player3.Tint = Color.Yellow;
            Entities.Add(player3);
            Players.Add(player3);
            player3.IsAI = true;

            // Player 4
            PaddleEntity player4 = new PaddleEntity(this, Vector2.Zero, paddle2, Keys.None, Keys.None, Keys.NumPad4, Keys.NumPad6);
            player4.Tint = Color.Green;
            Entities.Add(player4);
            Players.Add(player4);
            player4.IsAI = true;

            // Borders
            LineEntity topLine = new LineEntity(this, new Vector2(30, 26), new Vector2(Size.Width - 30, 26), Color.White, 2);
            Entities.Add(topLine);
            LineEntity bottomLine = new LineEntity(this, new Vector2(30, Size.Height - 29), new Vector2(Size.Width - 30, Size.Height - 29), Color.White, 2);
            Entities.Add(bottomLine);
            LineEntity leftLine = new LineEntity(this, new Vector2(30, 26), new Vector2(30, Size.Height - 27), Color.White, 2);
            Entities.Add(leftLine);
            LineEntity rightLine = new LineEntity(this, new Vector2(Size.Width - 28, 26), new Vector2(Size.Width - 28, Size.Height - 27), Color.White, 2);
            Entities.Add(rightLine);

            // Create menu
            MenuWorld = new MenuWorld(Game, this, Size, contentManager);
            MenuWorld.DoUpdate = false;
            MenuWorld.DoDraw = false;
            Worlds.Add(MenuWorld);

            // Create overlay
            Overlay = new TextEntity(this, new Vector2(Size.Width / 2, Size.Height / 2), "", contentManager.Load<SpriteFont>("QuartzMS"), Color.Red);
            Overlay.BackgroundColor = Color.Black;
            Entities.Add(Overlay);

            // Set starting state
            Reset();
            IsFrozen = true;
            SetOverlayText("Welcome to pong!\n\nTo start a new round, press [SPACE].\nPause using [ESC].\nPlayer 1 up: [W]\nPlayer 1 down: [S]\nPlayer 2 up: [UP]\nPlayer 2 down: [DOWN]\nPlayer 3 left: [H]\nPlayer 3 right: [K]\nPlayer 4 left: [NUM4]\nPlayer 4 right: [NUM6]\n\nEnable players by clicking their names in the menu.\nChange their difficulty and AI status by clicking the corresponding buttons.");
        }