| 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 Testing |
| { |
| /// <summary> |
| /// This is the main type for your game |
| /// </summary> |
| public class Game1 : Microsoft.Xna.Framework.Game |
| { |
| GraphicsDeviceManager graphics; |
| SpriteBatch spriteBatch; |
| |
| |
| |
| Texture2D paddletexture; |
| Vector2 paddleposition; |
| Vector2 paddlevelocity; |
| Rectangle playerrectangle; |
| |
| |
| Vector2 Leftside; |
| Vector2 Rightside; |
| |
| Texture2D balltexture; |
| Vector2 ballposition; |
| Rectangle ballrectangle; |
| int ballxSpeed; |
| int ballySpeed; |
| |
| |
| |
| Rectangle viewportrect; |
| |
| |
| SpriteFont Arial; |
| |
| |
| public Game1() |
| { |
| graphics = new GraphicsDeviceManager(this); |
| Content.RootDirectory = "Content"; |
| |
| graphics.PreferredBackBufferHeight = 500; |
| graphics.PreferredBackBufferWidth = 500; |
| graphics.ApplyChanges(); |
| } |
| |
| /// <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 |
| paddleposition = new Vector2(200, 470); |
| |
| |
| viewportrect = new Rectangle(graphics.GraphicsDevice.Viewport.Width, |
| graphics.GraphicsDevice.Viewport.Height, 0, 0); |
| |
| Leftside = new Vector2(2, 10); |
| Rightside = new Vector2(392, 10); |
| |
| ballrectangle = new Rectangle((int)ballposition.X, (int)ballposition.Y, |
| 50, 50); |
| |
| playerrectangle = new Rectangle((int)paddleposition.X, (int)paddleposition.Y, |
| 100, 100); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 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); |
| paddletexture = Content.Load<Texture2D>("Paddle") as Texture2D; |
| balltexture = Content.Load<Texture2D>("Ball") as Texture2D; |
| |
| Arial = Content.Load<SpriteFont>("Arial Black"); |
| |
| // TODO: use this.Content to load your game content here |
| } |
| |
| /// <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(); |
| |
| paddleposition += paddlevelocity; |
| |
| |
| GamePadState gamePadstate = GamePad.GetState(PlayerIndex.One); |
| |
| |
| |
| if (gamePadstate.IsButtonDown(Buttons.LeftThumbstickLeft) |
| && paddleposition.X >= Leftside.X) |
| { |
| paddlevelocity.X -= 5; |
| } |
| else |
| { |
| paddlevelocity.X = 0; |
| } |
| |
| |
| if (paddleposition.X <= Leftside.X) |
| { |
| paddlevelocity.X = 0; |
| } |
| |
| |
| |
| |
| |
| |
| if (gamePadstate.IsButtonDown(Buttons.LeftThumbstickRight)) |
| { |
| paddlevelocity.X += 5; |
| } |
| else |
| { |
| paddlevelocity.X = 0; |
| } |
| if (paddleposition.X >= Rightside.X) |
| { |
| paddlevelocity.X = 0; |
| } |
| |
| if (gamePadstate.IsButtonDown(Buttons.LeftThumbstickLeft) |
| && paddleposition.X >= Leftside.X) |
| { |
| paddlevelocity.X -= 5; |
| } |
| |
| |
| ballUpdate(); |
| |
| if (ballposition.X + balltexture.Width> graphics.GraphicsDevice.Viewport.Width) |
| { |
| ballxSpeed *= -1; |
| } |
| |
| else if (ballposition.X < 0) |
| { |
| ballxSpeed *= -1; |
| } |
| |
| if (ballposition.Y + balltexture.Height> graphics.GraphicsDevice.Viewport.Height) |
| { |
| ballySpeed *= -1; |
| } |
| |
| |
| |
| playerrectangle.X = (int)paddleposition.X; |
| playerrectangle.Y = (int)paddleposition.Y; |
| ballrectangle.X = (int)ballposition.X; |
| ballrectangle.Y = (int)ballposition.Y; |
| |
| if (ballrectangle.Intersects(ballrectangle)) |
| { |
| ballposition.Y = playerrectangle.Top; |
| |
| |
| ballySpeed *= -1; |
| |
| } |
| ballUpdate(); |
| |
| |
| |
| |
| // TODO: Add your update logic here |
| |
| base.Update(gameTime); |
| } |
| |
| |
| void ballUpdate() |
| { |
| ballxSpeed = 4; |
| ballySpeed = 4; |
| |
| ballposition.X += ballxSpeed; |
| ballposition.Y += ballySpeed; |
| |
| |
| |
| |
| |
| } |
| /// <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(); |
| spriteBatch.Draw(paddletexture, paddleposition, Color.White); |
| spriteBatch.Draw(balltexture, ballposition, Color.White); |
| spriteBatch.DrawString(Arial, "\nControls:\nLeft Trigger - Stop\nLeft Thumbstick - Move", |
| new Vector2(0, graphics.GraphicsDevice.Viewport.Width / 2 + -5), Color.Green); |
| |
| |
| spriteBatch.End(); |
| |
| // TODO: Add your drawing code here |
| |
| base.Draw(gameTime); |
| } |
| } |
| } |
| |