Advertisement
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 WindowsGame2
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- Ball ball1;
- Ball ball2;
- bool colide = false;
- Texture2D ballTexture;
- Rectangle ballRectangle;
- Texture2D batText;
- Rectangle player1Bat;
- Rectangle player2Bat;
- float timer = 0;
- float interval = 1000.0f;
- int ballXspeed;
- int ballYspeed;
- int ball2Xspeed;
- int ball2Yspeed;
- int batspeed;
- int score1;
- int score2;
- SpriteFont scorefont;
- KeyboardState keyboardstate;
- KeyboardState prekeystate;
- 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>
- ///
- private Rectangle GSREC(Texture2D texture, float widthFactor)
- {
- float aspectRatio = (float)texture.Width / texture.Height;
- float width = GraphicsDevice.Viewport.Width / widthFactor;
- float height = width / aspectRatio;
- Rectangle result = new Rectangle(
- 0,
- 0,
- (int)width,
- (int)height);
- return result;
- }
- protected override void Initialize()
- {
- 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);
- ballTexture = Content.Load<Texture2D>(@"Images\ball");
- ball1 = new Ball(
- GSREC(ballTexture, 20),
- ballTexture,
- GraphicsDevice.Viewport.Width / 120,
- GraphicsDevice.Viewport.Width / 120,
- Color.White);
- ball2 = new Ball(
- GSREC(ballTexture, 20),
- ballTexture,
- GraphicsDevice.Viewport.Width / 120,
- -GraphicsDevice.Viewport.Width / 120,
- Color.White);
- ball1.Rectangle = CenterBall(ball1);
- ball2.Rectangle = CenterBall(ball2);
- batText = Content.Load<Texture2D>(@"Images\bat");
- player1Bat = GSREC(batText, 25);
- player2Bat = player1Bat;
- player1Bat.X = player1Bat.Width;
- player1Bat.Y = 300;
- player2Bat.X = GraphicsDevice.Viewport.Width - (2 * player2Bat.Width);
- player2Bat.Y = 100;
- batspeed = GraphicsDevice.Viewport.Height / 100;
- }
- protected Rectangle CenterBall(Ball ball)
- {
- return new Rectangle(
- (GraphicsDevice.Viewport.Width / 2) - (ball1.Rectangle.Width / 2),
- (GraphicsDevice.Viewport.Height / 2) - (ball1.Rectangle.Height / 2),
- ball1.Rectangle.Width,
- ball1.Rectangle.Height);
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// all content.
- /// </summary>
- protected override void UnloadContent()
- {
- }
- /// <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)
- {
- prekeystate = keyboardstate;
- keyboardstate = Keyboard.GetState();
- if (ball2.ballstate == Ballstate.stopped)
- {
- ball2.timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
- if (ball2.timer > interval)
- {
- ball2.ballstate = Ballstate.going;
- ball2.timer = 0f;
- }
- }
- if (ball1.ballstate == Ballstate.stopped)
- {
- ball1.timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
- if (ball1.timer > interval)
- {
- ball1.ballstate = Ballstate.going;
- ball1.timer = 0f;
- }
- }
- // Allows the game to exit
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
- if (ball1.ballstate == Ballstate.going)
- {
- ball1.Rectangle.X = ball1.Rectangle.X + ball1.XSpeed;
- }
- if (((ball1.Rectangle.X + ball1.Rectangle.Width) > GraphicsDevice.Viewport.Width) || (ball1.Rectangle.X < 0))
- {
- ball1.Rectangle = CenterBall(ball1);
- ball1.ballstate = Ballstate.stopped;
- }
- if (ball1.ballstate == Ballstate.going)
- {
- ball1.Rectangle.Y = ball1.Rectangle.Y + ball1.YSpeed;
- }
- if (((ball1.Rectangle.Y + ball1.Rectangle.Height) > GraphicsDevice.Viewport.Height) || (ball1.Rectangle.Y < 0))
- {
- ball1.YSpeed = ball1.YSpeed * -1;
- }
- if (ball2.ballstate == Ballstate.going)
- {
- ball2.Rectangle.X = ball2.Rectangle.X + ball2.XSpeed;
- }
- if (((ball2.Rectangle.X + ball2.Rectangle.Width) > GraphicsDevice.Viewport.Width) || (ball2.Rectangle.X < 0))
- {
- ball2.Rectangle = CenterBall(ball2);
- ball2.ballstate = Ballstate.stopped;
- }
- if (ball2.ballstate == Ballstate.going)
- {
- ball2.Rectangle.Y = ball2.Rectangle.Y + ball2.YSpeed;
- }
- if (((ball2.Rectangle.Y + ball2.Rectangle.Height) > GraphicsDevice.Viewport.Height) || (ball2.Rectangle.Y < 0))
- {
- ball2.YSpeed = ball2.YSpeed * -1;
- }
- if (player1Bat.Intersects(ball1.Rectangle) ||
- player2Bat.Intersects(ball1.Rectangle))
- {
- ball1.XSpeed = ball1.XSpeed * -1;
- }
- if (player1Bat.Intersects(ball2.Rectangle) ||
- player2Bat.Intersects(ball2.Rectangle))
- {
- ball2.XSpeed = ball2.XSpeed * -1;
- }
- if (((ball1.Rectangle.Y + ball1.Rectangle.Height > ball2.Rectangle.Y) &&
- (ball1.Rectangle.Y < ball2.Rectangle.Y + ball2.Rectangle.Height)) &&
- ((ball1.Rectangle.X + ball1.Rectangle.Width > ball2.Rectangle.X) &&
- (ball1.Rectangle.X < ball2.Rectangle.X + ball2.Rectangle.Width)) &&
- !colide)
- {
- colide = true;
- ball1.YSpeed = ball1.YSpeed * -1;
- ball2.XSpeed = ball2.XSpeed * -1;
- }
- else
- {
- colide = false;
- }
- //
- /*
- GamePadState gamepad1 = GamePad.GetState(PlayerIndex.One);
- if (gamepad1.DPad.Up == ButtonState.Pressed)
- {
- player1Bat.Y = player1Bat.Y - batspeed;
- }
- if (gamepad1.DPad.Up == ButtonState.Pressed)
- {
- player1Bat.Y = player1Bat.Y + batspeed;
- }
- GamePadState gamepad2 = GamePad.GetState (PlayerIndex.Two);
- if (gamepad2.DPad.Up == ButtonState.Pressed)
- {
- player1Bat.Y = player1Bat.Y - batspeed;
- }
- if (gamepad2.DPad.Up == ButtonState.Pressed)
- {
- player1Bat.Y = player1Bat.Y + batspeed;
- }
- */
- if (keyboardstate.IsKeyDown(Keys.W))
- {
- if (player1Bat.Y > 0)
- {
- player1Bat.Y = player1Bat.Y - batspeed;
- }
- }
- if (keyboardstate.IsKeyDown(Keys.S))
- {
- if (player1Bat.Y + player1Bat.Height < GraphicsDevice.Viewport.Height)
- {
- player1Bat.Y = player1Bat.Y + batspeed;
- }
- }
- if (keyboardstate.IsKeyDown(Keys.Up))
- {
- if (player2Bat.Y > 0)
- {
- player2Bat.Y = player2Bat.Y - batspeed;
- }
- }
- if (keyboardstate.IsKeyDown(Keys.Down))
- {
- if (player2Bat.Y + player2Bat.Height < GraphicsDevice.Viewport.Height)
- {
- player2Bat.Y = player2Bat.Y + batspeed;
- }
- }
- if (player1Bat.Intersects(ball1.Rectangle) ||
- player2Bat.Intersects(ball1.Rectangle))
- {
- ballXspeed = ballXspeed * -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);
- scorefont = Content.Load<SpriteFont>(@"Score1");
- spriteBatch.Begin();
- spriteBatch.Draw(
- batText,
- player1Bat,
- Color.YellowGreen);
- spriteBatch.Draw(
- batText,
- player2Bat,
- Color.Tomato);
- spriteBatch.Draw(
- ball1.Texture,
- ball1.Rectangle,
- ball1.BallColor);
- spriteBatch.Draw(
- ball2.Texture,
- ball2.Rectangle,
- Color.Red);
- spriteBatch.DrawString(scorefont, "Player1's score:" + score1,
- new Vector2(0, 0),
- Color.Black);
- spriteBatch.DrawString(scorefont, "Player2's score:" + score2,
- new Vector2(0, GraphicsDevice.Viewport.Width / 4),
- Color.Black);
- spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement