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;
- namespace WindowsGame4
- {
- class Paddle
- {
- public Paddle(int width, int height)
- {
- position = new Vector2(0, 0);
- rect = new Rectangle(0, 0, width, height);
- }
- public void Update()
- {
- rect.X = (int)position.X;
- rect.Y = (int)position.Y;
- }
- public void Draw(SpriteBatch spritebatch, Texture2D _texture)
- {
- spritebatch.Draw(_texture, position, Color.White);
- }
- public Vector2 position;
- public Rectangle rect;
- public const int speed = 400;
- public int point;
- public bool looser;
- }
- class Ball
- {
- public Ball(int width, int height)
- {
- position = new Vector2(400, 285);
- rect = new Rectangle(0, 0, width, height);
- angle = 20;
- speed = 300;
- }
- public void Update()
- {
- rect.X = (int)position.X;
- rect.Y = (int)position.Y;
- }
- public void Draw(SpriteBatch spriteBatch, Texture2D _texture)
- {
- spriteBatch.Draw(_texture, position, Color.White);
- }
- public Vector2 position;
- public Rectangle rect;
- public float angle;
- public int speed;
- public const int maxSpeed = 1000;
- }
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- SpriteFont spriteFont;
- Texture2D background, redPaddle, bluePaddle, ballText;
- Paddle red, blue;
- Ball ball;
- double deltaTime;
- bool isPlaying;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- graphics.PreferredBackBufferWidth = 800;
- graphics.PreferredBackBufferHeight = 600;
- red = new Paddle(25, 100);
- blue = new Paddle(25, 100);
- blue.position.X = 775;
- blue.position.Y = red.position.Y = 250;
- ball = new Ball(20, 20);
- isPlaying = red.looser = blue.looser = false;
- red.point = blue.point = 0;
- Content.RootDirectory = "Content";
- }
- protected override void Initialize()
- {
- base.Initialize();
- }
- protected override void LoadContent()
- {
- spriteBatch = new SpriteBatch(GraphicsDevice);
- background = Content.Load<Texture2D>("Background2");
- redPaddle = Content.Load<Texture2D>("RedPaddle");
- bluePaddle = Content.Load<Texture2D>("BluePaddle");
- ballText = Content.Load<Texture2D>("Ball");
- spriteFont = this.Content.Load<SpriteFont>("Arial");
- }
- protected override void UnloadContent()
- { }
- protected override void Update(GameTime gameTime)
- {
- deltaTime = (float)gameTime.ElapsedGameTime.Milliseconds / 1000.0f;
- if (Keyboard.GetState().IsKeyDown(Keys.Escape)) this.Exit();
- if (Keyboard.GetState().IsKeyDown(Keys.Space))
- {
- if (!isPlaying)
- isPlaying = true;
- }
- if (isPlaying)
- {
- if (Keyboard.GetState().IsKeyDown(Keys.Q))
- {
- if (red.position.Y > 5)
- red.position.Y += (float)(-Paddle.speed * deltaTime);
- }
- if (Keyboard.GetState().IsKeyDown(Keys.P))
- {
- if (blue.position.Y > 5)
- blue.position.Y += (float)(-Paddle.speed * deltaTime);
- }
- if (Keyboard.GetState().IsKeyDown(Keys.A))
- {
- if (red.position.Y < 600 - red.rect.Height - 5)
- red.position.Y += (float)(Paddle.speed * deltaTime);
- }
- if (Keyboard.GetState().IsKeyDown(Keys.L))
- {
- if (blue.position.Y < 600 - blue.rect.Height - 5)
- blue.position.Y += (float)(Paddle.speed * deltaTime);
- }
- }
- // Ball move
- ball.position.X -= (float)(Math.Cos(ball.angle) * (ball.speed * deltaTime));
- ball.position.Y += (float)(Math.Sin(ball.angle) * (ball.speed * deltaTime));
- if (ball.position.Y < 0)
- {
- ball.angle = -ball.angle;
- ball.position.Y = 0.1f;
- }
- else if (ball.position.Y + ball.rect.Height > 600)
- {
- ball.angle = -ball.angle;
- ball.position.Y = (600 - ball.rect.Width - 0.1f);
- }
- if (ball.position.X < 0 && (isPlaying == true))
- {
- blue.point++;
- red.looser = true;
- Reset();
- }
- if (ball.position.X > 775 && (isPlaying == true))
- {
- red.point++;
- blue.looser = true;
- Reset();
- }
- red.Update();
- blue.Update();
- ball.Update();
- if (red.rect.Intersects(ball.rect))
- {
- ball.angle = MathHelper.Pi - ball.angle;
- ball.position.X = (red.position.X + red.rect.Width + 0.1f);
- ball.speed += 10;
- }
- else if (blue.rect.Intersects(ball.rect))
- {
- ball.angle = MathHelper.Pi - ball.angle;
- ball.position.X = (blue.position.X - ball.rect.Width - 0.1f);
- ball.speed += 10;
- }
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- spriteBatch.Begin();
- spriteBatch.Draw(background, new Vector2(0, 0), Color.White);
- spriteBatch.DrawString(spriteFont, "Czerwony: " + red.point, new Vector2(50, 10), Color.White);
- spriteBatch.DrawString(spriteFont, "Niebieski: " + blue.point, new Vector2(650, 10), Color.White);
- red.Draw(spriteBatch, redPaddle);
- blue.Draw(spriteBatch, bluePaddle);
- if(isPlaying == true)
- {
- ball.Draw(spriteBatch, ballText);
- if ((ball.position.X < 0) || (ball.position.X + ball.rect.Width > 800))
- isPlaying = false;
- }
- spriteBatch.End();
- base.Draw(gameTime);
- }
- public void Reset()
- {
- blue.position.X = 775;
- blue.position.Y = red.position.Y = 250;
- if (red.looser == true)
- ball.position.X += (float)(Math.Cos(ball.angle) * (ball.speed * deltaTime));
- else if (blue.looser == true)
- ball.position.X -= (float)(Math.Cos(ball.angle) * (ball.speed * deltaTime));
- ball.position.Y = 250;
- ball.angle = 10;
- ball.speed = 300;
- isPlaying = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement