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 Breakout
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- //varibles
- bool ballLaunch = false;
- int currentMoveX = 10; //Crappy name for a varible
- int currentMoveY = 10;
- //constants
- const int WINDOW_WIDTH = 800;
- const int WINDOW_HEIGHT = 600;
- //drawing support
- Texture2D paddle;
- Texture2D ball;
- Texture2D block;
- Rectangle ballBox;
- Rectangle paddleBox;
- Rectangle b1Rect;
- Rectangle testRect;
- //lists
- List<Block> blocklist = new List<Block>();
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- //changes the resolution (800 by 600)
- graphics.PreferredBackBufferWidth = 800;
- graphics.PreferredBackBufferHeight = 600;
- }
- /// <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();
- }
- /// <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);
- //loads textures in
- paddle = Content.Load<Texture2D>("Paddle");
- ball = Content.Load<Texture2D>("Ball");
- block = Content.Load<Texture2D>("Block");
- //blocks
- Block b1 = new Block(500, 500);
- //lists
- //controls for the for loop
- int xChange = 0;
- int yChange = 0;
- //loop iterates 10 times as a test
- for (int i = 0; i == 10; i++)
- {
- rectList.Add(new Rectangle(100+xChange, 200+ yChange, 40, 80));
- xChange += 100;
- yChange += 50;
- }
- paddleBox = new Rectangle(500,550,200,40);
- ballBox = new Rectangle(paddleBox.X, paddleBox.Y - paddleBox.Height+ 15, 25, 25); //no idea why 15 works it just does
- }
- /// <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
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
- // TODO: Add your update logic here
- //MouseState, for use to draw player at the mouse
- MouseState mouse = Mouse.GetState();
- IsMouseVisible = true;
- paddleBox.X = mouse.X-paddleBox.Width/2;
- //clamps the paddle to the screen
- if (paddleBox.X < 0)
- {
- paddleBox.X = 0;
- }
- if (paddleBox.X > WINDOW_WIDTH - 200)
- {
- paddleBox.X = WINDOW_WIDTH - paddleBox.Width;
- }
- if (!ballLaunch)
- {
- ballBox.X = paddleBox.X + (paddleBox.Width / 2)-ballBox.Width/2;
- ballBox.Y = paddleBox.Y - paddleBox.Height + 15;
- }
- if (mouse.LeftButton == ButtonState.Pressed)
- {
- ballLaunch = true;
- }
- if (ballBox.Y > WINDOW_HEIGHT + 50)
- {
- ballLaunch = false;
- }
- //makes the ball move and bounce off things
- if(ballLaunch)
- {
- ballBox.X += currentMoveX;
- ballBox.Y -= currentMoveY;
- if (ballBox.X > WINDOW_WIDTH || ballBox.X <0)
- {
- currentMoveX *= -1;
- }
- if (ballBox.Y < 0)
- {
- currentMoveY *= -1;
- }
- if (ballBox.Intersects(paddleBox))
- {
- currentMoveY *= -1;
- }
- foreach (Rectangle rect in rectList)
- {
- if (ballBox.Intersects(rect))
- {
- // currentMoveX *= -1;
- currentMoveY *= -1;
- }
- }
- }
- 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(paddle, paddleBox, Color.Red);
- spriteBatch.Draw(ball, ballBox, Color.Black);
- foreach (Rectangle rect in rectList)
- {
- spriteBatch.Draw(block, rect, Color.White);
- }
- spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement