Advertisement
Braber01

Final Programming Project

Dec 14th, 2016
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Graphics;
  8.  
  9. namespace BreakingOut {
  10.     public class Ball {
  11.         Vector2 motion;
  12.         Vector2 position;
  13.         Rectangle bounds;
  14.         float ballSpeed = 4;
  15.  
  16.         Texture2D texture;
  17.         Rectangle screenBounds;
  18.  
  19.         bool collided;
  20.  
  21.         public Rectangle Bounds {
  22.             get {
  23.                 bounds.X = (int) position.X;
  24.                 bounds.Y = (int) position.Y;
  25.                 return bounds;
  26.             }
  27.         }
  28.  
  29.         public Ball(Texture2D texture,
  30.                 Rectangle screenBounds) {
  31.             bounds = new Rectangle(0, 0,
  32.                 texture.Width,
  33.                 texture.Height);
  34.             this.texture = texture;
  35.             this.screenBounds = screenBounds;
  36.         }
  37.  
  38.         public void Update() {
  39.             collided = false;
  40.             position += motion * ballSpeed;
  41.             ballSpeed += 0.001f;
  42.             CheckWallCollsion();
  43.  
  44.         }
  45.  
  46.         private void CheckWallCollsion() {
  47.             if(position.X < 0) {
  48.                 position.X = 0;
  49.                 motion.X *= -1;
  50.             }
  51.             if (position.X + texture.Width > screenBounds.Width) {
  52.                 position.X = screenBounds.Width - texture.Width;
  53.                 motion.X *= -1;
  54.             }
  55.             if (position.Y < 0) {
  56.                 position.Y = 0;
  57.                 motion.Y *= -1;
  58.             }
  59.         }//check wall collision
  60.  
  61.         public void Deflection(Brick brick) {
  62.             if (!collided) {
  63.                 motion.Y *= -1;
  64.                 collided = true;
  65.             }
  66.         }
  67.  
  68.         const float BALL_START_SPEED = 8f;
  69.         public void SetInStartPosition(Rectangle paddleLocation) {
  70.             //motion = new Vector2(1, -1);
  71.             //position.Y = paddleLocation.Y - texture.Height;
  72.             //position.X = paddleLocation.X + (paddleLocation.Width - texture.Width) / 2;
  73.  
  74.             Random rand = new Random();
  75.             motion = new Vector2(rand.Next(2, 6),
  76.                 -rand.Next(2, 6));
  77.             motion.Normalize();
  78.             ballSpeed = BALL_START_SPEED;
  79.             position.Y = paddleLocation.Y - texture.Height;
  80.             position.X = paddleLocation.X + (paddleLocation.Width - texture.Width) / 2;
  81.         }
  82.  
  83.         public bool OffBottom() {
  84.             if (position.Y > screenBounds.Height)
  85.                 return true;
  86.             return false;
  87.         }
  88.  
  89.         public void PaddleCollision(Rectangle paddleLocation) {
  90.             Rectangle ballLocation = new Rectangle(
  91.                 (int) position.X,
  92.                 (int) position.Y,
  93.                 texture.Width,
  94.                 texture.Height);
  95.  
  96.             if (paddleLocation.Intersects(ballLocation)) {
  97.                 position.Y = paddleLocation.Y - texture.Height;
  98.                 motion.Y *= -1;
  99.             }
  100.         }//Paddle colision
  101.  
  102.         public void Draw(SpriteBatch spriteBatch) {
  103.             spriteBatch.Draw(texture, position, Color.White);
  104.         }
  105.  
  106.     }
  107. }
  108.  
  109.  
  110. using System;
  111. using System.Collections.Generic;
  112. using System.Linq;
  113. using System.Text;
  114. using BreakingOut.Components;
  115. using Microsoft.Xna.Framework;
  116. using Microsoft.Xna.Framework.Graphics;
  117.  
  118. namespace BreakingOut {
  119.     public class Brick {
  120.         Texture2D texture;
  121.         Rectangle location;
  122.         Color tint;
  123.         bool alive;
  124.  
  125.         //TODO: Manage lives here?!
  126.  
  127.         public Rectangle Location {
  128.             get { return location; }
  129.         }
  130.  
  131.         public Brick(Texture2D texture,
  132.                 Rectangle location,Color tint) {
  133.             this.texture = texture;
  134.             this.location = location;
  135.             this.tint = tint;
  136.             alive = true;
  137.         }
  138.  
  139.         public void CheckCollision(Ball ball) {
  140.             if(alive && ball.Bounds.Intersects(location)) {
  141.                 alive = false;
  142.                 ball.Deflection(this);
  143.             }
  144.         }
  145.  
  146.         public void Draw(SpriteBatch spriteBatch) {
  147.             if (alive)
  148.                 spriteBatch.Draw(texture, location, tint);
  149.         }
  150.     }
  151. }
  152.  
  153.  
  154. using System;
  155. using System.Collections.Generic;
  156. using System.Linq;
  157. using System.Text;
  158.  
  159. using Microsoft.Xna.Framework;
  160. using Microsoft.Xna.Framework.Graphics;
  161. using Microsoft.Xna.Framework.Input;
  162.  
  163. namespace BreakingOut {
  164.     public class Paddle {
  165.         Vector2 position;
  166.         Vector2 motion;
  167.         float paddleSpeed = 8f;
  168.  
  169.         KeyboardState keyboardState;
  170.         GamePadState gamePadState;
  171.  
  172.         Texture2D texture;
  173.         Rectangle screenBounds;
  174.  
  175.         public Paddle(Texture2D texture, Rectangle screenBounds) {
  176.             this.texture = texture;
  177.             this.screenBounds = screenBounds;
  178.             SetInStartPosition();
  179.         }
  180.  
  181.         public void Update() {
  182.             motion = Vector2.Zero;
  183.  
  184.             keyboardState = Keyboard.GetState();
  185.             gamePadState = GamePad.GetState(PlayerIndex.One);
  186.  
  187.             //TODO: Add gamePad Functionality later
  188.             if (keyboardState.IsKeyDown(Keys.Left))
  189.                 motion.X = -1;
  190.             if (keyboardState.IsKeyDown(Keys.Right))
  191.                 motion.X = 1;
  192.             motion.X *= paddleSpeed;
  193.             position += motion;
  194.             LockPaddle();
  195.         }
  196.  
  197.         private void LockPaddle() {
  198.             if (position.X < 0)
  199.                 position.X = 0;
  200.             if (position.X + texture.Width > screenBounds.Width)
  201.                 position.X = screenBounds.Width - texture.Width;
  202.         }
  203.  
  204.         public void SetInStartPosition() {
  205.             position.X = (screenBounds.Width - texture.Width) / 2;
  206.             position.Y = screenBounds.Height - texture.Height - 5;
  207.         }
  208.  
  209.         public void Draw(SpriteBatch spriteBatch) {
  210.             spriteBatch.Draw(texture, position, Color.White);
  211.         }
  212.  
  213.         public Rectangle GetBounds() {
  214.             return new Rectangle(
  215.                 (int) position.X,
  216.                 (int) position.Y,
  217.                 texture.Width,
  218.                 texture.Height);
  219.         }
  220.     }
  221. }
  222.  
  223.  
  224. using System;
  225. using System.Collections.Generic;
  226. using System.Linq;
  227. using Microsoft.Xna.Framework;
  228. using Microsoft.Xna.Framework.Audio;
  229. using Microsoft.Xna.Framework.Content;
  230. using Microsoft.Xna.Framework.GamerServices;
  231. using Microsoft.Xna.Framework.Graphics;
  232. using Microsoft.Xna.Framework.Input;
  233. using Microsoft.Xna.Framework.Media;
  234.  
  235. namespace BreakingOut {
  236.     /// <summary>
  237.     /// This is the main type for your game
  238.     /// </summary>
  239.     public class Game1 : Game {
  240.         public GraphicsDeviceManager graphics;
  241.         public SpriteBatch spriteBatch;
  242.  
  243.         Paddle paddle;
  244.         public Rectangle screenRectangle;
  245.  
  246.         Ball ball;
  247.         int bricksWide = 10;
  248.         int bricksHigh = 5;
  249.         Texture2D brickImage;
  250.         Brick[,] bricks;
  251.         public Game1() {
  252.             graphics = new GraphicsDeviceManager(this);
  253.             Content.RootDirectory = "Content";
  254.  
  255.             graphics.PreferredBackBufferWidth = 750;
  256.             graphics.PreferredBackBufferHeight = 600;
  257.  
  258.             screenRectangle = new Rectangle(
  259.                 0, 0,
  260.                 graphics.PreferredBackBufferWidth,
  261.                 graphics.PreferredBackBufferHeight);
  262.         }
  263.  
  264.         /// <summary>
  265.         /// Allows the game to perform any initialization it needs to before starting to run.
  266.         /// This is where it can query for any required services and load any non-graphic
  267.         /// related content.  Calling base.Initialize will enumerate through any components
  268.         /// and initialize them as well.
  269.         /// </summary>
  270.         protected override void Initialize() {
  271.             // TODO: Add your initialization logic here
  272.  
  273.             base.Initialize();
  274.         }
  275.  
  276.         /// <summary>
  277.         /// LoadContent will be called once per game and is the place to load
  278.         /// all of your content.
  279.         /// </summary>
  280.         protected override void LoadContent() {
  281.             // Create a new SpriteBatch, which can be used to draw textures.
  282.             spriteBatch = new SpriteBatch(GraphicsDevice);
  283.  
  284.             Texture2D tempTexture = Content.Load<Texture2D>("paddle");
  285.             paddle = new Paddle(tempTexture, screenRectangle);
  286.  
  287.             tempTexture = Content.Load<Texture2D>("ball");
  288.             ball = new Ball(tempTexture, screenRectangle);
  289.  
  290.             brickImage = Content.Load<Texture2D>("brick");
  291.  
  292.             StartGame();
  293.             // TODO: use this.Content to load your game content here
  294.         }
  295.  
  296.         private void StartGame() {
  297.             paddle.SetInStartPosition();
  298.             ball.SetInStartPosition(
  299.                 paddle.GetBounds());
  300.  
  301.             bricks = new Brick[bricksWide,
  302.                 bricksHigh];
  303.  
  304.             for (int y = 0; y < bricksHigh; y++) {
  305.                 Color tint = Color.White;
  306.                 switch (y) {
  307.                     case 0:
  308.                         tint = Color.Blue;
  309.                         break;
  310.                     case 1:
  311.                         tint = Color.Red;
  312.                         break;
  313.                     case 2:
  314.                         tint = Color.Green;
  315.                         break;
  316.                     case 3:
  317.                         tint = Color.Yellow;
  318.                         break;
  319.                     case 4:
  320.                         tint = Color.Purple;
  321.                         break;
  322.                 }//end switch
  323.  
  324.                 for(int x = 0; x < bricksWide; x++) {
  325.                     bricks[x, y] = new Brick(
  326.                         brickImage,
  327.                             new Rectangle(
  328.                                 x * brickImage.Width,
  329.                                 y * brickImage.Height,
  330.                                 brickImage.Width,
  331.                                 brickImage.Height),
  332.                             tint);
  333.                 } //for x
  334.             }//for y
  335.         }//StartGame
  336.  
  337.         /// <summary>
  338.         /// UnloadContent will be called once per game and is the place to unload
  339.         /// all content.
  340.         /// </summary>
  341.         protected override void UnloadContent() {
  342.             // TODO: Unload any non ContentManager content here
  343.         }
  344.  
  345.         /// <summary>
  346.         /// Allows the game to run logic such as updating the world,
  347.         /// checking for collisions, gathering input, and playing audio.
  348.         /// </summary>
  349.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  350.         protected override void Update(GameTime gameTime) {
  351.             // Allows the game to exit
  352.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  353.                 this.Exit();
  354.  
  355.             // TODO: Add your update logic here
  356.  
  357.             paddle.Update();
  358.             ball.Update();
  359.  
  360.             foreach (Brick brick in bricks) {
  361.                 brick.CheckCollision(ball);
  362.  
  363.             }
  364.  
  365.             ball.PaddleCollision(paddle.GetBounds());
  366.  
  367.             if (ball.OffBottom())
  368.                 StartGame();
  369.             //TODO: add a life system
  370.  
  371.             base.Update(gameTime);
  372.         }
  373.  
  374.         /// <summary>
  375.         /// This is called when the game should draw itself.
  376.         /// </summary>
  377.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  378.         protected override void Draw(GameTime gameTime) {
  379.             GraphicsDevice.Clear(Color.CornflowerBlue);
  380.  
  381.             // TODO: Add your drawing code here
  382.             spriteBatch.Begin();
  383.             paddle.Draw(spriteBatch);
  384.             ball.Draw(spriteBatch);
  385.  
  386.             foreach (Brick brick in bricks)
  387.                 brick.Draw(spriteBatch);
  388.             spriteBatch.End();
  389.             base.Draw(gameTime);
  390.         }
  391.     }
  392. }
  393.  
  394.  
  395. using System;
  396.  
  397. namespace BreakingOut {
  398. #if WINDOWS || XBOX
  399.     static class Program
  400.     {
  401.         /// <summary>
  402.         /// The main entry point for the application.
  403.         /// </summary>
  404.         static void Main(string[] args)
  405.         {
  406.             using (Game1 game = new Game1())
  407.             {
  408.                 game.Run();
  409.             }
  410.         }
  411.     }
  412. #endif
  413. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement