Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Using Statements
- using System;
- using System.Linq;
- using System.Collections.Generic;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Media;
- using Microsoft.Xna.Framework.Input;
- #endregion
- namespace MyXnaGame
- {
- /// <summary>
- /// Sample showing how to manage different game states, with transitions
- /// between menu screens, a loading screen, the game itself, and a pause
- /// menu. This main game class is extremely simple: all the interesting
- /// stuff happens in the ScreenManager component.
- /// </summary>
- public class GameStateManagementGame : Microsoft.Xna.Framework.Game
- {
- #region Fields
- GraphicsDeviceManager graphics;
- ScreenManager screenManager;
- public Song PlayingSong { get; private set; }
- private int screenWidth;
- private int screenHeight;
- private Input input;
- private Bat rightBat;
- private Bat leftBat;
- private Ball ball;
- SpriteBatch spriteBatch;
- private int resetTimer;
- private bool resetTimerInUse;
- private bool lastScored;
- SpriteFont arial;
- // By preloading any assets used by UI rendering, we avoid framerate glitches
- // when they suddenly need to be loaded in the middle of a menu transition.
- static readonly string[] preloadAssets =
- {
- "gradient",
- };
- #endregion
- #region Initialization
- protected override void Initialize()
- {
- // TODO: Add your initialization logic here
- screenHeight = 600;
- screenWidth = 800;
- graphics.PreferredBackBufferHeight = screenHeight;
- graphics.PreferredBackBufferWidth = screenWidth;
- graphics.ApplyChanges();
- input = new Input();
- rightBat = new Bat(Content, new Vector2(screenWidth, screenHeight), false);
- leftBat = new Bat(Content, new Vector2(screenWidth, screenHeight), true);
- ball = new Ball(Content, new Vector2(screenWidth, screenHeight));
- ball.Reset(true);
- resetTimer = 0;
- resetTimerInUse = true;
- lastScored = false;
- base.Initialize();
- }
- /// <summary>
- /// The main game constructor.
- /// </summary>
- public GameStateManagementGame()
- {
- Content.RootDirectory = "Content";
- graphics = new GraphicsDeviceManager(this);
- // graphics.PreferredBackBufferWidth = screenWidth;
- // graphics.PreferredBackBufferHeight = screenHeight;
- // Create the screen manager component.
- screenManager = new ScreenManager(this);
- Components.Add(screenManager);
- // Activate the first screens.
- screenManager.AddScreen(new BackgroundScreen(), null);
- //screenManager.AddScreen(new MainMenuScreen(), null);
- screenManager.AddScreen(new PressStartScreen(), null);
- }
- /// <summary>
- /// Loads graphics content.
- /// </summary>
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- PlayingSong = Content.Load<Song>(@"sfx/boomer");
- arial = Content.Load<SpriteFont>("Arial");
- foreach (string asset in preloadAssets)
- {
- Content.Load<object>(asset);
- }
- }
- #endregion
- /// <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)
- {
- input.Update();
- if (resetTimerInUse)
- {
- resetTimer++;
- ball.Stop();
- }
- if (resetTimer == 120)
- {
- resetTimerInUse = false;
- ball.Reset(lastScored);
- resetTimer = 0;
- }
- if (input.LeftDown) leftBat.MoveDown();
- else if ((input.LeftUp)) leftBat.MoveUp();
- if (input.RightDown) leftBat.MoveDown();
- else if (input.RightUp) leftBat.MoveUp();
- leftBat.UpdatePosition(ball);
- rightBat.UpdatePosition(ball);
- ball.UpdatePosition();
- if (ball.GetDirection() > 1.5f * Math.PI || ball.GetDirection() < 0.5f * Math.PI)
- {
- if (rightBat.GetSize().Intersects(ball.GetSize()))
- {
- ball.BatHit(CheckHitLocation(rightBat));
- }
- }
- else if (leftBat.GetSize().Intersects(ball.GetSize()))
- {
- ball.BatHit(CheckHitLocation(leftBat));
- }
- if (!resetTimerInUse)
- {
- if (ball.GetPosition().X > screenWidth)
- {
- resetTimerInUse = true;
- lastScored = true;
- leftBat.IncrementPoints();
- }
- else if (ball.GetPosition().X < 0)
- {
- resetTimerInUse = true;
- lastScored = false;
- rightBat.IncrementPoints();
- }
- }
- base.Update(gameTime);
- }
- private int CheckHitLocation(Bat bat)
- {
- int block = 0;
- if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 20) block = 1;
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 2) block = 2;
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 3) block = 3;
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 4) block = 4;
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 5) block = 5;
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 6) block = 6;
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 7) block = 7;
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 8) block = 8;
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 20 * 19) block = 9;
- else block = 10;
- return block;
- }
- #region Draw
- /// <summary>
- /// This is called when the game should draw itself.
- /// </summary>
- protected override void Draw(GameTime gameTime)
- {
- graphics.GraphicsDevice.Clear(Color.Black);
- // The real drawing happens inside the screen manager component.
- base.Draw(gameTime);
- }
- #endregion
- }
- #region Entry Point
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- static class Program
- {
- static void Main()
- {
- using (GameStateManagementGame game = new GameStateManagementGame())
- {
- game.Run();
- }
- }
- }
- #endregion
- }
RAW Paste Data