Guest User

Untitled

a guest
Jun 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Drawing.Drawing2D;
  10. using System.IO;
  11. using System.Reflection;
  12. using System.Diagnostics;
  13.  
  14. namespace Spillet
  15. {
  16.     public partial class Form2 : Form
  17.     {
  18.         private bool ActiveTorpedo = false;
  19.  
  20.         int t = 450;
  21.         bool[] h;
  22.         string[] k;
  23.  
  24.         Baat Ubaaten;
  25.         Torpedo Torpedoen;
  26.         Battleship[] Battleshipen = new Battleship[10];
  27.  
  28.         Battleship dust = new Battleship();
  29.  
  30.         public Form2()
  31.         {
  32.             InitializeComponent();
  33.             Assembly myAssembly = Assembly.GetExecutingAssembly();
  34.         }
  35.  
  36.         private void Form2_Load(object sender, EventArgs e)
  37.         {
  38.             this.Ubaaten = new Baat();
  39.             this.Controls.Add(this.Ubaaten);
  40.  
  41.             this.Torpedoen = new Torpedo();
  42.             this.Controls.Add(this.Torpedoen);
  43.  
  44.             h = new bool[10];
  45.             k = new string[10];
  46.  
  47.             this.Controls.Add(this.dust);
  48.  
  49.             for (int i = 0; i < 10; i++)
  50.             {
  51.                 this.Controls.Add(this.Battleshipen[i]);
  52.                 //h[i] = false;
  53.                 //Debug.WriteLine("h[i] er " + h[i]);
  54.                 //if (h[i] == false)
  55.                 //{
  56.                 //    k[i] = "false";
  57.                 //    Debug.WriteLine("k[i] er " + h[i]);
  58.                 //}
  59.                 //else
  60.                 //{
  61.                 //    k[i] = "true";
  62.                 //    Debug.WriteLine("k[i] er " + h[i]);
  63.                 //}
  64.             }
  65.             timer2.Enabled = true;
  66.         }
  67.  
  68.         private void Form2_KeyDown(object sender, KeyEventArgs e)
  69.         {
  70.             int ubaatposisjon = Ubaaten.Location.X;
  71.  
  72.             switch (e.KeyData)
  73.             {
  74.                 case Keys.Up:
  75.                     if (ActiveTorpedo == false)
  76.                     {
  77.                         t = 450;
  78.                         Torpedoen.Location = new System.Drawing.Point(ubaatposisjon, t);
  79.                         ActiveTorpedo = true;
  80.                         this.Refresh();
  81.                     }
  82.                         break;
  83.  
  84.                 case Keys.Down:
  85.                         // hmm
  86.                     this.Refresh();
  87.                     break;
  88.  
  89.                 case Keys.Left:
  90.                     if (ubaatposisjon - 50 >= 0)
  91.                     {
  92.                         Ubaaten.Location = new Point(ubaatposisjon -= 20, 450);
  93.                         this.Refresh();
  94.                     }
  95.                     break;
  96.  
  97.                 case Keys.Right:
  98.                     if (ubaatposisjon + 20 < 530)
  99.                     {
  100.                         Ubaaten.Location = new Point(ubaatposisjon += 20, 450);
  101.                         this.Refresh();
  102.                     }
  103.                     break;
  104.             }
  105.            
  106.         }
  107.  
  108.         private void timer1_Tick(object sender, EventArgs e)
  109.         {
  110.             t = t - 6;
  111.            
  112.             Torpedoen.Location = new System.Drawing.Point(Torpedoen.Location.X, t);
  113.            
  114.             if (Torpedoen.Location.Y <= 0)
  115.             {
  116.                 ActiveTorpedo = false;
  117.             }
  118.             this.Invalidate();
  119.         }
  120.  
  121.         private void timer2_Tick(object sender, EventArgs e)
  122.         {
  123.             Random hih = new Random();
  124.             int tilfeldig = hih.Next(0, 9);
  125.             Debug.WriteLine("tilfeldig er " + tilfeldig);
  126.  
  127.             //if (h[tilfeldig] == false)
  128.             //{
  129.                
  130.                 Battleshipen[tilfeldig].Enabled = true;
  131.             //}
  132.         }
  133.     }
  134. }
  135. _________________________Battleship Klassen___________________________
  136. using System;
  137. using System.Collections.Generic;
  138. using System.Linq;
  139. using System.Text;
  140. using System.Drawing;
  141. using System.Windows.Forms;
  142. using System.IO;
  143. using System.Reflection;
  144. using System.Timers;
  145. using System.Diagnostics;
  146.  
  147. namespace Spillet
  148. {
  149.     class Battleship : PictureBox
  150.     {
  151.         private Assembly assembly;
  152.         private Stream stream;
  153.         int p = 0;
  154.         int x = 0;
  155.  
  156.         System.Timers.Timer t = new System.Timers.Timer();
  157.  
  158.         public Battleship()
  159.         {
  160.             assembly = Assembly.GetExecutingAssembly();
  161.  
  162.             stream = assembly.GetManifestResourceStream("Spillet.Battleship.gif");
  163.  
  164.             this.Image = Image.FromStream(stream);
  165.  
  166.             this.Width = 90;
  167.             this.Height = 60;
  168.             this.Location = new Point(100, p);
  169.             this.Enabled = false;
  170.             this.Visible = true;
  171.             this.SizeMode = PictureBoxSizeMode.StretchImage;
  172.             Debug.WriteLine("Battleship() er igang");
  173.             sup();
  174.         }
  175.  
  176.         public Battleship(int p)
  177.         {
  178.             int[] placement = new int[7];
  179.             placement[0] = 20;
  180.             placement[1] = 60;
  181.             placement[2] = 100;
  182.             placement[3] = 140;
  183.             placement[4] = 180;
  184.             placement[5] = 220;
  185.             placement[6] = 260;
  186.  
  187.             Random random = new Random();
  188.             int hm = random.Next(0, 6);
  189.  
  190.             p = placement[hm];
  191.         }
  192.  
  193.         public void flytt(object source, ElapsedEventArgs e)
  194.         {
  195.             //if (this.Location.X >= -101)
  196.             //{
  197.                 Debug.WriteLine("flytt() er igang");
  198.                 x = x + 5;
  199.                 this.Location = new System.Drawing.Point(this.Location.X + x, this.Location.Y);
  200.             //}
  201.         }
  202.  
  203.         public void sup()
  204.         {
  205.             Debug.WriteLine("sup() er igang");
  206.             t.Elapsed += new ElapsedEventHandler(flytt);
  207.             t.Interval = 500;
  208.             t.Enabled = true;
  209.         }
  210.     }
  211. }
Add Comment
Please, Sign In to add comment