Advertisement
rakoczyn

BallThrow

Mar 31st, 2012
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11.  
  12. namespace BallThrow
  13. {
  14.     /// <summary>
  15.     /// This is the main type for your game
  16.     /// </summary>
  17.     public class Game1 : Microsoft.Xna.Framework.Game
  18.     {
  19.         GraphicsDeviceManager graphics;
  20.         SpriteBatch spriteBatch;
  21.         GraphicsDevice device;
  22.  
  23.  
  24.        
  25.         public Game1()
  26.         {
  27.  
  28.             graphics = new GraphicsDeviceManager(this);
  29.            
  30.  
  31.             Content.RootDirectory = "Content";
  32.  
  33.            
  34.         }
  35.  
  36.      
  37.  
  38.         int screenWidth;
  39.         int screenHeight;
  40.  
  41.         MouseState mouseStateCurrent, mouseStatePrevious;
  42.  
  43.         private Vector2 ballPosition;
  44.         private Vector2 ballMovement;
  45.         private int ballSize;
  46.         private Vector2 gravity;
  47.         private float ballRotation;
  48.         private float ballRevolution;
  49.         private const float maxVerticalSpeed = 12;
  50.  
  51.         private Texture2D ballTexture;
  52.         private Texture2D backgroundTexture;
  53.        
  54.  
  55.         /// <summary>
  56.         /// Allows the game to perform any initialization it needs to before starting to run.
  57.         /// This is where it can query for any required services and load any non-graphic
  58.         /// related content.  Calling base.Initialize will enumerate through any components
  59.         /// and initialize them as well.
  60.         /// </summary>
  61.         protected override void Initialize()
  62.         {
  63.             graphics.PreferredBackBufferWidth = 600;
  64.             graphics.PreferredBackBufferHeight = 600;
  65.             graphics.IsFullScreen = false;
  66.             graphics.ApplyChanges();
  67.             Window.Title = "BallThrow";
  68.  
  69.             IsMouseVisible = true;
  70.  
  71.             ballPosition = new Vector2(0, 0);
  72.             ballMovement = new Vector2(0, 0);
  73.             ballSize = 80;
  74.             ballRotation = 0.0f;
  75.             ballRevolution = 0.0f;
  76.  
  77.             gravity = new Vector2(0, 0.125f);
  78.  
  79.             base.Initialize();
  80.         }
  81.  
  82.         /// <summary>
  83.         /// LoadContent will be called once per game and is the place to load
  84.         /// all of your content.
  85.         /// </summary>
  86.         protected override void LoadContent()
  87.         {
  88.             // Create a new SpriteBatch, which can be used to draw textures.
  89.             spriteBatch = new SpriteBatch(GraphicsDevice);
  90.             device = graphics.GraphicsDevice;
  91.  
  92.             screenWidth = device.PresentationParameters.BackBufferWidth;
  93.             screenHeight = device.PresentationParameters.BackBufferHeight;
  94.  
  95.             ballTexture = Content.Load<Texture2D>("BallCL");
  96.             backgroundTexture = Content.Load<Texture2D>("TurfBackground");
  97.  
  98.            
  99.         }
  100.  
  101.         /// <summary>
  102.         /// UnloadContent will be called once per game and is the place to unload
  103.         /// all content.
  104.         /// </summary>
  105.         protected override void UnloadContent()
  106.         {
  107.             // TODO: Unload any non ContentManager content here
  108.         }
  109.  
  110.         /// <summary>
  111.         /// Allows the game to run logic such as updating the world,
  112.         /// checking for collisions, gathering input, and playing audio.
  113.         /// </summary>
  114.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  115.         protected override void Update(GameTime gameTime)
  116.         {
  117.             // Allows the game to exit
  118.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  119.                 this.Exit();
  120.  
  121.             mouseStateCurrent = Mouse.GetState();
  122.  
  123.             Vector2 mouseClickPos;
  124.             if (mouseStateCurrent.LeftButton == ButtonState.Released && mouseStatePrevious.LeftButton == ButtonState.Pressed)
  125.             {
  126.                 mouseClickPos = new Vector2(mouseStateCurrent.X, mouseStateCurrent.Y);
  127.                 ballMovement += Vector2.Multiply(Vector2.Subtract(mouseClickPos,ballPosition),0.1f);
  128.             }
  129.  
  130.             if (mouseStateCurrent.RightButton == ButtonState.Pressed && mouseStatePrevious.RightButton == ButtonState.Pressed)
  131.                 if ((mouseStateCurrent.X >= ballPosition.X - 25 && mouseStateCurrent.X <= ballPosition.X + 25)
  132.                     && (mouseStateCurrent.Y >= ballPosition.Y - 25 && mouseStateCurrent.Y <= ballPosition.Y + 25))
  133.                 {
  134.                     ballPosition = new Vector2(mouseStateCurrent.X, mouseStateCurrent.Y);
  135.                     ballMovement = Vector2.Zero;
  136.                     ballRevolution = 0.0f;
  137.                 }
  138.  
  139.             if (mouseStateCurrent.RightButton == ButtonState.Released && mouseStatePrevious.RightButton == ButtonState.Pressed)
  140.                 if ((mouseStatePrevious.X >= ballPosition.X - 25 && mouseStatePrevious.X <= ballPosition.X + 25)
  141.                     && (mouseStatePrevious.Y >= ballPosition.Y - 25 && mouseStatePrevious.Y <= ballPosition.Y + 25))
  142.                 {
  143.                     ballMovement = Vector2.Multiply(new Vector2(mouseStateCurrent.X - mouseStatePrevious.X, mouseStateCurrent.Y - mouseStatePrevious.Y),1f);
  144.                     ballRevolution = 0.0f;
  145.                 }
  146.  
  147.             ProcessBallPosition();
  148.  
  149.             mouseStatePrevious = mouseStateCurrent;
  150.  
  151.             // TODO: Add your update logic here
  152.  
  153.             base.Update(gameTime);
  154.         }
  155.  
  156.        
  157.  
  158.         /// <summary>
  159.         /// This is called when the game should draw itself.
  160.         /// </summary>
  161.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  162.         protected override void Draw(GameTime gameTime)
  163.         {
  164.             GraphicsDevice.Clear(Color.Black);
  165.  
  166.             spriteBatch.Begin();
  167.  
  168.  
  169.                 DrawBackground();
  170.                 DrawBall();
  171.  
  172.  
  173.             spriteBatch.End();
  174.  
  175.             base.Draw(gameTime);
  176.         }
  177.  
  178.         private void DrawBackground()
  179.         {
  180.             Rectangle screenRectangle = new Rectangle(0, 0, screenWidth, screenHeight);
  181.             spriteBatch.Draw(backgroundTexture, screenRectangle, Color.White);
  182.            
  183.         }
  184.  
  185.         private void DrawBall()
  186.         {
  187.            
  188.             Rectangle ballRectangle = new Rectangle((int)ballPosition.X, (int)ballPosition.Y, ballSize, ballSize);
  189.             spriteBatch.Draw(ballTexture, ballRectangle, null, Color.White, ballRotation, new Vector2(50), SpriteEffects.None, 0);
  190.         }
  191.  
  192.         private void DrawBallShadow()
  193.         {
  194.             Rectangle ballShadowRectangle = new Rectangle((int)ballPosition.X, (int)ballPosition.Y, ballSize, ballSize);
  195.             Vector2 shadowV = new Vector2();
  196.             shadowV.X = -(screenWidth - ballPosition.Y) * 0.5f;
  197.             shadowV.Y = (screenHeight - ballPosition.Y) * 0.3f;
  198.             spriteBatch.Draw(ballTexture, ballShadowRectangle, null, new Color(0,0,0,180), 0.0f, shadowV, SpriteEffects.None, 1);
  199.         }
  200.  
  201.         private void ProcessBallPosition()
  202.         {
  203.             ballRotation += ballRevolution;
  204.             ballRevolution = MathHelper.SmoothStep(ballRevolution, 0, 0.05f);
  205.  
  206.             ballMovement += gravity;
  207.  
  208.  
  209.             if (ballMovement.Y > maxVerticalSpeed) ballMovement.Y = maxVerticalSpeed;
  210.             if (ballMovement.Y < -maxVerticalSpeed) ballMovement.Y = -maxVerticalSpeed;
  211.             ballPosition += ballMovement;
  212.  
  213.             //ballMovement = new Vector2(ballMovement.X * 0.8f, ballMovement.Y);
  214.             float smoothing = Math.Abs(ballPosition.Y - (screenHeight - ballSize/2)) < 0.1 ? 0.12f : 0.01f;
  215.             ballMovement = Vector2.SmoothStep(ballMovement, new Vector2(0, ballMovement.Y), smoothing);
  216.             CheckBoundaries();
  217.  
  218.             if (Math.Abs(ballMovement.X) < 0.01)
  219.                 ballMovement.X = 0.00f;
  220.             if ((Math.Abs(ballMovement.Y) < 0.05) && (Math.Abs(ballPosition.Y - (screenHeight - ballSize / 2)) < 0.1))
  221.                 ballMovement.Y = 0.00f;
  222.         }
  223.  
  224.         private void CheckBoundaries()
  225.         {
  226.             float angleRate = 0.05f;
  227.  
  228.             // CHECK BOTTOM
  229.             if ((ballPosition.Y >= screenHeight - ballSize / 2) && (ballMovement.Y > 0))
  230.             {
  231.                 ballMovement = new Vector2(ballMovement.X, -(ballMovement.Y * 0.75f));
  232.                 ballRevolution = ballMovement.X * angleRate;
  233.             }
  234.  
  235.             if (ballPosition.Y > screenHeight - ballSize / 2) ballPosition.Y = screenHeight - ballSize / 2;
  236.  
  237.             // CHECK TOP
  238.             if ((ballPosition.Y <= ballSize / 2) && (ballMovement.Y < 0))
  239.             {
  240.                 ballMovement = new Vector2(ballMovement.X, -(ballMovement.Y));
  241.                 ballRevolution = -ballMovement.X * angleRate;
  242.             }
  243.             if (ballPosition.Y < ballSize / 2) ballPosition.Y = ballSize / 2;
  244.  
  245.             // CHECK LEFT
  246.             if ((ballPosition.X <= ballSize / 2) && (ballMovement.X < 0))
  247.             {
  248.                 ballMovement = new Vector2(-ballMovement.X * 0.75f, ballMovement.Y);
  249.                 ballRevolution = ballMovement.Y * angleRate;
  250.             }
  251.  
  252.             if (ballPosition.X < ballSize / 2) ballPosition.X = ballSize / 2;
  253.  
  254.             // CHECK RIGHT
  255.             if ((ballPosition.X >= screenWidth - ballSize / 2) && (ballMovement.X > 0))
  256.             {
  257.                 ballMovement = new Vector2(-ballMovement.X * 0.75f, ballMovement.Y);
  258.                 ballRevolution = -ballMovement.Y * angleRate;
  259.             }
  260.             if (ballPosition.X > screenWidth - ballSize / 2) ballPosition.X = screenWidth - ballSize / 2;
  261.            
  262.         }
  263.     }
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement