Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- namespace BreakingOut {
- public class Ball {
- Vector2 motion;
- Vector2 position;
- Rectangle bounds;
- float ballSpeed = 4;
- Texture2D texture;
- Rectangle screenBounds;
- bool collided;
- public Rectangle Bounds {
- get {
- bounds.X = (int) position.X;
- bounds.Y = (int) position.Y;
- return bounds;
- }
- }
- public Ball(Texture2D texture,
- Rectangle screenBounds) {
- bounds = new Rectangle(0, 0,
- texture.Width,
- texture.Height);
- this.texture = texture;
- this.screenBounds = screenBounds;
- }
- public void Update() {
- collided = false;
- position += motion * ballSpeed;
- ballSpeed += 0.001f;
- CheckWallCollsion();
- }
- private void CheckWallCollsion() {
- if(position.X < 0) {
- position.X = 0;
- motion.X *= -1;
- }
- if (position.X + texture.Width > screenBounds.Width) {
- position.X = screenBounds.Width - texture.Width;
- motion.X *= -1;
- }
- if (position.Y < 0) {
- position.Y = 0;
- motion.Y *= -1;
- }
- }//check wall collision
- public void Deflection(Brick brick) {
- if (!collided) {
- motion.Y *= -1;
- collided = true;
- }
- }
- const float BALL_START_SPEED = 8f;
- public void SetInStartPosition(Rectangle paddleLocation) {
- //motion = new Vector2(1, -1);
- //position.Y = paddleLocation.Y - texture.Height;
- //position.X = paddleLocation.X + (paddleLocation.Width - texture.Width) / 2;
- Random rand = new Random();
- motion = new Vector2(rand.Next(2, 6),
- -rand.Next(2, 6));
- motion.Normalize();
- ballSpeed = BALL_START_SPEED;
- position.Y = paddleLocation.Y - texture.Height;
- position.X = paddleLocation.X + (paddleLocation.Width - texture.Width) / 2;
- }
- public bool OffBottom() {
- if (position.Y > screenBounds.Height)
- return true;
- return false;
- }
- public void PaddleCollision(Rectangle paddleLocation) {
- Rectangle ballLocation = new Rectangle(
- (int) position.X,
- (int) position.Y,
- texture.Width,
- texture.Height);
- if (paddleLocation.Intersects(ballLocation)) {
- position.Y = paddleLocation.Y - texture.Height;
- motion.Y *= -1;
- }
- }//Paddle colision
- public void Draw(SpriteBatch spriteBatch) {
- spriteBatch.Draw(texture, position, Color.White);
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using BreakingOut.Components;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- namespace BreakingOut {
- public class Brick {
- Texture2D texture;
- Rectangle location;
- Color tint;
- bool alive;
- //TODO: Manage lives here?!
- public Rectangle Location {
- get { return location; }
- }
- public Brick(Texture2D texture,
- Rectangle location,Color tint) {
- this.texture = texture;
- this.location = location;
- this.tint = tint;
- alive = true;
- }
- public void CheckCollision(Ball ball) {
- if(alive && ball.Bounds.Intersects(location)) {
- alive = false;
- ball.Deflection(this);
- }
- }
- public void Draw(SpriteBatch spriteBatch) {
- if (alive)
- spriteBatch.Draw(texture, location, tint);
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- namespace BreakingOut {
- public class Paddle {
- Vector2 position;
- Vector2 motion;
- float paddleSpeed = 8f;
- KeyboardState keyboardState;
- GamePadState gamePadState;
- Texture2D texture;
- Rectangle screenBounds;
- public Paddle(Texture2D texture, Rectangle screenBounds) {
- this.texture = texture;
- this.screenBounds = screenBounds;
- SetInStartPosition();
- }
- public void Update() {
- motion = Vector2.Zero;
- keyboardState = Keyboard.GetState();
- gamePadState = GamePad.GetState(PlayerIndex.One);
- //TODO: Add gamePad Functionality later
- if (keyboardState.IsKeyDown(Keys.Left))
- motion.X = -1;
- if (keyboardState.IsKeyDown(Keys.Right))
- motion.X = 1;
- motion.X *= paddleSpeed;
- position += motion;
- LockPaddle();
- }
- private void LockPaddle() {
- if (position.X < 0)
- position.X = 0;
- if (position.X + texture.Width > screenBounds.Width)
- position.X = screenBounds.Width - texture.Width;
- }
- public void SetInStartPosition() {
- position.X = (screenBounds.Width - texture.Width) / 2;
- position.Y = screenBounds.Height - texture.Height - 5;
- }
- public void Draw(SpriteBatch spriteBatch) {
- spriteBatch.Draw(texture, position, Color.White);
- }
- public Rectangle GetBounds() {
- return new Rectangle(
- (int) position.X,
- (int) position.Y,
- texture.Width,
- texture.Height);
- }
- }
- }
- 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 BreakingOut {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Game {
- public GraphicsDeviceManager graphics;
- public SpriteBatch spriteBatch;
- Paddle paddle;
- public Rectangle screenRectangle;
- Ball ball;
- int bricksWide = 10;
- int bricksHigh = 5;
- Texture2D brickImage;
- Brick[,] bricks;
- public Game1() {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- graphics.PreferredBackBufferWidth = 750;
- graphics.PreferredBackBufferHeight = 600;
- screenRectangle = new Rectangle(
- 0, 0,
- graphics.PreferredBackBufferWidth,
- graphics.PreferredBackBufferHeight);
- }
- /// <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);
- Texture2D tempTexture = Content.Load<Texture2D>("paddle");
- paddle = new Paddle(tempTexture, screenRectangle);
- tempTexture = Content.Load<Texture2D>("ball");
- ball = new Ball(tempTexture, screenRectangle);
- brickImage = Content.Load<Texture2D>("brick");
- StartGame();
- // TODO: use this.Content to load your game content here
- }
- private void StartGame() {
- paddle.SetInStartPosition();
- ball.SetInStartPosition(
- paddle.GetBounds());
- bricks = new Brick[bricksWide,
- bricksHigh];
- for (int y = 0; y < bricksHigh; y++) {
- Color tint = Color.White;
- switch (y) {
- case 0:
- tint = Color.Blue;
- break;
- case 1:
- tint = Color.Red;
- break;
- case 2:
- tint = Color.Green;
- break;
- case 3:
- tint = Color.Yellow;
- break;
- case 4:
- tint = Color.Purple;
- break;
- }//end switch
- for(int x = 0; x < bricksWide; x++) {
- bricks[x, y] = new Brick(
- brickImage,
- new Rectangle(
- x * brickImage.Width,
- y * brickImage.Height,
- brickImage.Width,
- brickImage.Height),
- tint);
- } //for x
- }//for y
- }//StartGame
- /// <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
- paddle.Update();
- ball.Update();
- foreach (Brick brick in bricks) {
- brick.CheckCollision(ball);
- }
- ball.PaddleCollision(paddle.GetBounds());
- if (ball.OffBottom())
- StartGame();
- //TODO: add a life system
- 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);
- // TODO: Add your drawing code here
- spriteBatch.Begin();
- paddle.Draw(spriteBatch);
- ball.Draw(spriteBatch);
- foreach (Brick brick in bricks)
- brick.Draw(spriteBatch);
- spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- }
- using System;
- namespace BreakingOut {
- #if WINDOWS || XBOX
- static class Program
- {
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- static void Main(string[] args)
- {
- using (Game1 game = new Game1())
- {
- game.Run();
- }
- }
- }
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement