Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Using Statements
- using System;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Storage;
- using Microsoft.Xna.Framework.Input;
- #endregion
- namespace VipersPong
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Game
- {
- KeyboardState MovingPaddles;
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- Texture2D backgroundTexture;
- int backgroundXPos;
- int backgroundYPos;
- int backgroundWidth;
- int backgroundHeight;
- Texture2D paddleTexture;
- int paddleXPos;
- int paddleYPos;
- int paddleWidth;
- int paddleHeight;
- Texture2D ballTexture;
- int ballXPos;
- int ballYPos;
- int ballWidth;
- int ballHeight;
- Texture2D paddle2Texture;
- int paddle2XPos;
- int paddle2YPos;
- int paddle2Width;
- int paddle2Height;
- Texture2D menuTexture;
- int menuTextureXPos;
- int menuTextureYPos;
- int menuTextureWidth;
- int menuTextureHeight;
- enum EGameStates
- {
- MENU,
- GAME
- }
- public Game1 ()
- {
- graphics = new GraphicsDeviceManager (this);
- Content.RootDirectory = "Content";
- graphics.IsFullScreen = true;
- }
- /// <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 ();
- Vector2 backgroundTexture_pos = new Vector2(0, 0);
- backgroundWidth = 512;
- backgroundHeight = 512;
- Vector2 paddleTexture_pos = new Vector2(100, 0);
- paddleWidth = 64;
- paddleHeight = 64;
- Vector2 ballTexture_pos = new Vector2(256, 256);
- Vector2 ball_vel= new Vector2( 3, 7);
- ballWidth = 16;
- ballHeight = 16;
- Vector2 paddle2Texture_pos = new Vector2 (100, 512);
- paddle2Width = 16;
- paddle2Height = 16;
- Vector2 menuTexture_pos = new Vector2 (0, 0);
- menuTextureWidth = 512;
- menuTextureHeight = 512;
- }
- /// <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);
- //TODO: use this.Content to load your game content here
- backgroundTexture = Content.Load<Texture2D>("background");
- paddleTexture = Content.Load<Texture2D> ("paddle");
- ballTexture = Content.Load<Texture2D> ("ball");
- paddle2Texture = Content.Load<Texture2D> ("paddle2");
- menuTexture = Content.Load<Texture2D> ("menuTexture");
- }
- /// <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)
- {
- // For Mobile devices, this logic will close the Game when the Back button is pressed
- if (GamePad.GetState (PlayerIndex.One).Buttons.Back == ButtonState.Pressed) {
- Exit ();
- }
- // TODO: Add your update logic here
- base.Update (gameTime);
- EGameStates gameState = EGameStates.MENU;
- switch (gameState)
- {
- case EGameStates.GAME: UpdateGame (gameTime); break;
- case EGameStates.MENU: UpdateMenu (gameTime); break;
- default:Console.WriteLine ("An Error Has Occured."); break;
- }
- }
- void UpdateGame(GameTime gameTime)
- {
- if (paddleXPos > graphics.PreferredBackBufferWidth)
- {
- paddleXPos = 0;
- }
- if (paddleYPos > graphics.PreferredBackBufferHeight)
- {
- paddleYPos = 0;
- }
- if (paddle2XPos > graphics.PreferredBackBufferWidth)
- {
- paddle2XPos = 0;
- }
- if (paddle2YPos > graphics.PreferredBackBufferHeight)
- {
- paddle2YPos = 0;
- }
- if (ballXPos > graphics.PreferredBackBufferWidth)
- {
- Vector2 ball_vel = new Vector2( 7, 3);
- }
- if (ballYPos > graphics.PreferredBackBufferHeight)
- {
- ballYPos = 0;
- }
- if(state.IsKeyDown(Keys.Left))
- {
- paddleXPos -= 5;
- }
- if (state.IsKeyDown (Keys.Right))
- {
- paddeXPos += 5;
- }
- if (state.IsKeyDown (Keys.A))
- {
- padde2XPos -= 5;
- }
- if (state.IsKeyDown (Keys.D))
- {
- padde2XPos += 5;
- }
- if (Rectangle.Intersect(paddleTextre, ballTexure) != Rectangle.Empty )
- {
- Vector2 ball_vel = new Vector2( 7, 3);
- }
- if (Rectangle.Intersect(paddle2Texture, ballTexture) != Rectangle.Empty )
- {
- Vector2 ball_vel = new Vector2( 7, 3);
- }
- if (Keyboard.GetState().IsKeyDown(Keys.Escape))
- {
- EGameStates gameState = EGameStates.MENU;
- }
- }
- void UpdateMenu(GameTime gameTime)
- {
- if (Keyboard.GetState().IsKeyDown(Keys.Space))
- {
- EGameStates gameState = EGameStates.GAME;
- }
- }
- void DrawGame(GameTime gameTime)
- {
- //Draw Game Resources Here
- spriteBatch.Begin();
- spriteBatch.Draw(backgroundTexture,
- new Rectangle(backgroundXPos, backgroundYPos, backgroundWidth, backgroundHeight),
- Color.White);
- spriteBatch.End();
- spriteBatch.Begin();
- spriteBatch.Draw(paddleTexture,
- new Rectangle(paddleXPos, paddleYPos, paddleWidth, paddleHeight),
- Color.White);
- spriteBatch.End();
- spriteBatch.Begin();
- spriteBatch.Draw(ballTexture,
- new Rectangle(ballXPos, ballYPos, ballWidth, ballHeight),
- Color.White);
- spriteBatch.End();
- spriteBatch.Begin();
- spriteBatch.Draw(paddle2Texture,
- new Rectangle(paddle2XPos, paddle2YPos, paddle2Width, paddle2Height),
- Color.White);
- spriteBatch.End();
- }
- void DrawMenu(GameTime gameTime)
- {
- spriteBatch.Begin();
- spriteBatch.Draw(menuTexture,
- new Rectangle(menuTextureXPos, menuTextureYPos, menuTextureWidth, menuTextureHeight),
- Color.White);
- spriteBatch.End();
- }
- /// <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)
- {
- graphics.GraphicsDevice.Clear (Color.CornflowerBlue);
- //TODO: Add your drawing code here
- switch (gameState)
- {
- case EGameStates.GAME: DrawGame(); break;
- case EGameStates.MENU: DrawMenu(); break;
- default: Console.WriteLine("An Error Has Occured."); break;
- }
- base.Draw (gameTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement