Advertisement
Guest User

window

a guest
Nov 13th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.83 KB | None | 0 0
  1. using SharpDX.DirectInput;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12.  
  13. namespace UPJoystick
  14. {
  15.     public partial class MainWindow : Form
  16.     {
  17.         Panel target;
  18.         Joystick joystick = null;
  19.         bool work = true;
  20.         int counterValue = 0;
  21.         int x = 30000;
  22.         int y = 30000;
  23.         Thread thread;
  24.         bool team = false;
  25.         Random rnd = new Random();
  26.         int timeleft;
  27.        
  28.         public MainWindow(Joystick joystick)
  29.         {
  30.             this.joystick = joystick;
  31.            
  32.             InitializeComponent();
  33.             target = new Panel();
  34.             target.Parent = PoleBitwy;
  35.             target.Width = 50;
  36.             target.Height = 50;
  37.             target.Visible = true;
  38.             target.Left = 100;
  39.             target.Top = 100;
  40.             target.BackColor = Color.Blue;
  41.         }
  42.  
  43.         private void MainWindow_Load(object sender, EventArgs e)
  44.         {
  45.  
  46.             if (joystick == null)
  47.             {
  48.                 return;
  49.             }
  50.  
  51.  
  52.             // Set BufferSize in order to use buffered data.
  53.             joystick.Properties.BufferSize = 128;
  54.  
  55.             // Acquire the joystick
  56.             joystick.Acquire();
  57.  
  58.             thread = new Thread(new ThreadStart(CheckPosition));
  59.             thread.Start();
  60.         }
  61.  
  62.         private void UpdateCounter(JoystickUpdate value)
  63.         {
  64.             if (InvokeRequired)
  65.             {
  66.                 this.Invoke(new Action<JoystickUpdate>(UpdateCounter), new object[] { value });
  67.                 return;
  68.             }
  69.  
  70.             outTextBox.Text = value.ToString();
  71.  
  72.             if (value.Offset.ToString() == "X") x = value.Value;
  73.             if (value.Offset.ToString() == "Y") y = value.Value;
  74.             if (value.Offset.ToString() == "Buttons0")
  75.             {
  76.                 if (Player.Checked == true) Player.Checked = false;
  77.                 else Player.Checked = true;
  78.                 if (Player.Top > target.Top && Player.Top < target.Top + 20 && Player.Left > target.Left && Player.Left < target.Left + 20)
  79.                 {
  80.  
  81.                     target.Left = rnd.Next(10, 430);
  82.                     target.Top = rnd.Next(10, 430);
  83.                     if(timer1.Enabled)
  84.                     {
  85.                         if (Player.BackColor == Color.Olive) lScoreY.Text = (Convert.ToInt32(lScoreY.Text) + 1).ToString();
  86.                         if (Player.BackColor == Color.Tomato) lScoreX.Text = (Convert.ToInt32(lScoreX.Text) + 1).ToString();
  87.                     }
  88.                    
  89.                 }
  90.             }
  91.             if (value.Offset.ToString() == "Buttons9" && team)
  92.             {
  93.                 team = !team;
  94.                 Player.BackColor = Color.Olive;
  95.                 timeleft = Convert.ToInt32( numericUpDown1.Value);
  96.                 timer1.Enabled = true;
  97.                 lScoreY.Text = "0";
  98.  
  99.             }
  100.             if (value.Offset.ToString() == "Buttons10" && !team)
  101.             {
  102.                 team = !team;
  103.                 Player.BackColor = Color.Tomato;
  104.                 timeleft = Convert.ToInt32(numericUpDown1.Value);
  105.                 timer1.Enabled = true;
  106.                 lScoreX.Text = "0";
  107.             }
  108.  
  109.         }
  110.  
  111.         private void MoveObject(int value)
  112.         {
  113.             if (InvokeRequired)
  114.             {
  115.                 this.Invoke(new Action<int>(MoveObject), new object[] { value });
  116.                 return;
  117.             }
  118.             if(Player.Top>18)
  119.                 if (y < 32000) Player.Top -= CalculateSpeed(y);
  120.             if (Player.Top < 460)
  121.                 if (y > 33500) Player.Top += CalculateSpeed(y);
  122.             if(Player.Left>18)
  123.                 if (x < 32000) Player.Left -= CalculateSpeed(x);
  124.             if(Player.Left<460)
  125.                 if (x > 33500) Player.Left += CalculateSpeed(x);
  126.             labelTime.Text = timeleft.ToString();
  127.         }
  128.  
  129.         private int CalculateSpeed(int value)
  130.         {
  131.             if (InvokeRequired)
  132.             {
  133.                 this.Invoke(new Action<int>(MoveObject), new object[] { value });
  134.                 return 0;
  135.             }
  136.  
  137.             int newValue = 0;
  138.  
  139.             if (value > 33500) newValue = value - 33500;
  140.             else newValue = 32000 - value;
  141.  
  142.  
  143.             return newValue / 2500;
  144.  
  145.         }
  146.  
  147.  
  148.  
  149.         private void CheckPosition()
  150.         {
  151.             while (work)
  152.             {
  153.                 joystick.Poll();
  154.                 JoystickUpdate[] datas = joystick.GetBufferedData();
  155.                 foreach (JoystickUpdate state in datas)
  156.                 {
  157.                     //if(state.Value == 128)
  158.                         UpdateCounter(state);
  159.                 }
  160.  
  161.                 MoveObject(0);
  162.                
  163.                 Thread.Sleep(20);
  164.             }
  165.            
  166.         }
  167.  
  168.         private void outTextBox_TextChanged(object sender, EventArgs e)
  169.        {
  170.             outTextBox.SelectionStart = outTextBox.Text.Length;
  171.             outTextBox.ScrollToCaret();
  172.         }
  173.  
  174.         private void Start_Click(object sender, EventArgs e)
  175.         {
  176.            
  177.         }
  178.  
  179.         private void MainWindow_Closing(object sender, FormClosingEventArgs e)
  180.         {
  181.             work = false;
  182.             thread.Abort();
  183.         }
  184.  
  185.         private void timer1_Tick(object sender, EventArgs e)
  186.         {
  187.             if (timeleft > 0) timeleft--;
  188.             else timer1.Enabled = false;
  189.         }
  190.  
  191.         //private void Stop_Click(object sender, EventArgs e)
  192.         //{
  193.         //    updateTextBox = false;
  194.         //    Start.Enabled = true;
  195.         //    Stop.Enabled = false;
  196.         //}
  197.  
  198.  
  199.  
  200.  
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement