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 BreakOutClone
- {
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- Texture2D levelImage;
- List<int> BlockPlacement = new List<int>();
- bool checkHex(int inty)
- {
- if (inty > 127)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- List<int> PixelToIDs(Texture2D tex)
- {
- Color[] texColors = new Color[tex.Height * tex.Width];
- tex.GetData(texColors);
- List<int> numbers = new List<int>();
- foreach (Color c in texColors)
- {
- if (checkHex(c.A))
- {
- bool R = checkHex(c.R);
- bool B = checkHex(c.B);
- bool G = checkHex(c.G);
- int numb = 0;
- if (R & B & G)
- {
- //white
- numb = 9;
- }
- else if (R & !B & !G)
- {
- //red
- numb = 5;
- if (RNG.Next(1, 100) < 23)//23% of mines
- {
- numb = 100;
- }
- numb = 100;//takes out the randomness (To submit to jim, so it's easier for him to see what each block does)
- }
- else if (B & !R & !G)
- {
- //blue
- numb = 8;
- }
- else if (G & !R & !B)
- {
- //green
- numb = 7;
- if (RNG.Next(1, 100) < 23)//23% of powerup block
- {
- numb = 102;
- }
- numb = 102;//takes out the randomness (To submit to jim, so it's easier for him to see what each block does)
- }
- else if (!R & !B & !G)
- {
- //black
- numb = 4;
- }
- else if (!R)
- {
- //turqoise
- numb = 1;
- }
- else if (!G)
- {
- //Purple
- numb = 2;
- }
- else if (!B)
- {
- //yelow
- numb = 6;
- if (RNG.Next(1, 100) < 23)//23% of Stars
- {
- numb = 101;
- }
- numb = 101;//takes out the randomness (To submit to jim, so it's easier for him to see what each block does)
- }
- numbers.Add(numb);
- }
- else
- {
- numbers.Add(0);
- }
- }
- return numbers;
- }
- SpriteFont scoreFont;
- KeyboardState keyboardState;
- public List<int> powerUpsID = new List<int>();
- public int score = 0;
- public string testString = "";
- int timeSinceLastEnter = 0;
- public string powerUpsTextPut = "";
- public string getPowerUpName(int id)
- {
- switch (id)
- {
- case 1:
- return "Slow ball";
- case 2:
- return "Fast Ball";
- case 3:
- return "+1 Life";
- case 4:
- return "Big Ball";
- case 5:
- return "Small Ball";
- case 6:
- return "Dangerous Ball";
- case 7:
- return "Split Balls!";
- default:
- return "Unknown..Crap";
- }
- }
- public void asorbePowerUp(int powerUP)
- {
- if (powerUP == 100)
- {
- Random random = new Random();
- score += random.Next(100, 200);
- }
- else if (powerUP == 200)
- {
- Random random = new Random();
- score -= random.Next(200, 400);
- }
- else
- {
- powerUpsID.Add(powerUP);
- updatePowerUpList();
- }
- }
- public void spawnPercisePowerUps(Vector2 position, int Type123)
- {
- Random random = new Random();
- int spawnAmount = random.Next(3, 8);
- if (Type123 == 102)
- {
- //So less powerup fall
- spawnAmount = random.Next(3, 5);
- }
- for (int i = 0; i < spawnAmount; i++)
- {
- switch (Type123)
- {
- case 102:
- powerUps.Add(new PowerUps(Content.Load<Texture2D>(@"Images\PowerUps"), position, this, Convert.ToUInt32(RNG.Next(1, 8))));
- break;
- case 101:
- StarPoints.Add(new PowerUps(Content.Load<Texture2D>(@"Images\PowerUps"), position, this, 100));
- break;
- case 100:
- xMine.Add(new PowerUps(Content.Load<Texture2D>(@"Images\PowerUps"), position, this, 200));
- break;
- }
- }
- }
- void updatePowerUpList()
- {
- powerUpsTextPut = "";
- if (powerUpsID.Count == 0)
- {
- powerUpsTextPut = "None";
- }
- else if (powerUpsID.Count >= 15)
- {
- //10 max powerups
- powerUpsID.RemoveAt(powerUpsID.Count() - 1);
- }
- foreach (int p in powerUpsID)
- {
- powerUpsTextPut += getPowerUpName(p) + "\n";
- }
- }
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- Paddle paddle;
- List<Ball> ball = new List<Ball>();
- public Random RNG;
- public Collision.circle ballPos;
- List<Brick> Bricks = new List<Brick>();
- public List<Collision.circle> BrickPos = new List<Collision.circle>();
- List<Collision.segment2D> mapSegments = new List<Collision.segment2D>();
- public List<Collision.segment2D> PaddleSides = new List<Collision.segment2D>();
- Texture2D background;
- List<PowerUps> StarPoints = new List<PowerUps>();
- public List<Collision.circle> StarPointsPos = new List<Collision.circle>();
- List<PowerUps> xMine = new List<PowerUps>();
- public List<Collision.circle> xMinePos = new List<Collision.circle>();
- List<PowerUps> powerUps = new List<PowerUps>();
- public List<Collision.circle> powerUpPos = new List<Collision.circle>();
- SoundEffect music;
- SoundEffectInstance soundEffectInstance;
- public SoundEffectInstance bump;
- public void SpawnPowerUp(Vector2 position)
- {
- Random randomGen = new Random();
- if (randomGen.Next(1, 100) < 10)
- {//10% chance to spawn regular powerup
- uint random = Convert.ToUInt32(randomGen.Next(1, 7));
- //position = new Vector2(320, 200);
- powerUps.Add(new PowerUps(Content.Load<Texture2D>(@"Images\PowerUps"), position, this, random));
- }
- if (randomGen.Next(1, 100) < 3)
- {//3% chance to spawn split powerup
- uint random = Convert.ToUInt32(7);
- //position = new Vector2(320, 200);
- powerUps.Add(new PowerUps(Content.Load<Texture2D>(@"Images\PowerUps"), position, this, random));
- }
- if (randomGen.Next(1, 100) < 30)//30% chance to spawn PowerStar
- {
- StarPoints.Add(new PowerUps(Content.Load<Texture2D>(@"Images\PowerUps"), position, this, 100));
- }
- if (randomGen.Next(1, 100) < 20)//20% chance to spawn xMine
- {
- xMine.Add(new PowerUps(Content.Load<Texture2D>(@"Images\PowerUps"), position, this, 200));
- }
- }
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- }
- /// <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()
- {
- background = Content.Load<Texture2D>(@"Images\Background");
- music = Content.Load<SoundEffect>(@"media\music");
- soundEffectInstance = music.CreateInstance();
- soundEffectInstance.IsLooped = true;
- soundEffectInstance.Play();
- music = Content.Load<SoundEffect>(@"media\bump");
- bump = music.CreateInstance();
- paddle = new Paddle(Content.Load<Texture2D>(@"Images\Paddle"), new Vector2(320, 450));
- RNG = new Random();
- // Background = Content.Load<Texture2D>(@"Images\Background");
- for (int i = 0; i < 5; i++)
- {
- ball.Add(new Ball(Content.Load<Texture2D>(@"Images\Ball"), new Vector2(550 + 15 * i, 450), this, 1));
- }
- ballPos.R = ball[0].ballRadius;
- Random random = new Random();
- levelImage = Content.Load<Texture2D>(@"LevelImages\BrickBreakerText");
- BlockPlacement = PixelToIDs(levelImage);
- for (int y = 0; y < levelImage.Height; y++)
- {
- for (int x = 0; x < levelImage.Width; x++)
- {
- uint blockType = Convert.ToUInt32(BlockPlacement[(y * levelImage.Width) + x]);
- Bricks.Add(new Brick(Content.Load<Texture2D>(@"Images\Bricks"), new Vector2((x * 20) + 20, (y * 20) + 20), this, blockType, Convert.ToUInt32(random.Next(1, 3))));
- }
- }
- //for (int x = 20; x <= 500; x += 20)
- //{
- // for (int y = 20; y <= 60; y += 20)
- // {
- // Bricks.Add(new Brick(Content.Load<Texture2D>(@"Images\Bricks"), new Vector2(x, y), this, 3, Convert.ToUInt32(random.Next(3, 5))));
- // }
- //}
- //for (int x = 20; x <= 500; x += 20)
- //{
- // for (int y = 80; y <= 120; y += 20)
- // {
- // Bricks.Add(new Brick(Content.Load<Texture2D>(@"Images\Bricks"), new Vector2(x, y), this, 2, Convert.ToUInt32(random.Next(1, 5))));
- // }
- //}
- //for (int x = 20; x <= 500; x += 20)
- //{
- // for (int y = 140; y <= 180; y += 20)
- // {
- // Bricks.Add(new Brick(Content.Load<Texture2D>(@"Images\Bricks"), new Vector2(x, y), this, 1, Convert.ToUInt32(random.Next(1, 3))));
- // }
- //}
- foreach (Brick b in Bricks)
- {
- BrickPos.Add(new Collision.circle(b.position, b.radius));
- }
- foreach (PowerUps p in powerUps)
- {
- powerUpPos.Add(new Collision.circle(p.position, p.radius));
- }
- foreach (PowerUps s in StarPoints)
- {
- StarPointsPos.Add(new Collision.circle(s.position, s.radius));
- }
- foreach (PowerUps m in xMine)
- {
- xMinePos.Add(new Collision.circle(m.position, m.radius));
- }
- base.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()
- {
- scoreFont = Content.Load<SpriteFont>(@"fonts\score");
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- for (int i = 0; i < 5; i++)
- {
- ball[i].textureImage = Content.Load<Texture2D>(@"Images\Ball");
- }
- // TODO: use this.Content to load your game content here
- }
- /// <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)
- {
- timeSinceLastEnter++;
- if (powerUpsID.Count != 0 && timeSinceLastEnter > 20)
- {
- //testString = "true";
- keyboardState = Keyboard.GetState();
- if (keyboardState.IsKeyDown(Keys.X))
- {
- timeSinceLastEnter = 0;
- powerUpsID.RemoveAt(0);
- updatePowerUpList();
- }
- if (keyboardState.IsKeyDown(Keys.Enter))
- {
- timeSinceLastEnter = 0;
- int currentPowerUp = powerUpsID.First();
- powerUpsID.RemoveAt(0);
- updatePowerUpList();
- switch (currentPowerUp)
- {
- case 1:
- foreach (Ball b in ball)
- {
- if (b.position.X < 540)
- {
- //This weird layout with speed changers is to prevent a bug where the speed is set too high
- //and the speed isn't fixed fast enough
- if (b.speed - 3 <= 3)
- {
- b.speed = 3;
- }
- else
- {
- b.maxSpeed -= 4;
- }
- }
- }
- break;
- case 2:
- foreach (Ball b in ball)
- {
- if (b.position.X < 540)
- {
- //This weird layout with speed changers is to prevent a bug where the speed is set too high
- //and the speed isn't fixed fast enough
- if (b.speed + 3 >= 10)
- {
- b.speed = 10;
- }
- else
- {
- b.maxSpeed += 3;
- }
- }
- }
- break;
- case 3:
- int setX = 450;
- int lastPos = 0;
- foreach (Ball b in ball)
- {
- if (b.position.X > lastPos)
- {
- setX = (int)b.position.X;
- }
- }
- setX += 15;
- ball.Add(new Ball(Content.Load<Texture2D>(@"Images\Ball"), new Vector2(setX, 450), this, 1));
- break;
- case 4:
- foreach (Ball b in ball)
- {
- if (b.position.X < 540)
- {
- b.SetAnimation("2");
- }
- }
- break;
- case 5:
- foreach (Ball b in ball)
- {
- if (b.position.X < 540)
- {
- b.SetAnimation("3");
- }
- }
- break;
- case 6:
- foreach (Ball b in ball)
- {
- if (b.position.X < 540)
- {
- b.SetAnimation("4");
- }
- }
- break;
- case 7:
- int ballCounter = 0;
- foreach (Ball b in ball.ToList())
- {
- if (b.position.X < 540 & ballCounter < 3)
- {
- ballCounter++;
- }
- }
- if (ballCounter <= 2)
- {
- foreach (Ball b in ball.ToList())
- {
- if (b.position.X < 540)
- {
- ball.Add(new Ball(Content.Load<Texture2D>(@"Images\Ball"), new Vector2(b.position.X, b.position.Y), this, 1));
- ball.Last().direction.X = b.direction.X + 1;
- ball.Last().speed = 7;
- ball.Last().BallType = b.BallType;
- }
- }
- }
- break;
- }
- }
- }
- // Allows the game to exit
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
- for (int i = 0; i < ball.Count; i++)
- {
- if (ball[i].deleteMe)
- {
- ball.RemoveAt(i);
- }
- }
- for (int i = 0; i < Bricks.Count; i++)
- {
- if (Bricks[i].deleteMe)
- {
- Bricks.RemoveAt(i);
- BrickPos.RemoveAt(i);
- }
- }
- for (int i = 0; i < powerUps.Count; i++)
- {
- if (powerUps[i].deleteMe)
- {
- powerUps.RemoveAt(i);
- }
- }
- for (int i = 0; i < StarPoints.Count; i++)
- {
- if (StarPoints[i].deleteMe)
- {
- StarPoints.RemoveAt(i);
- }
- }
- for (int i = 0; i < xMine.Count; i++)
- {
- if (xMine[i].deleteMe)
- {
- xMine.RemoveAt(i);
- }
- }
- if (ball.Count > 0)
- {
- ballPos.P = ball[0].position;
- }
- else
- {
- ballPos.P = Vector2.Zero;
- }
- foreach (Brick b in Bricks)
- {
- b.Update(gameTime, Window.ClientBounds);
- }
- foreach (PowerUps p in powerUps)
- {
- p.Update(gameTime, Window.ClientBounds);
- }
- foreach (PowerUps s in StarPoints)
- {
- s.Update(gameTime, Window.ClientBounds);
- }
- foreach (PowerUps m in xMine)
- {
- m.Update(gameTime, Window.ClientBounds);
- }
- // TODO: Add your update logic here
- paddle.Update(gameTime, Window.ClientBounds);
- PaddleSides = new List<Collision.segment2D>();
- PaddleSides.Add(paddle.paddleLeft);
- PaddleSides.Add(paddle.paddleRight);
- if (ball.Count > 0)
- {
- if (ball[0].position.X > 540)
- {
- ball[0].position.X = paddle.position.X + 50;
- ball[0].position.Y = paddle.position.Y - 16;
- }
- if (ball[0].position.Y > 500)
- {
- ball.RemoveAt(0);
- }
- }
- foreach (Ball b in ball)
- {
- b.Update(gameTime, Window.ClientBounds);
- }
- 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.Black);
- spriteBatch.Begin();
- spriteBatch.Draw(background, Vector2.Zero, Color.White);
- //spriteBatch.DrawString(scoreFont, "Test:" + testString, new Vector2(200, 300), Color.Black, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
- spriteBatch.DrawString(scoreFont, "Power ups:", new Vector2(550, 20), Color.Black, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
- spriteBatch.DrawString(scoreFont, "Enter to use", new Vector2(550, 40), Color.Black, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
- spriteBatch.DrawString(scoreFont, powerUpsTextPut, new Vector2(550, 70), Color.Black, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
- updatePowerUpList();
- spriteBatch.DrawString(scoreFont, "Score: " + score.ToString(), new Vector2(550, 420), Color.Black, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
- foreach (Brick b in Bricks)
- {
- b.Draw(gameTime, spriteBatch);
- }
- foreach (PowerUps p in powerUps)
- {
- p.Draw(gameTime, spriteBatch);
- }
- foreach (PowerUps s in StarPoints)
- {
- s.Draw(gameTime, spriteBatch);
- }
- foreach (PowerUps m in xMine)
- {
- m.Draw(gameTime, spriteBatch);
- }
- foreach (Ball b in ball)
- {
- b.Draw(gameTime, spriteBatch);
- }
- paddle.Draw(gameTime, spriteBatch);
- // threeRings.Draw(gameTime,spriteBatch);
- //threeRings2.Draw(gameTime, spriteBatch);
- //threeRings3.Draw(gameTime, spriteBatch);
- // foreach (Sprite s in RingList)
- //{
- // s.Draw(gameTime, spriteBatch);
- // }
- spriteBatch.End();
- // TODO: Add your drawing code here
- base.Draw(gameTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement