Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- namespace Pong
- {
- /// <summary>
- /// This is the main type for your game.
- /// </summary>
- public class Game1 : Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- Texture2D background;
- Texture2D rocket1;
- Texture2D rocket2;
- Texture2D ball;
- Rockets rockets = new Rockets();
- Rectangle ballRectangle;
- Rectangle rocket1Rectangle;
- Rectangle rocket2Rectangle;
- int rocket1XValue = 0;
- int rocket1YValue = 250;
- int rocket2XValue = 775;
- int rocket2YValue = 250;
- int ballXValue = 390;
- int ballYValue = 290;
- int one = 0;
- int two = 0;
- float speedX;
- float speedY;
- Vector2 ballVelocity;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- graphics.PreferredBackBufferHeight = 600;
- graphics.PreferredBackBufferWidth = 800;
- Window.AllowUserResizing = false;
- IsMouseVisible = true;
- Content.RootDirectory = "Content";
- }
- /// <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
- speedX = 5;
- speedY = 0;
- base.Initialize();
- }
- /// <summary>
- /// LoadContent will be called once per game and is the place to load
- /// </summary>
- /// all of your content.
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- background = Content.Load<Texture2D>("pongBackground");
- rockets.LoadContent(Content);
- ball = Content.Load<Texture2D>("ball");
- ballRectangle = new Rectangle(ballXValue, ballYValue, ball.Width - 4, ball.Height - 5);
- // TODO: use this.Content to load your game content here
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// game-specific 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)
- {
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
- Exit();
- ballRectangle.X += (int)ballVelocity.X;
- ballRectangle.Y += (int)ballVelocity.Y;
- ballVelocity = new Vector2(speedX, speedY);
- if(Keyboard.GetState().IsKeyDown(Keys.Space))
- {
- this.Initialize();
- }
- if (ballRectangle.Intersects(rockets.rocketUp1))
- {
- speedX = 5;
- speedY = -5;
- }
- if(ballRectangle.Intersects(rockets.rocketMiddle1))
- {
- speedX = 5;
- speedY = 0;
- }
- if (ballRectangle.Intersects(rockets.rocketDown1))
- {
- speedX = 5;
- speedY = 5;
- }
- if (ballRectangle.Intersects(rockets.rocketUp2))
- {
- speedX = -5;
- speedY = -5;
- }
- if (ballRectangle.Intersects(rockets.rocketMiddle2))
- {
- speedX = -5;
- speedY = 0;
- }
- if (ballRectangle.Intersects(rockets.rocketDown2))
- {
- speedX = -5;
- speedY = 5;
- }
- if(ballRectangle.X <= 0)
- {
- two++;
- //speedX = 5;
- //speedY = 0;
- //Initialize();
- }
- if(ballRectangle.X >=800)
- {
- one++;
- //speedX = -5;
- //speedY = 0;
- //Initialize();
- }
- if(ballRectangle.Y <= 0)
- {
- speedY = -speedY;
- ballRectangle.Y += 10;
- }
- if(ballRectangle.Y >= 600-10)
- {
- speedY = -speedY;
- ballRectangle.Y -= 10;
- }
- rockets.Update(gameTime);
- 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();
- spriteBatch.Draw(background, new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height), Color.White);
- rockets.Draw(spriteBatch);
- spriteBatch.Draw(ball, ballRectangle, Color.White);
- spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement