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 WindowsGame1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D img;
Texture2D img2;
Texture2D img3;
Texture2D back;
Vector2 pos1 = new Vector2(0,0);
Vector2 pos2 = new Vector2(780,0);
Vector2 pos3 = new Vector2(395,0);
Vector2 pos4 = new Vector2(0, 0);
Vector2 speed = new Vector2(5, 2);
Song music;
Song sound;
Random rnd = new Random();
int musicCount;
SpriteFont font;
SpriteFont font2;
SpriteFont font3;
int score1 = 0;
int score2 = 0;
Rectangle rect = new Rectangle(0, 0, 16, 62);
Rectangle rect1 = new Rectangle(790, 0, 16, 62);
Rectangle rect2 = new Rectangle(395, 0, 200, 200);
KeyboardState key;
//pos1.X
//pos2.Y
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
base.Initialize();
musicCount = 0;
}
/// <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);
back = Content.Load<Texture2D>("background");
sound = Content.Load<Song>("bop");
img = Content.Load<Texture2D>("paddle1");
img2 = Content.Load<Texture2D>("paddle1");
img3 = Content.Load<Texture2D>("ball");
music = Content.Load<Song>("I cant stop");
font = Content.Load<SpriteFont>("SpriteFont1");
font2 = Content.Load<SpriteFont>("score 1");
font3 = Content.Load<SpriteFont>("score2");
// 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
key = Keyboard.GetState();
if (key.IsKeyDown(Keys.Up))
{
if (pos2.Y + 3 > 0)
{
pos2.Y -= 8;
rect.Y -= 8;
}
}
if (key.IsKeyDown(Keys.Down))
{
if (pos2.Y < 365)
{
pos2.Y += 8;
rect.Y += 8;
}
}
if (key.IsKeyDown(Keys.W))
{
if (pos1.Y + 3 > 0)
{
pos1.Y -= 8;
rect1.Y -= 8;
}
}
if (key.IsKeyDown(Keys.S))
{
if (pos2.Y < 365)
{
pos1.Y += 8;
rect1.Y += 8;
}
}
pos3.Y += speed.Y * 2;
pos3.X += speed.X * 2;
if (rect.Intersects(rect2))
{
speed.X = -speed.X;
}
if (rect1.Intersects(rect2))
{
speed.X = -speed.X;
}
if (pos3.Y <= 0)
{
speed.Y = -speed.Y;
}
if (pos3.Y > 450)
{
speed.Y = -speed.Y;
}
if (pos3.X <= -770)
{
score2 += 1;
pos3.X = 400;
pos3.Y = 400;
speed.X = rnd.Next(1, 3);
if (speed.X == 2)
{
speed.X = 5;
}
else
{
speed.X = -5;
}
speed.Y = rnd.Next(-2, 3);
}
if (pos3.X > 1550)
{
score1 += 1;
pos3.X = 400;
pos3.Y = 400;
speed.X = rnd.Next(1, 3);
if (speed.X == 2)
{
speed.X = 5;
}
else
{
speed.X = -5;
}
speed.Y = rnd.Next(-2, 3);
}
/*
if (pos3.X >= pos2.X - 5 && pos3.Y > pos2.Y - 100 && pos3.Y < pos2.Y + 100)
{
speed.X = -speed.X;
}
if (pos3.X <= pos1.X - 5 && pos3.Y > pos1.Y - 100 && pos3.Y < pos1.Y + 100)
{
speed.X = -speed.X;
}*/
musicCount -= 17;
if (musicCount <= 0)
{
MediaPlayer.Play(music);
musicCount = 34000;
}
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
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(back, pos4, Color.White);
spriteBatch.Draw(img, pos1, Color.White);
spriteBatch.Draw(img2, pos2, Color.White);
spriteBatch.Draw(img3, pos3, Color.White);
spriteBatch.DrawString(font, "X: " + speed.X + "\n Y: " + speed.Y, new Vector2(400,50), Color.Black);
spriteBatch.DrawString(font, "player 1; " + score1,new Vector2(300, 400), Color.Black);
spriteBatch.DrawString(font, "player 2; " + score2, new Vector2(500, 450), Color.Black);
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
}
}