private void OnCollisionEnter2D(Collision2D other) { BallEntity ball = other.gameObject.GetComponent <BallEntity>(); if (ball != null) { // Increase score settings.scores[player] += 1; // Reset position and speed ball.SetSpeed(5f); ball.gameObject.transform.position = Vector3.zero; } }
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."); }