/// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code here spriteBatch.Begin(); spriteBatch.Draw(backgroundTexture2D, backgroundRectangle, Color.White); rightPaddle.Draw(spriteBatch); leftPaddle.Draw(spriteBatch); LeftScore.Draw(spriteBatch); RightScore.Draw(spriteBatch); ball.DrawBall(spriteBatch); if (ball.isDestroyed) { ball.DrawExplosion(spriteBatch); } // leftPaddle.DrawFragments(spriteBatch); spriteBatch.End(); base.Draw(gameTime); }
void CheckCollisions(GameTime gameTime) { /* if (ball.CheckCollisonWithPaddle(leftPaddle.body)) * { * leftPaddle.Hit(); * } * leftPaddle.CheckIfHitted(gameTime); * * if (ball.CheckCollisonWithPaddle(rightPaddle.body)) * { * rightPaddle.Hit(); * }*/ if (leftPaddle.CheckCollisionWithBall(ball)) { leftPaddle.Hit(); } else if (rightPaddle.CheckCollisionWithBall(ball)) { rightPaddle.Hit(); } else { if (ball.CheckCollisionWithWall(0, GraphicsDevice.Viewport.Width) == 'l') { RightScore.AddPoint(); ball.DestroyBall(gameTime, GraphicsDevice.Viewport.Width); play = false; } else if (ball.CheckCollisionWithWall(0, GraphicsDevice.Viewport.Width) == 'r') { LeftScore.AddPoint(); ball.DestroyBall(gameTime, GraphicsDevice.Viewport.Width); play = false; } } rightPaddle.CheckIfHitted(gameTime); ball.BounceFromWall(GraphicsDevice.Viewport.Height); }
void OnCollisionEnter2D(Collision2D col) { if (rs == 21) { TextMesh t = (TextMesh)Winner.GetComponent(typeof(TextMesh)); t.text = "Winner Right"; } else if (ls == 21) { TextMesh t = (TextMesh)Winner.GetComponent(typeof(TextMesh)); t.text = "Winner Left"; } else { if (col.gameObject.name == "RacketLeft") { float y = hitFactor(transform.position, col.transform.position, col.collider.bounds.size.y); ls++; TextMesh t = (TextMesh)LeftScore.GetComponent(typeof(TextMesh)); t.text = "Left Score : " + ls.ToString(); Vector2 dir = new Vector2(1, y).normalized; GetComponent <Rigidbody2D>().velocity = dir * speed; } if (col.gameObject.name == "RacketRight") { float y = hitFactor(transform.position, col.transform.position, col.collider.bounds.size.y); rs++; TextMesh t = (TextMesh)RightScore.GetComponent(typeof(TextMesh)); t.text = "Right Score : " + rs.ToString(); Vector2 dir = new Vector2(-1, y).normalized; GetComponent <Rigidbody2D>().velocity = dir * speed; } } }
/// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.Black); spriteBatch.Begin(); switch (State) { case GameStates.Playing: LeftPaddle.Draw(spriteBatch); RightPaddle.Draw(spriteBatch); foreach (var ball in Balls) { ball.Draw(spriteBatch); } break; case GameStates.Paused: break; case GameStates.RightWin: spriteBatch.DrawString(Game1.sf, "Right Wins ", new Vector2(3, 0), Color.White); break; case GameStates.LeftWin: spriteBatch.DrawString(Game1.sf, "Leftt Wins ", new Vector2(3, 0), Color.White); break; default: break; } spriteBatch.DrawString(Game1.sf, "Left: " + LeftScore.ToString(), new Vector2(9, 0), Color.White); spriteBatch.DrawString(Game1.sf, "Right: " + RightScore.ToString(), new Vector2(600, 0), Color.White); spriteBatch.End(); base.Draw(gameTime); }