Advertisement
rohits134

Ball.cs

Jul 5th, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.74 KB | None | 0 0
  1. namespace Pong_Clone
  2. {
  3.     using System;
  4.     using Microsoft.Xna.Framework;
  5.     using Microsoft.Xna.Framework.Content;
  6.     using Microsoft.Xna.Framework.Graphics;
  7.  
  8.     public class Ball
  9.     {
  10.         private bool isVisible;
  11.         private Vector2 position;
  12.         private double direction;
  13.         private Texture2D texture;
  14.         private Rectangle size;
  15.         private float speed;
  16.         private float moveSpeed;
  17.         private Vector2 resetPos;
  18.         Random rand;
  19.  
  20.         public Ball(ContentManager content, Vector2 screenSize)
  21.         {
  22.             moveSpeed = 8f;
  23.             speed = 0;
  24.             texture = content.Load<Texture2D>("ball_thumb");
  25.             direction = 0;
  26.             size = new Rectangle(0, 0, texture.Width, texture.Height);
  27.             resetPos = new Vector2(screenSize.X / 2, screenSize.Y / 2);
  28.             position = resetPos;
  29.             rand = new Random();
  30.             isVisible = false;
  31.         }
  32.  
  33.         private void CheckWallHit()
  34.         {
  35.             while (direction > 2 * Math.PI) direction -= 2 * Math.PI;
  36.             while (direction < 0) direction += 2 * Math.PI;
  37.             if (position.Y <= 0 || (position.Y > resetPos.Y * 2 - size.Height))
  38.             {
  39.                 direction = 2 * Math.PI - direction;
  40.             }
  41.         }
  42.  
  43.         public Rectangle GetSize()
  44.         {
  45.             return size;
  46.         }
  47.  
  48.         public double GetDirection()
  49.         {
  50.             return direction;
  51.         }
  52.         public void Stop()
  53.         {
  54.             isVisible = false;
  55.             speed = 0;
  56.         }
  57.  
  58.         public void IncreaseSpeed()
  59.         {
  60.             moveSpeed += 0.7f;
  61.         }
  62.  
  63.         public Vector2 GetPosition()
  64.         {
  65.             return position;
  66.         }
  67.  
  68.         public void Reset(bool left)
  69.         {
  70.             if (left) direction = 0;
  71.             else direction = Math.PI;
  72.             position = resetPos;
  73.             isVisible = true;
  74.             speed = moveSpeed;
  75.             if (rand.Next(2) == 0)
  76.             {
  77.                 direction += MathHelper.ToRadians(rand.Next(30));
  78.             }
  79.             else
  80.             {
  81.                 direction -= MathHelper.ToRadians(rand.Next(30));
  82.             }
  83.         }
  84.  
  85.         public void BatHit(int block)
  86.         {
  87.             if (direction > Math.PI * 1.5f || direction < Math.PI * 0.5f)
  88.             {
  89.                 switch (block)
  90.                 {
  91.                     case 1:
  92.                         direction = MathHelper.ToRadians(220);
  93.                         break;
  94.                     case 2:
  95.                         direction = MathHelper.ToRadians(215);
  96.                         break;
  97.                     case 3:
  98.                         direction = MathHelper.ToRadians(200);
  99.                         break;
  100.                     case 4:
  101.                         direction = MathHelper.ToRadians(195);
  102.                         break;
  103.                     case 5:
  104.                         direction = MathHelper.ToRadians(180);
  105.                         break;
  106.                     case 6:
  107.                         direction = MathHelper.ToRadians(180);
  108.                         break;
  109.                     case 7:
  110.                         direction = MathHelper.ToRadians(165);
  111.                         break;
  112.                     case 8:
  113.                         direction = MathHelper.ToRadians(130);
  114.                         break;
  115.                     case 9:
  116.                         direction = MathHelper.ToRadians(115);
  117.                         break;
  118.                     case 10:
  119.                         direction = MathHelper.ToRadians(110);
  120.                         break;
  121.                 }
  122.             }
  123.             else
  124.             {
  125.                 switch (block)
  126.                 {
  127.                     case 1:
  128.                         direction = MathHelper.ToRadians(290);
  129.                         break;
  130.                     case 2:
  131.                         direction = MathHelper.ToRadians(295);
  132.                         break;
  133.                     case 3:
  134.                         direction = MathHelper.ToRadians(310);
  135.                         break;
  136.                     case 4:
  137.                         direction = MathHelper.ToRadians(345);
  138.                         break;
  139.                     case 5:
  140.                         direction = MathHelper.ToRadians(0);
  141.                         break;
  142.                     case 6:
  143.                         direction = MathHelper.ToRadians(0);
  144.                         break;
  145.                     case 7:
  146.                         direction = MathHelper.ToRadians(15);
  147.                         break;
  148.                     case 8:
  149.                         direction = MathHelper.ToRadians(50);
  150.                         break;
  151.                     case 9:
  152.                         direction = MathHelper.ToRadians(65);
  153.                         break;
  154.                     case 10:
  155.                         direction = MathHelper.ToRadians(70);
  156.                         break;
  157.                 }
  158.             }
  159.             if (rand.Next(2) == 0)
  160.             {
  161.                 direction += MathHelper.ToRadians(rand.Next(3));
  162.             }
  163.             else
  164.             {
  165.                 direction -= MathHelper.ToRadians(rand.Next(3));
  166.             }
  167.         }
  168.  
  169.  
  170.  
  171.         public void UpdatePosition()
  172.         {
  173.             size.X = (int)position.X;
  174.             size.Y = (int)position.Y;
  175.             position.X += speed * (float)Math.Cos(direction);
  176.             position.Y += speed * (float)Math.Sin(direction);
  177.             CheckWallHit();
  178.         }
  179.  
  180.         public void Draw(SpriteBatch batch)
  181.         {
  182.             if (isVisible)
  183.             {
  184.                 batch.Draw(texture, position, Color.White);
  185.             }
  186.         }
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement