Guest User

Untitled

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