Advertisement
Guest User

Main Code

a guest
Oct 26th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.24 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 BreakOutClone
  13. {
  14. public class Game1 : Microsoft.Xna.Framework.Game
  15. {
  16. Texture2D levelImage;
  17. List<int> BlockPlacement = new List<int>();
  18. bool checkHex(int inty)
  19. {
  20. if (inty > 127)
  21. {
  22. return true;
  23. }
  24. else
  25. {
  26. return false;
  27. }
  28. }
  29.  
  30.  
  31. List<int> PixelToIDs(Texture2D tex)
  32. {
  33. Color[] texColors = new Color[tex.Height * tex.Width];
  34. tex.GetData(texColors);
  35. List<int> numbers = new List<int>();
  36.  
  37.  
  38.  
  39. foreach (Color c in texColors)
  40. {
  41. if (checkHex(c.A))
  42. {
  43. bool R = checkHex(c.R);
  44. bool B = checkHex(c.B);
  45. bool G = checkHex(c.G);
  46. int numb = 0;
  47.  
  48. if (R & B & G)
  49. {
  50. //white
  51. numb = 9;
  52. }
  53. else if (R & !B & !G)
  54. {
  55. //red
  56. numb = 5;
  57. if (RNG.Next(1, 100) < 23)//23% of mines
  58. {
  59. numb = 100;
  60. }
  61. numb = 100;//takes out the randomness (To submit to jim, so it's easier for him to see what each block does)
  62. }
  63. else if (B & !R & !G)
  64. {
  65. //blue
  66. numb = 8;
  67. }
  68. else if (G & !R & !B)
  69. {
  70. //green
  71. numb = 7;
  72. if (RNG.Next(1, 100) < 23)//23% of powerup block
  73. {
  74. numb = 102;
  75. }
  76. numb = 102;//takes out the randomness (To submit to jim, so it's easier for him to see what each block does)
  77. }
  78. else if (!R & !B & !G)
  79. {
  80. //black
  81. numb = 4;
  82. }
  83. else if (!R)
  84. {
  85. //turqoise
  86. numb = 1;
  87. }
  88. else if (!G)
  89. {
  90. //Purple
  91. numb = 2;
  92. }
  93. else if (!B)
  94. {
  95. //yelow
  96. numb = 6;
  97. if (RNG.Next(1, 100) < 23)//23% of Stars
  98. {
  99. numb = 101;
  100. }
  101. numb = 101;//takes out the randomness (To submit to jim, so it's easier for him to see what each block does)
  102. }
  103. numbers.Add(numb);
  104. }
  105. else
  106. {
  107. numbers.Add(0);
  108. }
  109. }
  110.  
  111. return numbers;
  112. }
  113.  
  114.  
  115. SpriteFont scoreFont;
  116. KeyboardState keyboardState;
  117. public List<int> powerUpsID = new List<int>();
  118.  
  119. public int score = 0;
  120. public string testString = "";
  121. int timeSinceLastEnter = 0;
  122. public string powerUpsTextPut = "";
  123.  
  124. public string getPowerUpName(int id)
  125. {
  126. switch (id)
  127. {
  128. case 1:
  129. return "Slow ball";
  130. case 2:
  131. return "Fast Ball";
  132. case 3:
  133. return "+1 Life";
  134. case 4:
  135. return "Big Ball";
  136. case 5:
  137. return "Small Ball";
  138. case 6:
  139. return "Dangerous Ball";
  140. case 7:
  141. return "Split Balls!";
  142. default:
  143. return "Unknown..Crap";
  144. }
  145. }
  146. public void asorbePowerUp(int powerUP)
  147. {
  148. if (powerUP == 100)
  149. {
  150. Random random = new Random();
  151. score += random.Next(100, 200);
  152. }
  153. else if (powerUP == 200)
  154. {
  155. Random random = new Random();
  156. score -= random.Next(200, 400);
  157. }
  158. else
  159. {
  160. powerUpsID.Add(powerUP);
  161. updatePowerUpList();
  162. }
  163. }
  164.  
  165.  
  166. public void spawnPercisePowerUps(Vector2 position, int Type123)
  167. {
  168. Random random = new Random();
  169. int spawnAmount = random.Next(3, 8);
  170. if (Type123 == 102)
  171. {
  172. //So less powerup fall
  173. spawnAmount = random.Next(3, 5);
  174. }
  175. for (int i = 0; i < spawnAmount; i++)
  176. {
  177. switch (Type123)
  178. {
  179. case 102:
  180. powerUps.Add(new PowerUps(Content.Load<Texture2D>(@"Images\PowerUps"), position, this, Convert.ToUInt32(RNG.Next(1, 8))));
  181. break;
  182. case 101:
  183. StarPoints.Add(new PowerUps(Content.Load<Texture2D>(@"Images\PowerUps"), position, this, 100));
  184. break;
  185. case 100:
  186. xMine.Add(new PowerUps(Content.Load<Texture2D>(@"Images\PowerUps"), position, this, 200));
  187. break;
  188. }
  189. }
  190. }
  191.  
  192.  
  193. void updatePowerUpList()
  194. {
  195. powerUpsTextPut = "";
  196. if (powerUpsID.Count == 0)
  197. {
  198. powerUpsTextPut = "None";
  199. }
  200. else if (powerUpsID.Count >= 15)
  201. {
  202. //10 max powerups
  203. powerUpsID.RemoveAt(powerUpsID.Count() - 1);
  204. }
  205. foreach (int p in powerUpsID)
  206. {
  207. powerUpsTextPut += getPowerUpName(p) + "\n";
  208. }
  209. }
  210.  
  211. GraphicsDeviceManager graphics;
  212. SpriteBatch spriteBatch;
  213. Paddle paddle;
  214. List<Ball> ball = new List<Ball>();
  215. public Random RNG;
  216. public Collision.circle ballPos;
  217. List<Brick> Bricks = new List<Brick>();
  218. public List<Collision.circle> BrickPos = new List<Collision.circle>();
  219. List<Collision.segment2D> mapSegments = new List<Collision.segment2D>();
  220. public List<Collision.segment2D> PaddleSides = new List<Collision.segment2D>();
  221. Texture2D background;
  222.  
  223.  
  224. List<PowerUps> StarPoints = new List<PowerUps>();
  225. public List<Collision.circle> StarPointsPos = new List<Collision.circle>();
  226.  
  227. List<PowerUps> xMine = new List<PowerUps>();
  228. public List<Collision.circle> xMinePos = new List<Collision.circle>();
  229.  
  230. List<PowerUps> powerUps = new List<PowerUps>();
  231. public List<Collision.circle> powerUpPos = new List<Collision.circle>();
  232. SoundEffect music;
  233. SoundEffectInstance soundEffectInstance;
  234. public SoundEffectInstance bump;
  235.  
  236.  
  237. public void SpawnPowerUp(Vector2 position)
  238. {
  239. Random randomGen = new Random();
  240. if (randomGen.Next(1, 100) < 10)
  241. {//10% chance to spawn regular powerup
  242. uint random = Convert.ToUInt32(randomGen.Next(1, 7));
  243. //position = new Vector2(320, 200);
  244. powerUps.Add(new PowerUps(Content.Load<Texture2D>(@"Images\PowerUps"), position, this, random));
  245. }
  246. if (randomGen.Next(1, 100) < 3)
  247. {//3% chance to spawn split powerup
  248. uint random = Convert.ToUInt32(7);
  249. //position = new Vector2(320, 200);
  250. powerUps.Add(new PowerUps(Content.Load<Texture2D>(@"Images\PowerUps"), position, this, random));
  251. }
  252.  
  253. if (randomGen.Next(1, 100) < 30)//30% chance to spawn PowerStar
  254. {
  255. StarPoints.Add(new PowerUps(Content.Load<Texture2D>(@"Images\PowerUps"), position, this, 100));
  256. }
  257. if (randomGen.Next(1, 100) < 20)//20% chance to spawn xMine
  258. {
  259. xMine.Add(new PowerUps(Content.Load<Texture2D>(@"Images\PowerUps"), position, this, 200));
  260. }
  261. }
  262.  
  263.  
  264.  
  265.  
  266. public Game1()
  267. {
  268. graphics = new GraphicsDeviceManager(this);
  269. Content.RootDirectory = "Content";
  270. }
  271.  
  272. /// <summary>
  273. /// Allows the game to perform any initialization it needs to before starting to run.
  274. /// This is where it can query for any required services and load any non-graphic
  275. /// related content. Calling base.Initialize will enumerate through any components
  276. /// and initialize them as well.
  277. /// </summary>
  278. protected override void Initialize()
  279. {
  280.  
  281.  
  282.  
  283.  
  284. background = Content.Load<Texture2D>(@"Images\Background");
  285.  
  286. music = Content.Load<SoundEffect>(@"media\music");
  287. soundEffectInstance = music.CreateInstance();
  288. soundEffectInstance.IsLooped = true;
  289. soundEffectInstance.Play();
  290.  
  291.  
  292. music = Content.Load<SoundEffect>(@"media\bump");
  293. bump = music.CreateInstance();
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302. paddle = new Paddle(Content.Load<Texture2D>(@"Images\Paddle"), new Vector2(320, 450));
  303. RNG = new Random();
  304.  
  305. // Background = Content.Load<Texture2D>(@"Images\Background");
  306.  
  307. for (int i = 0; i < 5; i++)
  308. {
  309. ball.Add(new Ball(Content.Load<Texture2D>(@"Images\Ball"), new Vector2(550 + 15 * i, 450), this, 1));
  310. }
  311. ballPos.R = ball[0].ballRadius;
  312.  
  313.  
  314. Random random = new Random();
  315. levelImage = Content.Load<Texture2D>(@"LevelImages\BrickBreakerText");
  316.  
  317. BlockPlacement = PixelToIDs(levelImage);
  318. for (int y = 0; y < levelImage.Height; y++)
  319. {
  320. for (int x = 0; x < levelImage.Width; x++)
  321. {
  322. uint blockType = Convert.ToUInt32(BlockPlacement[(y * levelImage.Width) + x]);
  323. 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))));
  324. }
  325. }
  326.  
  327.  
  328.  
  329.  
  330. //for (int x = 20; x <= 500; x += 20)
  331. //{
  332. // for (int y = 20; y <= 60; y += 20)
  333. // {
  334. // Bricks.Add(new Brick(Content.Load<Texture2D>(@"Images\Bricks"), new Vector2(x, y), this, 3, Convert.ToUInt32(random.Next(3, 5))));
  335. // }
  336. //}
  337.  
  338.  
  339. //for (int x = 20; x <= 500; x += 20)
  340. //{
  341. // for (int y = 80; y <= 120; y += 20)
  342. // {
  343. // Bricks.Add(new Brick(Content.Load<Texture2D>(@"Images\Bricks"), new Vector2(x, y), this, 2, Convert.ToUInt32(random.Next(1, 5))));
  344. // }
  345. //}
  346.  
  347. //for (int x = 20; x <= 500; x += 20)
  348. //{
  349. // for (int y = 140; y <= 180; y += 20)
  350. // {
  351. // Bricks.Add(new Brick(Content.Load<Texture2D>(@"Images\Bricks"), new Vector2(x, y), this, 1, Convert.ToUInt32(random.Next(1, 3))));
  352. // }
  353. //}
  354. foreach (Brick b in Bricks)
  355. {
  356. BrickPos.Add(new Collision.circle(b.position, b.radius));
  357. }
  358. foreach (PowerUps p in powerUps)
  359. {
  360. powerUpPos.Add(new Collision.circle(p.position, p.radius));
  361. }
  362. foreach (PowerUps s in StarPoints)
  363. {
  364. StarPointsPos.Add(new Collision.circle(s.position, s.radius));
  365. }
  366. foreach (PowerUps m in xMine)
  367. {
  368. xMinePos.Add(new Collision.circle(m.position, m.radius));
  369. }
  370.  
  371.  
  372. base.Initialize();
  373. // TODO: Add your initialization logic here
  374.  
  375. base.Initialize();
  376. }
  377.  
  378. /// <summary>
  379. /// LoadContent will be called once per game and is the place to load
  380. /// all of your content.
  381. /// </summary>
  382. protected override void LoadContent()
  383. {
  384.  
  385. scoreFont = Content.Load<SpriteFont>(@"fonts\score");
  386.  
  387.  
  388.  
  389.  
  390. // Create a new SpriteBatch, which can be used to draw textures.
  391. spriteBatch = new SpriteBatch(GraphicsDevice);
  392. for (int i = 0; i < 5; i++)
  393. {
  394. ball[i].textureImage = Content.Load<Texture2D>(@"Images\Ball");
  395. }
  396.  
  397. // TODO: use this.Content to load your game content here
  398. }
  399.  
  400. /// <summary>
  401. /// UnloadContent will be called once per game and is the place to unload
  402. /// all content.
  403. /// </summary>
  404. protected override void UnloadContent()
  405. {
  406. // TODO: Unload any non ContentManager content here
  407. }
  408.  
  409. /// <summary>
  410. /// Allows the game to run logic such as updating the world,
  411. /// checking for collisions, gathering input, and playing audio.
  412. /// </summary>
  413. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  414. protected override void Update(GameTime gameTime)
  415. {
  416. timeSinceLastEnter++;
  417. if (powerUpsID.Count != 0 && timeSinceLastEnter > 20)
  418. {
  419. //testString = "true";
  420. keyboardState = Keyboard.GetState();
  421. if (keyboardState.IsKeyDown(Keys.X))
  422. {
  423. timeSinceLastEnter = 0;
  424. powerUpsID.RemoveAt(0);
  425. updatePowerUpList();
  426. }
  427. if (keyboardState.IsKeyDown(Keys.Enter))
  428. {
  429. timeSinceLastEnter = 0;
  430. int currentPowerUp = powerUpsID.First();
  431. powerUpsID.RemoveAt(0);
  432. updatePowerUpList();
  433. switch (currentPowerUp)
  434. {
  435.  
  436. case 1:
  437. foreach (Ball b in ball)
  438. {
  439. if (b.position.X < 540)
  440. {
  441. //This weird layout with speed changers is to prevent a bug where the speed is set too high
  442. //and the speed isn't fixed fast enough
  443. if (b.speed - 3 <= 3)
  444. {
  445. b.speed = 3;
  446. }
  447. else
  448. {
  449. b.maxSpeed -= 4;
  450. }
  451. }
  452. }
  453. break;
  454. case 2:
  455. foreach (Ball b in ball)
  456. {
  457. if (b.position.X < 540)
  458. {
  459. //This weird layout with speed changers is to prevent a bug where the speed is set too high
  460. //and the speed isn't fixed fast enough
  461. if (b.speed + 3 >= 10)
  462. {
  463. b.speed = 10;
  464. }
  465. else
  466. {
  467. b.maxSpeed += 3;
  468. }
  469.  
  470. }
  471. }
  472. break;
  473. case 3:
  474. int setX = 450;
  475. int lastPos = 0;
  476. foreach (Ball b in ball)
  477. {
  478. if (b.position.X > lastPos)
  479. {
  480. setX = (int)b.position.X;
  481. }
  482. }
  483. setX += 15;
  484. ball.Add(new Ball(Content.Load<Texture2D>(@"Images\Ball"), new Vector2(setX, 450), this, 1));
  485. break;
  486. case 4:
  487. foreach (Ball b in ball)
  488. {
  489. if (b.position.X < 540)
  490. {
  491. b.SetAnimation("2");
  492. }
  493. }
  494. break;
  495. case 5:
  496. foreach (Ball b in ball)
  497. {
  498. if (b.position.X < 540)
  499. {
  500. b.SetAnimation("3");
  501. }
  502. }
  503. break;
  504. case 6:
  505. foreach (Ball b in ball)
  506. {
  507. if (b.position.X < 540)
  508. {
  509. b.SetAnimation("4");
  510. }
  511. }
  512. break;
  513. case 7:
  514. int ballCounter = 0;
  515. foreach (Ball b in ball.ToList())
  516. {
  517. if (b.position.X < 540 & ballCounter < 3)
  518. {
  519. ballCounter++;
  520. }
  521. }
  522.  
  523. if (ballCounter <= 2)
  524. {
  525. foreach (Ball b in ball.ToList())
  526. {
  527. if (b.position.X < 540)
  528. {
  529. ball.Add(new Ball(Content.Load<Texture2D>(@"Images\Ball"), new Vector2(b.position.X, b.position.Y), this, 1));
  530. ball.Last().direction.X = b.direction.X + 1;
  531. ball.Last().speed = 7;
  532. ball.Last().BallType = b.BallType;
  533. }
  534. }
  535. }
  536. break;
  537. }
  538. }
  539. }
  540.  
  541.  
  542. // Allows the game to exit
  543. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  544. this.Exit();
  545.  
  546. for (int i = 0; i < ball.Count; i++)
  547. {
  548. if (ball[i].deleteMe)
  549. {
  550. ball.RemoveAt(i);
  551. }
  552. }
  553. for (int i = 0; i < Bricks.Count; i++)
  554. {
  555. if (Bricks[i].deleteMe)
  556. {
  557. Bricks.RemoveAt(i);
  558. BrickPos.RemoveAt(i);
  559. }
  560. }
  561. for (int i = 0; i < powerUps.Count; i++)
  562. {
  563. if (powerUps[i].deleteMe)
  564. {
  565. powerUps.RemoveAt(i);
  566. }
  567. }
  568. for (int i = 0; i < StarPoints.Count; i++)
  569. {
  570. if (StarPoints[i].deleteMe)
  571. {
  572. StarPoints.RemoveAt(i);
  573. }
  574. }
  575. for (int i = 0; i < xMine.Count; i++)
  576. {
  577. if (xMine[i].deleteMe)
  578. {
  579. xMine.RemoveAt(i);
  580. }
  581. }
  582.  
  583.  
  584. if (ball.Count > 0)
  585. {
  586. ballPos.P = ball[0].position;
  587. }
  588. else
  589. {
  590. ballPos.P = Vector2.Zero;
  591. }
  592.  
  593.  
  594. foreach (Brick b in Bricks)
  595. {
  596. b.Update(gameTime, Window.ClientBounds);
  597. }
  598.  
  599. foreach (PowerUps p in powerUps)
  600. {
  601. p.Update(gameTime, Window.ClientBounds);
  602. }
  603. foreach (PowerUps s in StarPoints)
  604. {
  605. s.Update(gameTime, Window.ClientBounds);
  606. }
  607. foreach (PowerUps m in xMine)
  608. {
  609. m.Update(gameTime, Window.ClientBounds);
  610. }
  611.  
  612. // TODO: Add your update logic here
  613. paddle.Update(gameTime, Window.ClientBounds);
  614. PaddleSides = new List<Collision.segment2D>();
  615. PaddleSides.Add(paddle.paddleLeft);
  616. PaddleSides.Add(paddle.paddleRight);
  617.  
  618. if (ball.Count > 0)
  619. {
  620. if (ball[0].position.X > 540)
  621. {
  622. ball[0].position.X = paddle.position.X + 50;
  623. ball[0].position.Y = paddle.position.Y - 16;
  624. }
  625. if (ball[0].position.Y > 500)
  626. {
  627. ball.RemoveAt(0);
  628. }
  629. }
  630. foreach (Ball b in ball)
  631. {
  632. b.Update(gameTime, Window.ClientBounds);
  633. }
  634.  
  635.  
  636. base.Update(gameTime);
  637. }
  638.  
  639. /// <summary>
  640. /// This is called when the game should draw itself.
  641. /// </summary>
  642. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  643. protected override void Draw(GameTime gameTime)
  644. {
  645. GraphicsDevice.Clear(Color.Black);
  646. spriteBatch.Begin();
  647. spriteBatch.Draw(background, Vector2.Zero, Color.White);
  648.  
  649. //spriteBatch.DrawString(scoreFont, "Test:" + testString, new Vector2(200, 300), Color.Black, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
  650. spriteBatch.DrawString(scoreFont, "Power ups:", new Vector2(550, 20), Color.Black, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
  651. spriteBatch.DrawString(scoreFont, "Enter to use", new Vector2(550, 40), Color.Black, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
  652. spriteBatch.DrawString(scoreFont, powerUpsTextPut, new Vector2(550, 70), Color.Black, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
  653.  
  654. updatePowerUpList();
  655. spriteBatch.DrawString(scoreFont, "Score: " + score.ToString(), new Vector2(550, 420), Color.Black, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
  656.  
  657.  
  658. foreach (Brick b in Bricks)
  659. {
  660. b.Draw(gameTime, spriteBatch);
  661. }
  662.  
  663. foreach (PowerUps p in powerUps)
  664. {
  665. p.Draw(gameTime, spriteBatch);
  666. }
  667. foreach (PowerUps s in StarPoints)
  668. {
  669. s.Draw(gameTime, spriteBatch);
  670. }
  671. foreach (PowerUps m in xMine)
  672. {
  673. m.Draw(gameTime, spriteBatch);
  674. }
  675.  
  676. foreach (Ball b in ball)
  677. {
  678. b.Draw(gameTime, spriteBatch);
  679. }
  680. paddle.Draw(gameTime, spriteBatch);
  681.  
  682.  
  683. // threeRings.Draw(gameTime,spriteBatch);
  684. //threeRings2.Draw(gameTime, spriteBatch);
  685. //threeRings3.Draw(gameTime, spriteBatch);
  686. // foreach (Sprite s in RingList)
  687. //{
  688. // s.Draw(gameTime, spriteBatch);
  689. // }
  690. spriteBatch.End();
  691. // TODO: Add your drawing code here
  692. base.Draw(gameTime);
  693. }
  694. }
  695. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement