Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Game1.cs */
- 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 GroundZero_sPong {
- public class Game1 : Microsoft.Xna.Framework.Game {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- KeyboardState _KBS;
- KeyboardState oldKBS;
- SpriteFont helpFont, score, info;
- Texture2D background, padTexture, ball;
- Vector2 ballPos, ballMvmnt, leftScorePos, rightScorePos, helpPos, helpOrgn, infoPos, infoOrgn;
- Rectangle borders, left, right;
- SoundEffect pongSound, scoreSound, victorySound;
- bool paused, endgame;
- byte speed;
- const int maxScore = 25;
- string helpString, infoString;
- ball Ball;
- pad Left;
- pad Right;
- public Game1() {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- }
- protected override void Initialize() {
- #region WindowSize
- graphics.PreferredBackBufferWidth = 800;
- graphics.PreferredBackBufferHeight = 500;
- graphics.IsFullScreen = false;
- graphics.ApplyChanges();
- borders = new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
- #endregion
- #region HelpInfo
- helpString = "";
- helpPos = new Vector2(graphics.PreferredBackBufferWidth / 2, graphics.PreferredBackBufferHeight / 2);
- infoString = "";
- infoPos = new Vector2(graphics.PreferredBackBufferWidth / 2, 100);
- #endregion
- #region Pads
- left = new Rectangle(15, 0, 10, 50); // 15 spacing
- right = new Rectangle(borders.Right - 15 - 10, 0, 10, 50);
- speed = 5;
- #endregion
- #region ball
- ballMvmnt = new Vector2(4, -4);
- ballPos = new Vector2(borders.Width / 2, borders.Height / 2);
- #endregion
- #region score
- leftScorePos = new Vector2(10, 5);
- rightScorePos = new Vector2(graphics.PreferredBackBufferWidth - 10, 5);
- #endregion
- paused = true;
- endgame = false;
- oldKBS = Keyboard.GetState();
- base.Initialize();
- }
- protected override void LoadContent() {
- spriteBatch = new SpriteBatch(GraphicsDevice);
- helpFont = Content.Load<SpriteFont>("help");
- score = Content.Load<SpriteFont>("score");
- info = Content.Load<SpriteFont>("info");
- background = Content.Load<Texture2D>("background");
- padTexture = Content.Load<Texture2D>("pad");
- ball = Content.Load<Texture2D>("ball");
- pongSound = Content.Load<SoundEffect>("pongSound");
- scoreSound = Content.Load<SoundEffect>("scoreSound");
- victorySound = Content.Load<SoundEffect>("victorySound");
- Ball = new ball(10, borders, ballPos, ballMvmnt, scoreSound, pongSound, true);
- Left = new pad(borders, left, speed, true);
- Right = new pad(borders, right, speed, true);
- }
- protected override void UnloadContent() {
- // TODO: Unload any non ContentManager content here
- }
- protected override void Update(GameTime gameTime) {
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
- _KBS = Keyboard.GetState();
- #region Game
- if (_KBS.IsKeyDown(Keys.F1) || _KBS.IsKeyDown(Keys.Space)) {
- if (!oldKBS.IsKeyDown(Keys.F1) && !oldKBS.IsKeyDown(Keys.Space)) {
- Ball.Pause();
- Left.Pause();
- Right.Pause();
- paused = !paused;
- }
- }
- if (paused) {
- infoString = "GAME PAUSED";
- helpString = "Move the left paddle with W/Z & S and the right paddle with the up & down arrow key." + Environment.NewLine +
- "First one to reach " + maxScore + " points wins!" + Environment.NewLine +
- Environment.NewLine + "Pauze and resume by pressing space or F1.";
- } else {
- infoString = "";
- helpString = "";
- #region left
- if (_KBS.IsKeyDown(Keys.W) || _KBS.IsKeyDown(Keys.Z))
- Left.MoveUp();
- else if (_KBS.IsKeyDown(Keys.S))
- Left.MoveDown();
- #endregion
- #region right
- if (_KBS.IsKeyDown(Keys.Up))
- Right.MoveUp();
- else if (_KBS.IsKeyDown(Keys.Down))
- Right.MoveDown();
- #endregion
- }
- #endregion
- #region endGame
- if (Ball.ScoreLeft >= maxScore) {
- infoString = "Congratulations Player 1" + Environment.NewLine + "You won the game!";
- if (!endgame) {
- victorySound.Play();
- endgame = true;
- }
- } else if (Ball.ScoreRight >= maxScore) {
- infoString = "Congratulations Player 2" + Environment.NewLine + "You won the game!";
- if (!endgame) {
- victorySound.Play();
- endgame = true;
- }
- }
- if (endgame) {
- ballPos = new Vector2(borders.Width / 2, borders.Height / 2);
- Ball.Pause();
- Left.Pause();
- Right.Pause();
- helpString = "Press space to restart";
- if (_KBS.IsKeyDown(Keys.Space)) {
- Game1 GameX = new Game1();
- this.Exit();
- }
- }
- #endregion
- // update keyboardstate
- oldKBS = _KBS;
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime) {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- helpOrgn = helpFont.MeasureString(helpString) / 2;
- infoOrgn = info.MeasureString(infoString) / 2;
- string scoreLeft = Ball.ScoreLeft.ToString();
- string scoreRight = Ball.ScoreRight.ToString();
- rightScorePos.X = graphics.PreferredBackBufferWidth - 10 - score.MeasureString(scoreRight).X;
- spriteBatch.Begin();
- spriteBatch.Draw(background, new Vector2(0), Color.Green);
- spriteBatch.DrawString(helpFont, helpString, helpPos, Color.White, 0, helpOrgn, 1, SpriteEffects.None, 0);
- spriteBatch.DrawString(info, infoString, infoPos, Color.Tomato, 0, infoOrgn, 1, SpriteEffects.None, 0);
- spriteBatch.DrawString(score, scoreLeft, leftScorePos, Color.White);
- spriteBatch.DrawString(score, scoreRight, rightScorePos, Color.White);
- spriteBatch.End();
- Ball.Draw(spriteBatch, ball, Color.White, Left.recPad, Right.recPad);
- Left.Draw(spriteBatch, padTexture, Color.White);
- Right.Draw(spriteBatch, padTexture, Color.White);
- base.Draw(gameTime);
- }
- }
- }
- /* pad.cs */
- 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 GroundZero_sPong {
- class pad {
- private Rectangle borders;
- public Rectangle recPad;
- private byte speed;
- private bool paused;
- public pad(Rectangle gameBorders, Rectangle recPad, byte speed, bool startPaused) {
- this.borders = gameBorders;
- this.recPad = recPad;
- this.speed = speed;
- if (startPaused) Pause();
- }
- /* This made me unable to move the recPad (I used RecPad where I now use recPad)
- public Rectangle RecPad {
- get { return recPad; }
- set { recPad = value; }
- } */
- public void Pause() {
- paused = !paused;
- }
- public void MoveUp() {
- if (!paused)
- recPad.Offset(0, -speed);
- CheckBorders();
- }
- public void MoveDown() {
- if (!paused)
- recPad.Offset(0, speed);
- CheckBorders();
- }
- private void CheckBorders() {
- MathHelper.Clamp(recPad.Y, borders.Top, borders.Bottom - recPad.Height);
- }
- public void Draw(SpriteBatch sBatch, Texture2D texture, Color color) {
- sBatch.Begin();
- sBatch.Draw(texture, recPad, color);
- sBatch.End();
- }
- }/*pad*/
- }/*GroundZero_sPong*/
- /* ball.cs */
- 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 GroundZero_sPong {
- class ball {
- private Vector2 movement, movementSaved;
- private Rectangle borders, myBounds, left, right;
- SoundEffect score, bounce;
- private byte scoreLeft, scoreRight;
- private bool paused;
- public byte ScoreLeft {
- get { return scoreLeft; }
- private set { scoreLeft = value; }
- }
- public byte ScoreRight {
- get { return scoreRight; }
- private set { scoreRight = value; }
- }
- public ball(byte radius, Rectangle borders, Vector2 topLeft, Vector2 movement, SoundEffect score, SoundEffect bounce, bool startPaused) {
- this.borders = borders;
- this.movement = movement;
- this.score = score;
- this.bounce = bounce;
- myBounds = new Rectangle((int)topLeft.X, (int)topLeft.Y, radius, radius);
- if (startPaused) Pause();
- }
- public void Pause() {
- if (paused) { // unpause
- movement = movementSaved;
- paused = false;
- } else { // pause
- movementSaved = movement;
- movement = new Vector2(0);
- paused = true;
- }
- }
- private void Move() {
- myBounds.Offset((int)movement.X, (int)movement.Y);
- if (myBounds.Top < borders.Top) {
- myBounds.Offset(-borders.Top, 0);
- movement.Y *= -1;
- } else if (myBounds.Bottom > borders.Bottom) {
- myBounds.Offset(borders.Bottom - myBounds.Bottom, 0);
- movement.Y *= -1;
- }
- CheckBounce();
- CheckScore();
- }
- private void CheckScore() {
- bool scored = false;
- if (myBounds.Left < borders.Left) {
- ScoreLeft++;
- myBounds.Location = new Point(left.Right + 1, right.Center.Y);
- scored = true;
- } else if (myBounds.Right > borders.Right) {
- ScoreRight++;
- myBounds.Location = new Point(right.Left - myBounds.Width - 1, right.Center.Y);
- scored = true;
- }
- if (scored) {
- score.Play();
- movement.X *= -1;
- }
- }
- public void CheckBounce() {
- if ((myBounds.Intersects(left) && movement.X < 0) || (myBounds.Intersects(right) && movement.X > 0)) {
- movement.X *= -1;
- bounce.Play();
- }
- }
- public void Draw(SpriteBatch sBatch, Texture2D texture, Color color, Rectangle left, Rectangle right) {
- this.left = left;
- this.right = right;
- Move();
- sBatch.Begin();
- sBatch.Draw(texture, myBounds, color);
- sBatch.End();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement