Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using SharpDX.DirectInput;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace UPJoystick
- {
- public partial class MainWindow : Form
- {
- Panel target;
- Joystick joystick = null;
- bool work = true;
- int counterValue = 0;
- int x = 30000;
- int y = 30000;
- Thread thread;
- bool team = false;
- Random rnd = new Random();
- int timeleft;
- public MainWindow(Joystick joystick)
- {
- this.joystick = joystick;
- InitializeComponent();
- target = new Panel();
- target.Parent = PoleBitwy;
- target.Width = 50;
- target.Height = 50;
- target.Visible = true;
- target.Left = 100;
- target.Top = 100;
- target.BackColor = Color.Blue;
- }
- private void MainWindow_Load(object sender, EventArgs e)
- {
- if (joystick == null)
- {
- return;
- }
- // Set BufferSize in order to use buffered data.
- joystick.Properties.BufferSize = 128;
- // Acquire the joystick
- joystick.Acquire();
- thread = new Thread(new ThreadStart(CheckPosition));
- thread.Start();
- }
- private void UpdateCounter(JoystickUpdate value)
- {
- if (InvokeRequired)
- {
- this.Invoke(new Action<JoystickUpdate>(UpdateCounter), new object[] { value });
- return;
- }
- outTextBox.Text = value.ToString();
- if (value.Offset.ToString() == "X") x = value.Value;
- if (value.Offset.ToString() == "Y") y = value.Value;
- if (value.Offset.ToString() == "Buttons0")
- {
- if (Player.Checked == true) Player.Checked = false;
- else Player.Checked = true;
- if (Player.Top > target.Top && Player.Top < target.Top + 20 && Player.Left > target.Left && Player.Left < target.Left + 20)
- {
- target.Left = rnd.Next(10, 430);
- target.Top = rnd.Next(10, 430);
- if(timer1.Enabled)
- {
- if (Player.BackColor == Color.Olive) lScoreY.Text = (Convert.ToInt32(lScoreY.Text) + 1).ToString();
- if (Player.BackColor == Color.Tomato) lScoreX.Text = (Convert.ToInt32(lScoreX.Text) + 1).ToString();
- }
- }
- }
- if (value.Offset.ToString() == "Buttons9" && team)
- {
- team = !team;
- Player.BackColor = Color.Olive;
- timeleft = Convert.ToInt32( numericUpDown1.Value);
- timer1.Enabled = true;
- lScoreY.Text = "0";
- }
- if (value.Offset.ToString() == "Buttons10" && !team)
- {
- team = !team;
- Player.BackColor = Color.Tomato;
- timeleft = Convert.ToInt32(numericUpDown1.Value);
- timer1.Enabled = true;
- lScoreX.Text = "0";
- }
- }
- private void MoveObject(int value)
- {
- if (InvokeRequired)
- {
- this.Invoke(new Action<int>(MoveObject), new object[] { value });
- return;
- }
- if(Player.Top>18)
- if (y < 32000) Player.Top -= CalculateSpeed(y);
- if (Player.Top < 460)
- if (y > 33500) Player.Top += CalculateSpeed(y);
- if(Player.Left>18)
- if (x < 32000) Player.Left -= CalculateSpeed(x);
- if(Player.Left<460)
- if (x > 33500) Player.Left += CalculateSpeed(x);
- labelTime.Text = timeleft.ToString();
- }
- private int CalculateSpeed(int value)
- {
- if (InvokeRequired)
- {
- this.Invoke(new Action<int>(MoveObject), new object[] { value });
- return 0;
- }
- int newValue = 0;
- if (value > 33500) newValue = value - 33500;
- else newValue = 32000 - value;
- return newValue / 2500;
- }
- private void CheckPosition()
- {
- while (work)
- {
- joystick.Poll();
- JoystickUpdate[] datas = joystick.GetBufferedData();
- foreach (JoystickUpdate state in datas)
- {
- //if(state.Value == 128)
- UpdateCounter(state);
- }
- MoveObject(0);
- Thread.Sleep(20);
- }
- }
- private void outTextBox_TextChanged(object sender, EventArgs e)
- {
- outTextBox.SelectionStart = outTextBox.Text.Length;
- outTextBox.ScrollToCaret();
- }
- private void Start_Click(object sender, EventArgs e)
- {
- }
- private void MainWindow_Closing(object sender, FormClosingEventArgs e)
- {
- work = false;
- thread.Abort();
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- if (timeleft > 0) timeleft--;
- else timer1.Enabled = false;
- }
- //private void Stop_Click(object sender, EventArgs e)
- //{
- // updateTextBox = false;
- // Start.Enabled = true;
- // Stop.Enabled = false;
- //}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement