Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.Windows.Threading;
  16. using WpfApplication2;
  17. using System.Media;
  18. using System.IO;
  19. using Microsoft.Win32;
  20. using System.Runtime.InteropServices.WindowsRuntime;
  21. using System.Text;
  22. namespace SpaceInvaders
  23. {
  24. public partial class MainWindow : Window
  25. {
  26. Random rand = new Random();
  27. private DispatcherTimer strafeTimer;
  28. private DispatcherTimer bulletTimer;
  29. string bulletPath = "Resources/donaldthumb.png";
  30. private List<CustomShape> enemies = new List<CustomShape>();
  31. private List<CustomShape> shipbullets = new List<CustomShape>();
  32. private List<CustomShape> enemybullets = new List<CustomShape>();
  33. CustomShape barrier1 = new CustomShape();
  34. CustomShape barrier2 = new CustomShape();
  35. CustomShape barrier3 = new CustomShape();
  36. CustomShape ship = new CustomShape();
  37. double bulletSpeed = 8;
  38. double speed = 1;
  39. bool leftPressed;
  40. bool rightPressed;
  41. bool isPaused = false;
  42. int killCount = 0;
  43. int difficulty = 1;
  44. int rows = 3;
  45. int cols = 8;
  46. double top = 0.0;
  47. SoundPlayer player = new System.Media.SoundPlayer("Resources/shotSound.wav");
  48. public MainWindow()
  49. {
  50.  
  51. Loaded += delegate
  52. {
  53. InitializeComponent();
  54. Credits.Text = "Writen By:\n Eric Hughes\n Roan Chamberlain\n Jon Depaz";
  55. };
  56. }
  57. private void NewGameClick(object sender, RoutedEventArgs e)
  58. {
  59. MainWindow window = new MainWindow();
  60. Credits.Foreground = null;
  61. window.Close();
  62. start_button.Visibility = Visibility.Hidden;
  63. kills.Visibility = Visibility.Visible;
  64. startButtonText.Visibility = Visibility.Hidden;
  65. strafeTimer = new DispatcherTimer();
  66. bulletTimer = new DispatcherTimer();
  67. strafeTimer.Interval = new TimeSpan(0, 0, 0, 0, 10);
  68. strafeTimer.Tick += move;
  69. bulletTimer.Interval = new TimeSpan(0, 0, 0, 0, 10);
  70. bulletTimer.Tick += moveBullet;
  71. string barrierPath = "Resources/barrier.png";
  72. barrier1.shape = new Rectangle();
  73. barrier2.shape = new Rectangle();
  74. barrier3.shape = new Rectangle();
  75. barrier1.shape.Width = 100;
  76. barrier1.shape.Height = 50;
  77. barrier1.shape.Fill = Brushes.Cyan;
  78. barrier2.shape.Width = 100;
  79. barrier2.shape.Height = 50;
  80. barrier2.shape.Fill = Brushes.Cyan;
  81. barrier3.shape.Width = 100;
  82. barrier3.shape.Height = 50;
  83. barrier3.shape.Fill = Brushes.Cyan;
  84. barrier1.shape.Fill = new ImageBrush(new BitmapImage(new Uri(barrierPath, UriKind.Relative)));
  85. barrier2.shape.Fill = new ImageBrush(new BitmapImage(new Uri(barrierPath, UriKind.Relative)));
  86. barrier3.shape.Fill = new ImageBrush(new BitmapImage(new Uri(barrierPath, UriKind.Relative)));
  87. Canvas.SetLeft(barrier1.shape, 10);
  88. Canvas.SetBottom(barrier1.shape, 50);
  89. Canvas.SetLeft(barrier2.shape, 200);
  90. Canvas.SetBottom(barrier2.shape, 50);
  91. Canvas.SetLeft(barrier3.shape, 400);
  92. Canvas.SetBottom(barrier3.shape, 50);
  93. canvas.Children.Add(barrier1.shape);
  94. canvas.Children.Add(barrier2.shape);
  95. canvas.Children.Add(barrier3.shape);
  96. ship.shape = new Rectangle();
  97. ship.Name = "Ship";
  98. ship.shape.Width = 50;
  99. ship.shape.Height = 50;
  100. String shipPath = "Resources/ship.png";
  101. String backGroundPath = "Resources/background.gif";
  102. ship.shape.Fill = new ImageBrush(new BitmapImage(new Uri(shipPath, UriKind.Relative)));
  103. Canvas.SetLeft(ship.shape, 200);
  104. Canvas.SetBottom(ship.shape, 10);
  105. canvas.Children.Add(ship.shape);
  106. canvas.Background = new ImageBrush(new BitmapImage(new Uri(backGroundPath, UriKind.Relative)));
  107. createLevel(difficulty);
  108. }
  109. public void createLevel(int difficulty)
  110. {
  111. if (difficulty > 1)
  112. {
  113. rows++;
  114. speed += 0.5;
  115. }
  116. else if (difficulty > 3)
  117. cols++;
  118. var FoeYSpacing = 0.0;
  119. var FoeXSpacing = 1.0;
  120. for (int i = 0; i < rows; i++)
  121. {
  122. for (int j = 0; j < cols; j++)
  123. {
  124. string relativePath = "Resources/hilaryclintonface.png";
  125. CustomShape foe = new CustomShape(); //create the rectangle
  126. foe.shape = new Rectangle();
  127. foe.shape.Fill = new ImageBrush(new BitmapImage(new Uri(relativePath, UriKind.Relative)));
  128. foe.Name = "Enemy row" + i + " col " + j;
  129. foe.shape.Width = 50.0;
  130. foe.shape.Height = 50.0;
  131. foe.PositionX = FoeXSpacing;
  132. foe.PositionY = FoeYSpacing;
  133. foe.Health = 3;
  134. Canvas.SetLeft(foe.shape, FoeXSpacing);
  135. FoeXSpacing += foe.shape.Width;
  136. Canvas.SetTop(foe.shape, FoeYSpacing);
  137. enemies.Add(foe);
  138. }
  139. FoeXSpacing = 0.0;
  140. FoeYSpacing += enemies[i].shape.Height;
  141. }
  142. foreach (CustomShape foe in enemies)
  143. {
  144. canvas.Children.Add(foe.shape);
  145. }
  146. strafeTimer.Start();
  147. }
  148. public void move(object sender, EventArgs e)
  149. {
  150. if (enemies.Count == 0)
  151. {
  152. strafeTimer.Stop();
  153. top = 0.0;
  154. foreach (var item in shipbullets)
  155. {
  156. canvas.Children.Remove(item.shape);
  157. }
  158. bulletTimer.Stop();
  159. shipbullets.Clear();
  160. createLevel(++difficulty);
  161. }
  162. Boolean diretionChanged = false;
  163. for (int i = 0; i < enemies.Count; i++)
  164. {
  165. double canvaswidth = Math.Round(canvas.ActualWidth);
  166. if (enemies[i].PositionX + enemies[i].shape.Width >= canvaswidth)
  167. {
  168. speed = -speed;
  169. top = 2;
  170. diretionChanged = true;
  171. for (int j = 0; j < enemies.Count; j++)
  172. {
  173. enemies[j].PositionY += top;
  174. Canvas.SetTop(enemies[j].shape, enemies[j].PositionY);
  175. }
  176. }
  177. else if (enemies[i].PositionX < 0)
  178. {
  179. speed = -speed;
  180. top = 2.0;
  181. diretionChanged = true;
  182. for (int j = 0; j < enemies.Count; j++)
  183. {
  184. enemies[j].PositionY += top;
  185. Canvas.SetTop(enemies[j].shape, enemies[j].PositionY);
  186. }
  187. }
  188. if (diretionChanged)
  189. {
  190. for (int k = 0; k < enemies.Count; k++)
  191. {
  192. enemies[k].PositionX += speed;
  193. Canvas.SetLeft(enemies[k].shape, enemies[k].PositionX);
  194. }
  195. diretionChanged = false;
  196. i = enemies.Count;
  197. }
  198. else
  199. {
  200. enemies[i].PositionX += speed;
  201. Canvas.SetLeft(enemies[i].shape, enemies[i].PositionX);
  202. }
  203. }
  204. double x = Canvas.GetLeft(ship.shape);
  205. if (leftPressed)
  206. {
  207. if (x > 0)
  208. {
  209. x -= 3;
  210. Canvas.SetLeft(ship.shape, x);
  211. }
  212. }
  213. if (rightPressed)
  214. {
  215. if (x + ship.shape.ActualWidth < canvas.ActualWidth)
  216. {
  217. x += 3;
  218. Canvas.SetLeft(ship.shape, x);
  219. }
  220. }
  221. }
  222. public void updateKillCount()
  223. {
  224. killCount++;
  225. kills.Text = Convert.ToString(killCount);
  226. }
  227. public void moveBullet(object sender, EventArgs e)
  228. {
  229. int enemyCount = enemies.Count;
  230. for (int i = 0; i < enemyCount; i++)
  231. {
  232. for (int j = 0; j < shipbullets.Count; j++)
  233. {
  234. try
  235. {
  236. if ((shipbullets[j].PositionY <= enemies[i].PositionY + enemies[i].shape.Height && shipbullets[j].PositionY >= enemies[i].PositionY)
  237. &&
  238. (shipbullets[j].PositionX + shipbullets[j].shape.Width > enemies[i].PositionX &&
  239. shipbullets[j].PositionX <= enemies[i].PositionX + enemies[i].shape.Width))
  240. {
  241. canvas.Children.Remove(enemies[i].shape);
  242. canvas.Children.Remove(shipbullets[j].shape);
  243. enemies.Remove(enemies[i]);
  244. shipbullets.Remove(shipbullets[j]);
  245. enemyCount = enemies.Count;
  246. updateKillCount();
  247. }
  248. else if (shipbullets[j].PositionY < 0)
  249. {
  250. canvas.Children.Remove(shipbullets[j].shape);
  251. shipbullets.Remove(shipbullets[j]);
  252. }
  253. }
  254. catch (ArgumentOutOfRangeException ioe)
  255. {
  256. continue;
  257. }
  258. }
  259. }
  260. for (int z = 0; z < shipbullets.Count; z++)
  261. {
  262. shipbullets[z].PositionY -= bulletSpeed;
  263. Canvas.SetTop(shipbullets[z].shape, shipbullets[z].PositionY);
  264. }
  265. }
  266. private void kDown(object sender, KeyEventArgs e)
  267. {
  268. switch (e.Key)
  269. {
  270. case Key.Left:
  271. leftPressed = true;
  272. break;
  273. case Key.Right:
  274. rightPressed = true;
  275. break;
  276. case Key.P:
  277. if (isPaused)
  278. {
  279. strafeTimer.Start();
  280. isPaused = !isPaused;
  281. paused2.Visibility = Visibility.Hidden;
  282. paused.Visibility = Visibility.Hidden;
  283. break;
  284. }
  285. strafeTimer.Stop();
  286. isPaused = !isPaused;
  287. paused2.Visibility = Visibility.Visible;
  288. paused.Visibility = Visibility.Visible;
  289. break;
  290. case Key.S:
  291. saveFile();
  292. break;
  293. }
  294. }
  295. private void kUp(object sender, KeyEventArgs e)
  296. {
  297. switch (e.Key)
  298. {
  299. case Key.Left:
  300. leftPressed = false;
  301. break;
  302. case Key.Right:
  303. rightPressed = false;
  304. break;
  305. case Key.Space:
  306. CustomShape bullet = new CustomShape();
  307. bullet.shape = new Rectangle();
  308. //player.Play();
  309. bullet.shape.Fill = new ImageBrush(new BitmapImage(new Uri(bulletPath, UriKind.Relative)));
  310. bullet.Name = "Bullet";
  311. bullet.shape.Width = 10;
  312. bullet.shape.Height = 20;
  313. Canvas.SetTop(bullet.shape, canvas.ActualHeight - ship.shape.ActualHeight);
  314. Canvas.SetLeft(bullet.shape, Canvas.GetLeft(ship.shape) + (ship.shape.ActualWidth / 2.0));
  315. shipbullets.Add(bullet);
  316. bullet.PositionY = Canvas.GetTop(bullet.shape);
  317. bullet.PositionX = Canvas.GetLeft(bullet.shape);
  318. canvas.Children.Add(bullet.shape);
  319. bulletTimer.Start();
  320. break;
  321. }
  322. }
  323. private void saveFile()
  324. {
  325. strafeTimer.Stop();
  326. string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  327. List<String> state = new List<String>();
  328. OpenFileDialog fileChooser = new OpenFileDialog();
  329. fileChooser.ShowDialog();
  330. String fileName = fileChooser.FileName;
  331. state.Add("count:" + enemies.Count);
  332. for (int i = 0; i < enemies.Count; i++)
  333. {
  334. state.Add(i + ":" + enemies[i].PositionX + ":" + enemies[i].PositionY + ":" + enemies[i].shape.Height + ":" + enemies[i].shape.Width + ":" + enemies[i].Health);
  335. }
  336. state.Add("--End Enemies");
  337. CustomShape a = enemies[1];
  338. using (StreamWriter outputFile = new StreamWriter(fileName))
  339. {
  340. foreach (string line in state)
  341. outputFile.WriteLine(line);
  342. }
  343. strafeTimer.Start();
  344. }
  345. //Window.DialogResult result;
  346. //String fileName;
  347. //OpenFileDialog fileChooser = new OpenFileDialog()
  348. //result = fileChooser.ShowDialog();
  349. //fileName = fileChooser.FileName;
  350. //FileStream file = new FileStream;
  351. //StreamWriter a = new StreamWriter();
  352. }
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement