Advertisement
Guest User

breakout clone (WIP)

a guest
Feb 8th, 2014
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11.  
  12. namespace Breakout
  13. {
  14.     /// <summary>
  15.     /// This is the main type for your game
  16.     /// </summary>
  17.     public class Game1 : Microsoft.Xna.Framework.Game
  18.     {
  19.         GraphicsDeviceManager graphics;
  20.         SpriteBatch spriteBatch;
  21.         //varibles
  22.         bool ballLaunch = false;
  23.         int currentMoveX = 10; //Crappy name for a varible
  24.         int currentMoveY = 10;
  25.         //constants
  26.         const int WINDOW_WIDTH = 800;
  27.         const int WINDOW_HEIGHT = 600;
  28.         //drawing support
  29.         Texture2D paddle;
  30.         Texture2D ball;
  31.         Texture2D block;
  32.  
  33.         Rectangle ballBox;
  34.         Rectangle paddleBox;
  35.         Rectangle b1Rect;
  36.         Rectangle testRect;
  37.        
  38.         //lists
  39.         List<Block> blocklist = new List<Block>();
  40.         public Game1()
  41.         {
  42.             graphics = new GraphicsDeviceManager(this);
  43.             Content.RootDirectory = "Content";
  44.             //changes the resolution (800 by 600)
  45.             graphics.PreferredBackBufferWidth = 800;
  46.             graphics.PreferredBackBufferHeight = 600;
  47.         }
  48.  
  49.         /// <summary>
  50.         /// Allows the game to perform any initialization it needs to before starting to run.
  51.         /// This is where it can query for any required services and load any non-graphic
  52.         /// related content.  Calling base.Initialize will enumerate through any components
  53.         /// and initialize them as well.
  54.         /// </summary>
  55.         protected override void Initialize()
  56.         {
  57.             // TODO: Add your initialization logic here
  58.  
  59.             base.Initialize();
  60.         }
  61.  
  62.         /// <summary>
  63.         /// LoadContent will be called once per game and is the place to load
  64.         /// all of your content.
  65.         /// </summary>
  66.         protected override void LoadContent()
  67.         {
  68.             // Create a new SpriteBatch, which can be used to draw textures.
  69.             spriteBatch = new SpriteBatch(GraphicsDevice);
  70.             //loads textures in
  71.             paddle = Content.Load<Texture2D>("Paddle");
  72.             ball = Content.Load<Texture2D>("Ball");
  73.             block = Content.Load<Texture2D>("Block");
  74.             //blocks
  75.            
  76.             Block b1 = new Block(500, 500);
  77.             //lists
  78.            
  79.             //controls for the for loop
  80.             int xChange = 0;
  81.             int yChange = 0;
  82.             //loop iterates 10 times as a test
  83.             for (int i = 0; i == 10; i++)
  84.             {
  85.                 rectList.Add(new Rectangle(100+xChange, 200+ yChange, 40, 80));
  86.                 xChange += 100;
  87.                 yChange += 50;
  88.             }
  89.             paddleBox = new Rectangle(500,550,200,40);
  90.            
  91.             ballBox = new Rectangle(paddleBox.X, paddleBox.Y - paddleBox.Height+ 15, 25, 25); //no idea why 15 works it just does
  92.         }
  93.         /// <summary>
  94.         /// UnloadContent will be called once per game and is the place to unload
  95.         /// all content.
  96.         /// </summary>
  97.         protected override void UnloadContent()
  98.         {
  99.             // TODO: Unload any non ContentManager content here
  100.         }
  101.  
  102.         /// <summary>
  103.         /// Allows the game to run logic such as updating the world,
  104.         /// checking for collisions, gathering input, and playing audio.
  105.         /// </summary>
  106.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  107.         protected override void Update(GameTime gameTime)
  108.         {
  109.             // Allows the game to exit
  110.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  111.                 this.Exit();
  112.  
  113.             // TODO: Add your update logic here
  114.             //MouseState, for use to draw player at the mouse
  115.             MouseState mouse = Mouse.GetState();
  116.             IsMouseVisible = true;
  117.             paddleBox.X = mouse.X-paddleBox.Width/2;
  118.             //clamps the paddle to the screen
  119.             if (paddleBox.X < 0)
  120.             {
  121.                 paddleBox.X = 0;
  122.             }
  123.             if (paddleBox.X > WINDOW_WIDTH - 200)
  124.             {
  125.                 paddleBox.X = WINDOW_WIDTH - paddleBox.Width;
  126.             }
  127.             if (!ballLaunch)
  128.             {
  129.                 ballBox.X = paddleBox.X + (paddleBox.Width / 2)-ballBox.Width/2;
  130.                 ballBox.Y = paddleBox.Y - paddleBox.Height + 15;
  131.             }
  132.             if (mouse.LeftButton == ButtonState.Pressed)
  133.             {
  134.                 ballLaunch = true;
  135.             }
  136.             if (ballBox.Y > WINDOW_HEIGHT + 50)
  137.             {
  138.                 ballLaunch = false;
  139.             }
  140.             //makes the ball move and bounce off things
  141.             if(ballLaunch)
  142.             {
  143.                 ballBox.X += currentMoveX;
  144.                 ballBox.Y -= currentMoveY;
  145.                 if (ballBox.X > WINDOW_WIDTH || ballBox.X <0)
  146.                 {
  147.                     currentMoveX *= -1;
  148.                 }
  149.                 if (ballBox.Y < 0)
  150.                 {
  151.                     currentMoveY *= -1;
  152.                 }
  153.                 if (ballBox.Intersects(paddleBox))
  154.                 {
  155.                     currentMoveY *= -1;
  156.                 }
  157.                 foreach (Rectangle rect in rectList)
  158.                 {
  159.                     if (ballBox.Intersects(rect))
  160.                     {
  161.                       //  currentMoveX *= -1;
  162.                         currentMoveY *= -1;
  163.                     }
  164.                 }
  165.             }
  166.             base.Update(gameTime);
  167.         }
  168.  
  169.         /// <summary>
  170.         /// This is called when the game should draw itself.
  171.         /// </summary>
  172.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  173.         protected override void Draw(GameTime gameTime)
  174.         {
  175.             GraphicsDevice.Clear(Color.CornflowerBlue);
  176.             spriteBatch.Begin();
  177.             spriteBatch.Draw(paddle, paddleBox, Color.Red);
  178.             spriteBatch.Draw(ball, ballBox, Color.Black);
  179.             foreach (Rectangle rect in rectList)
  180.             {
  181.                 spriteBatch.Draw(block, rect,  Color.White);
  182.             }
  183.             spriteBatch.End();
  184.             base.Draw(gameTime);
  185.         }
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement