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;
- using Microsoft.Xna.Framework.Net;
- using Microsoft.Xna.Framework.Storage;
- namespace Pool
- {
- /// <summary>
- /// Arcade Pool
- /// Graphics by Matt Pickering
- /// </summary>
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- Texture2D backgroundImage;
- Texture2D whiteBall;
- Vector2 whiteSpeed;
- Vector2 whitePosition;
- Rectangle rectangle;
- int backgroundNumber;
- bool backgroundChanged;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- graphics.PreferredBackBufferHeight = 594;
- graphics.PreferredBackBufferWidth = 1030;
- }
- /// <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()
- {
- // TODO: Add your initialization logic here
- 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);
- backgroundImage = Content.Load<Texture2D>(@"Tables/table_blue");
- backgroundNumber = 0;
- backgroundChanged = false;
- whiteBall = Content.Load<Texture2D>(@"Balls/white1");
- whitePosition = new Vector2(200, 280);
- whiteSpeed = Vector2.Zero;
- rectangle = new Rectangle(-35, -24, graphics.GraphicsDevice.Viewport.Width + 70, graphics.GraphicsDevice.Viewport.Height + 48);
- }
- /// <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();
- if (GamePad.GetState(PlayerIndex.One).Buttons.RightShoulder == ButtonState.Released && backgroundChanged)
- backgroundChanged = false;
- if (GamePad.GetState(PlayerIndex.One).Buttons.RightShoulder == ButtonState.Pressed && !backgroundChanged)
- {
- TableColour();
- backgroundChanged = true;
- }
- // Allow player to control the white ball
- if (whiteSpeed == Vector2.Zero)
- {
- GamePadState padState = GamePad.GetState(PlayerIndex.One);
- if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed)
- {
- whiteSpeed.X = 8 * padState.ThumbSticks.Left.X;
- whiteSpeed.Y = 8 * -padState.ThumbSticks.Left.Y;
- }
- }
- // Move the ball and friction
- whitePosition += whiteSpeed;
- if (whiteSpeed != Vector2.Zero)
- whiteSpeed *= new Vector2(0.99f, 0.99f);
- // Stop the ball
- if (Math.Abs(whiteSpeed.X) < 0.26f && Math.Abs(whiteSpeed.Y) < 0.26f)
- whiteSpeed = Vector2.Zero;
- // Check bouncing against cushion.
- if (whitePosition.X < 72 || whitePosition.X > Window.ClientBounds.Width - (whiteBall.Width + 72))
- whiteSpeed.X *= -1;
- if (whitePosition.Y < 76 || whitePosition.Y > Window.ClientBounds.Height - (whiteBall.Height + 76))
- whiteSpeed.Y *= -1;
- 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.CornflowerBlue);
- spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
- spriteBatch.Draw(backgroundImage, rectangle, Color.White);
- spriteBatch.Draw(whiteBall, whitePosition, Color.White);
- spriteBatch.End();
- base.Draw(gameTime);
- }
- public void TableColour()
- {
- switch (backgroundNumber)
- {
- case 0:
- backgroundImage = Content.Load<Texture2D>(@"Tables/table_green");
- backgroundNumber = 1;
- break;
- case 1:
- backgroundImage = Content.Load<Texture2D>(@"Tables/table_purple");
- backgroundNumber = 2;
- break;
- case 2:
- backgroundImage = Content.Load<Texture2D>(@"Tables/table_red");
- backgroundNumber = 3;
- break;
- case 3:
- backgroundImage = Content.Load<Texture2D>(@"Tables/table_yellow");
- backgroundNumber = 4;
- break;
- case 4: backgroundImage = Content.Load<Texture2D>(@"Tables/table_blue");
- backgroundNumber = 0;
- break;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment