/PongXNA/PongXNA/Game1.cs
C# | 261 lines | 173 code | 38 blank | 50 comment | 26 complexity | 6deb04e65051624c9425129764af159c MD5 | raw file
- 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;
-
- namespace PongXNA
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
-
- public class Game1 : Microsoft.Xna.Framework.Game
- {
-
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- SpriteFont font;
- /// <summary>
- /// TODO Decleration of variable used in this program
- /// </summary>
- ///
- private Rectangle FirstRacketHitBox { get; set; }
- private Rectangle SecondRacketHitBox { get; set; }
- private Rectangle BallHitBox { get; set; }
- private double currentPosXFirstRacket;
- private double currentPosYFirstRacket;
- private double currentPosXSecondRacket;
- private double currentPosYSecondRacket;
- private float currentPosXBall;
- private float currentPosYBall;
- private Vector2 vectorBall;
- private Scene scene = new Scene();
- private int leftScore;
- private int rightScore;
- private string score;
- private int vitesse = 6;
-
-
-
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- 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
-
- this.IsMouseVisible = true; // Allow the mouse to be visible
-
- /// This the declaration of the position of the first racket we create a hit box for testing in the future if the ball touching them
- /// <FirstRacket>
- currentPosXFirstRacket = 0;
- currentPosYFirstRacket = GraphicsDevice.Viewport.Height / 2 - 39;
- FirstRacketHitBox = new Rectangle((int)currentPosXFirstRacket, (int)currentPosYFirstRacket, 9 * 2 + 8 * 2, 78 * 2);
- /// </FirstRacket>
- ///
- /// This the declaration of the position of the second racket we create a hit box for testing in the future if the ball touching them
- /// <SecondRacket>
- currentPosXSecondRacket = GraphicsDevice.Viewport.Width - 9 * 2 - 8 * 2;
- currentPosYSecondRacket = GraphicsDevice.Viewport.Height / 2 - 39;
- SecondRacketHitBox = new Rectangle((int)currentPosXSecondRacket, (int)currentPosYSecondRacket, 9 * 2 + 8 * 2, 78 * 2);
- /// </SecondRacket>
- ///
- /// This the declaration of the position of the ball and we create a hit box
- /// <Ball>
- currentPosXBall = GraphicsDevice.Viewport.Width /2 -4;
- currentPosYBall= GraphicsDevice.Viewport.Height / 2 -4;
- BallHitBox = new Rectangle((int)currentPosXBall, (int)currentPosYBall, 8 * 2, 8 * 2);
- vectorBall.X = -1; vectorBall.Y = -1;
- /// </SecondRacket>
-
-
- scene.PosXLeftSide = -50;
- scene.PosYLeftSide = -50;
- scene.LeftSideHitBox = new Rectangle((int)scene.PosXLeftSide, (int)scene.PosYLeftSide, 8 * 2 + 50, GraphicsDevice.Viewport.Height + 100);
-
- scene.PosXRightSide = GraphicsDevice.Viewport.Width - 8 * 2;
- scene.PosYRightSide = 0;
- scene.RightSideHitBox = new Rectangle((int)scene.PosXRightSide, (int)scene.PosYRightSide, 8 * 2 + 50, GraphicsDevice.Viewport.Height + 100);
-
- scene.PosXUpSide = -50;
- scene.PosYUpSide = -50;
- scene.UpSideHitBox = new Rectangle((int)scene.PosXUpSide, (int)scene.PosYUpSide, GraphicsDevice.Viewport.Width + 100, 8 * 2 + 50);
-
- scene.PosXDownSide = -50;
- scene.PosYDownSide = GraphicsDevice.Viewport.Height - 8 * 2;
- scene.DownSideHitBox = new Rectangle((int)scene.PosXDownSide, (int)scene.PosYDownSide, GraphicsDevice.Viewport.Width + 100, 8 * 2 + 50);
-
- leftScore = 0;
- rightScore = 0;
- score = "Score : " + leftScore + " - " + rightScore;
-
-
- base.Initialize();
- }
-
- /// <summary>
- /// LoadContent will be called once per game and is the place to load
- /// all of your content.
- /// </summary>
- private Texture2D FirstRacket;
- private Texture2D SecondRacket;
- private Texture2D Ball;
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- font = Content.Load<SpriteFont>("Times New Roman");
-
- // TODO: use this.Content to load your game content here
- FirstRacket = Content.Load<Texture2D>("FirstRacket");
- SecondRacket = Content.Load<Texture2D>("SecondRacket");
- Ball = Content.Load<Texture2D>("Ball");
- }
-
- /// <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();
-
-
- // TODO: Add your update logic here
- MouseState mouseState = Mouse.GetState(); // We just want to have the current position of mouse
- KeyboardState UpOrDown = Keyboard.GetState();
- if ((mouseState.Y - 39 * 2) > 0 && (mouseState.Y + 39 * 2) < GraphicsDevice.Viewport.Height)//we want to know if the mouse is in the window
- {
- currentPosYFirstRacket = mouseState.Y - 39 * 2;// to put the middle of the racket in front a the mouse
- }
-
-
- if (UpOrDown.IsKeyDown(Keys.Down) && currentPosYSecondRacket + 78 * 2 < GraphicsDevice.Viewport.Height)//to control the second racket manually
- {
- currentPosYSecondRacket = currentPosYSecondRacket + 4;
- }
- else if (UpOrDown.IsKeyDown(Keys.Up) && currentPosYSecondRacket > 0)
- {
- currentPosYSecondRacket = currentPosYSecondRacket - 4;
- }
- else if (currentPosYSecondRacket > 0 && vectorBall.X > 0 && currentPosYSecondRacket + 78 * 2 < GraphicsDevice.Viewport.Height)// to control the second racket with an AI
- {
- currentPosYSecondRacket = currentPosYSecondRacket + 8 * (currentPosYBall + 4 * 2 - currentPosYSecondRacket - 78) / Math.Abs(0.1 + (currentPosYBall + 4 * 2 - currentPosYSecondRacket - 78));
- }
- else
- { }
- if (currentPosYSecondRacket < 0)
- {
- currentPosYSecondRacket = currentPosYSecondRacket + 3;
- }
- else if (currentPosYSecondRacket >= GraphicsDevice.Viewport.Height)
- {
- currentPosYSecondRacket = currentPosYSecondRacket - 3;
- }
- else
- { }
-
- if (currentPosXBall < 0)
- {
- //point a droite
- currentPosXBall = GraphicsDevice.Viewport.Width / 2 - 4;
- currentPosYBall = GraphicsDevice.Viewport.Height / 2 - 4;
- rightScore++;
- vectorBall.X = -1;
- vectorBall.Y = 0;
- vitesse = 6;
- }
- else if (currentPosXBall > GraphicsDevice.Viewport.Width)
- {
- //point a gauche
- currentPosXBall = GraphicsDevice.Viewport.Width / 2 - 4;
- currentPosYBall = GraphicsDevice.Viewport.Height / 2 - 4;
- leftScore++;
- vectorBall.X = 1;
- vectorBall.Y = 0;
- vitesse = 6;
- }
- else if (currentPosYBall < 0 || currentPosYBall + 8 * 2 > GraphicsDevice.Viewport.Height)
- {
- vectorBall.Y *= -1;
-
- }
- else if (FirstRacketHitBox.Contains(BallHitBox))
- {
- vectorBall.X *= -1;
- vectorBall.Y += ((float)currentPosYBall + 8 - (float)currentPosYFirstRacket - 78) / 14;
- vitesse++;
- }
- else if (SecondRacketHitBox.Contains(BallHitBox))
- {
- vectorBall.X *= -1;
- vectorBall.Y += ((float)currentPosYBall + 8 - (float)currentPosYSecondRacket - 78) / 14;
- vitesse++;
- }
- else
- { }
-
- vectorBall.Normalize();
- currentPosXBall=currentPosXBall + vitesse * vectorBall.X;
- currentPosYBall=currentPosYBall + vitesse * vectorBall.Y;
-
-
-
-
- FirstRacketHitBox = new Rectangle((int)currentPosXFirstRacket, (int)currentPosYFirstRacket, 9*2 + 8 * 2, 78*2); // We just put the new position of mouse
- SecondRacketHitBox = new Rectangle((int)currentPosXSecondRacket, (int)currentPosYSecondRacket, 9 * 2 + 8 * 2, 78 * 2);//We do the same for a test
- BallHitBox = new Rectangle((int)currentPosXBall, (int)currentPosYBall, 8 * 2, 8 * 2);
-
- 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);
-
- // TODO: Add your drawing code here
- graphics.GraphicsDevice.Clear(Color.White);
- spriteBatch.Begin();
- spriteBatch.Draw(FirstRacket, FirstRacketHitBox, Color.White);
- spriteBatch.Draw(SecondRacket, SecondRacketHitBox, Color.White);
- spriteBatch.Draw(Ball,BallHitBox,Color.White);
- spriteBatch.DrawString(font, score, new Vector2(GraphicsDevice.Viewport.Width / 2 - font.MeasureString(score).X /2, 10), Color.Black);
- spriteBatch.End();
- base.Draw(gameTime);
- }
-
-
- }
- }