Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- namespace BallThrow
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- GraphicsDevice device;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- }
- int screenWidth;
- int screenHeight;
- MouseState mouseStateCurrent, mouseStatePrevious;
- private Vector2 ballPosition;
- private Vector2 ballMovement;
- private int ballSize;
- private Vector2 gravity;
- private float ballRotation;
- private float ballRevolution;
- private const float maxVerticalSpeed = 12;
- private Texture2D ballTexture;
- private Texture2D backgroundTexture;
- /// <summary>
- /// Allows the game to perform any initialization it needs to before starting to run.
- /// This is where it can query for any required services and load any non-graphic
- /// related content. Calling base.Initialize will enumerate through any components
- /// and initialize them as well.
- /// </summary>
- protected override void Initialize()
- {
- graphics.PreferredBackBufferWidth = 600;
- graphics.PreferredBackBufferHeight = 600;
- graphics.IsFullScreen = false;
- graphics.ApplyChanges();
- Window.Title = "BallThrow";
- IsMouseVisible = true;
- ballPosition = new Vector2(0, 0);
- ballMovement = new Vector2(0, 0);
- ballSize = 80;
- ballRotation = 0.0f;
- ballRevolution = 0.0f;
- gravity = new Vector2(0, 0.125f);
- base.Initialize();
- }
- /// <summary>
- /// LoadContent will be called once per game and is the place to load
- /// all of your content.
- /// </summary>
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- device = graphics.GraphicsDevice;
- screenWidth = device.PresentationParameters.BackBufferWidth;
- screenHeight = device.PresentationParameters.BackBufferHeight;
- ballTexture = Content.Load<Texture2D>("BallCL");
- backgroundTexture = Content.Load<Texture2D>("TurfBackground");
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// all content.
- /// </summary>
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- }
- /// <summary>
- /// Allows the game to run logic such as updating the world,
- /// checking for collisions, gathering input, and playing audio.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Update(GameTime gameTime)
- {
- // Allows the game to exit
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
- mouseStateCurrent = Mouse.GetState();
- Vector2 mouseClickPos;
- if (mouseStateCurrent.LeftButton == ButtonState.Released && mouseStatePrevious.LeftButton == ButtonState.Pressed)
- {
- mouseClickPos = new Vector2(mouseStateCurrent.X, mouseStateCurrent.Y);
- ballMovement += Vector2.Multiply(Vector2.Subtract(mouseClickPos,ballPosition),0.1f);
- }
- if (mouseStateCurrent.RightButton == ButtonState.Pressed && mouseStatePrevious.RightButton == ButtonState.Pressed)
- if ((mouseStateCurrent.X >= ballPosition.X - 25 && mouseStateCurrent.X <= ballPosition.X + 25)
- && (mouseStateCurrent.Y >= ballPosition.Y - 25 && mouseStateCurrent.Y <= ballPosition.Y + 25))
- {
- ballPosition = new Vector2(mouseStateCurrent.X, mouseStateCurrent.Y);
- ballMovement = Vector2.Zero;
- ballRevolution = 0.0f;
- }
- if (mouseStateCurrent.RightButton == ButtonState.Released && mouseStatePrevious.RightButton == ButtonState.Pressed)
- if ((mouseStatePrevious.X >= ballPosition.X - 25 && mouseStatePrevious.X <= ballPosition.X + 25)
- && (mouseStatePrevious.Y >= ballPosition.Y - 25 && mouseStatePrevious.Y <= ballPosition.Y + 25))
- {
- ballMovement = Vector2.Multiply(new Vector2(mouseStateCurrent.X - mouseStatePrevious.X, mouseStateCurrent.Y - mouseStatePrevious.Y),1f);
- ballRevolution = 0.0f;
- }
- ProcessBallPosition();
- mouseStatePrevious = mouseStateCurrent;
- // TODO: Add your update logic here
- base.Update(gameTime);
- }
- /// <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();
- DrawBackground();
- DrawBall();
- spriteBatch.End();
- base.Draw(gameTime);
- }
- private void DrawBackground()
- {
- Rectangle screenRectangle = new Rectangle(0, 0, screenWidth, screenHeight);
- spriteBatch.Draw(backgroundTexture, screenRectangle, Color.White);
- }
- private void DrawBall()
- {
- Rectangle ballRectangle = new Rectangle((int)ballPosition.X, (int)ballPosition.Y, ballSize, ballSize);
- spriteBatch.Draw(ballTexture, ballRectangle, null, Color.White, ballRotation, new Vector2(50), SpriteEffects.None, 0);
- }
- private void DrawBallShadow()
- {
- Rectangle ballShadowRectangle = new Rectangle((int)ballPosition.X, (int)ballPosition.Y, ballSize, ballSize);
- Vector2 shadowV = new Vector2();
- shadowV.X = -(screenWidth - ballPosition.Y) * 0.5f;
- shadowV.Y = (screenHeight - ballPosition.Y) * 0.3f;
- spriteBatch.Draw(ballTexture, ballShadowRectangle, null, new Color(0,0,0,180), 0.0f, shadowV, SpriteEffects.None, 1);
- }
- private void ProcessBallPosition()
- {
- ballRotation += ballRevolution;
- ballRevolution = MathHelper.SmoothStep(ballRevolution, 0, 0.05f);
- ballMovement += gravity;
- if (ballMovement.Y > maxVerticalSpeed) ballMovement.Y = maxVerticalSpeed;
- if (ballMovement.Y < -maxVerticalSpeed) ballMovement.Y = -maxVerticalSpeed;
- ballPosition += ballMovement;
- //ballMovement = new Vector2(ballMovement.X * 0.8f, ballMovement.Y);
- float smoothing = Math.Abs(ballPosition.Y - (screenHeight - ballSize/2)) < 0.1 ? 0.12f : 0.01f;
- ballMovement = Vector2.SmoothStep(ballMovement, new Vector2(0, ballMovement.Y), smoothing);
- CheckBoundaries();
- if (Math.Abs(ballMovement.X) < 0.01)
- ballMovement.X = 0.00f;
- if ((Math.Abs(ballMovement.Y) < 0.05) && (Math.Abs(ballPosition.Y - (screenHeight - ballSize / 2)) < 0.1))
- ballMovement.Y = 0.00f;
- }
- private void CheckBoundaries()
- {
- float angleRate = 0.05f;
- // CHECK BOTTOM
- if ((ballPosition.Y >= screenHeight - ballSize / 2) && (ballMovement.Y > 0))
- {
- ballMovement = new Vector2(ballMovement.X, -(ballMovement.Y * 0.75f));
- ballRevolution = ballMovement.X * angleRate;
- }
- if (ballPosition.Y > screenHeight - ballSize / 2) ballPosition.Y = screenHeight - ballSize / 2;
- // CHECK TOP
- if ((ballPosition.Y <= ballSize / 2) && (ballMovement.Y < 0))
- {
- ballMovement = new Vector2(ballMovement.X, -(ballMovement.Y));
- ballRevolution = -ballMovement.X * angleRate;
- }
- if (ballPosition.Y < ballSize / 2) ballPosition.Y = ballSize / 2;
- // CHECK LEFT
- if ((ballPosition.X <= ballSize / 2) && (ballMovement.X < 0))
- {
- ballMovement = new Vector2(-ballMovement.X * 0.75f, ballMovement.Y);
- ballRevolution = ballMovement.Y * angleRate;
- }
- if (ballPosition.X < ballSize / 2) ballPosition.X = ballSize / 2;
- // CHECK RIGHT
- if ((ballPosition.X >= screenWidth - ballSize / 2) && (ballMovement.X > 0))
- {
- ballMovement = new Vector2(-ballMovement.X * 0.75f, ballMovement.Y);
- ballRevolution = -ballMovement.Y * angleRate;
- }
- if (ballPosition.X > screenWidth - ballSize / 2) ballPosition.X = screenWidth - ballSize / 2;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement